Skip to content

Commit 30ef749

Browse files
committed
Merge pull request django-oscar#1482 from machtfit/tables-plural-caption
Tables plural caption
2 parents 7bef0db + bbd54df commit 30ef749

File tree

7 files changed

+54
-12
lines changed

7 files changed

+54
-12
lines changed

docs/source/releases/v1.1.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ What's new in Oscar 1.1?
2828
------------------------
2929

3030
* The frontend templates have been updated to use Bootstrap 3 instead of version 2 (`#1576`_)
31+
* The icon and caption of `django-tables2` tables can be set directly on the `Table` object, if it
32+
derives from :class:`~oscar.apps.dashboard.tables.DashboardTable`. The caption can be localized
33+
in singular and plural. (`#1482`_)
3134

3235
.. _`#1576`: https://github.com/django-oscar/django-oscar/pull/1576
36+
.. _`#1482`: https://github.com/django-oscar/django-oscar/pull/1482
3337

3438

3539
.. _minor_changes_in_1.1:

oscar/apps/dashboard/catalogue/tables.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.utils.safestring import mark_safe
2-
from django.utils.translation import ugettext_lazy as _
2+
from django.utils.translation import ugettext_lazy as _, ugettext_noop
33

4-
from django_tables2 import Table, Column, LinkColumn, TemplateColumn, A
4+
from django_tables2 import Column, LinkColumn, TemplateColumn, A
55

66
from oscar.core.loading import get_class, get_model
77

@@ -10,7 +10,7 @@
1010
Category = get_model('catalogue', 'Category')
1111

1212

13-
class ProductTable(Table):
13+
class ProductTable(DashboardTable):
1414
title = TemplateColumn(
1515
verbose_name=_('Title'),
1616
template_name='dashboard/catalogue/product_row_title.html',
@@ -37,6 +37,8 @@ class ProductTable(Table):
3737
template_name='dashboard/catalogue/product_row_actions.html',
3838
orderable=False)
3939

40+
icon = "sitemap"
41+
4042
class Meta(DashboardTable.Meta):
4143
model = Product
4244
fields = ('upc', 'date_updated')
@@ -45,7 +47,7 @@ class Meta(DashboardTable.Meta):
4547
order_by = '-date_updated'
4648

4749

48-
class CategoryTable(Table):
50+
class CategoryTable(DashboardTable):
4951
name = LinkColumn('dashboard:catalogue-category-update', args=[A('pk')])
5052
description = TemplateColumn(
5153
template_code='{{ record.description|default:""|striptags'
@@ -61,6 +63,10 @@ class CategoryTable(Table):
6163
template_name='dashboard/catalogue/category_row_actions.html',
6264
orderable=False)
6365

66+
icon = "sitemap"
67+
caption_singular = ugettext_noop("{count} Category")
68+
caption_plural = ugettext_noop("{count} Categories")
69+
6470
class Meta(DashboardTable.Meta):
6571
model = Category
6672
fields = ('name', 'description')

oscar/apps/dashboard/catalogue/views.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,21 @@ def get_context_data(self, **kwargs):
7676
ctx = super(ProductListView, self).get_context_data(**kwargs)
7777
ctx['form'] = self.form
7878
ctx['productclass_form'] = self.productclass_form_class()
79-
ctx['queryset_description'] = self.get_description(self.form)
8079
return ctx
8180

8281
def get_description(self, form):
8382
if form.is_valid() and any(form.cleaned_data.values()):
8483
return _('Product search results')
8584
return _('Products')
8685

86+
def get_table(self, **kwargs):
87+
if 'recently_edited' in self.request.GET:
88+
kwargs.update(dict(orderable=False))
89+
90+
table = super(ProductListView, self).get_table(**kwargs)
91+
table.caption = self.get_description(self.form)
92+
return table
93+
8794
def get_table_pagination(self):
8895
return dict(per_page=20)
8996

@@ -514,7 +521,6 @@ def get_queryset(self):
514521
def get_context_data(self, *args, **kwargs):
515522
ctx = super(CategoryListView, self).get_context_data(*args, **kwargs)
516523
ctx['child_categories'] = Category.get_root_nodes()
517-
ctx['queryset_description'] = _("Categories")
518524
return ctx
519525

520526

@@ -533,7 +539,6 @@ def get_context_data(self, *args, **kwargs):
533539
**kwargs)
534540
ctx['child_categories'] = self.object.get_children()
535541
ctx['ancestors'] = self.object.get_ancestors_and_self()
536-
ctx['queryset_description'] = _("Categories")
537542
return ctx
538543

539544

oscar/apps/dashboard/tables.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1+
from django.utils.translation import ungettext, ugettext_noop
2+
13
from django_tables2 import Table
24

35

46
class DashboardTable(Table):
7+
_caption = None
8+
caption_singular = ugettext_noop('{count} Row')
9+
caption_plural = ugettext_noop('{count} Rows')
10+
11+
@property
12+
def caption(self):
13+
if self._caption:
14+
return self._caption
15+
16+
return (ungettext(self.caption_singular, self.caption_plural,
17+
self.paginator.count)
18+
.format(count=self.paginator.count))
19+
20+
@caption.setter
21+
def caption(self, caption):
22+
self._caption = caption
23+
524
class Meta:
625
template = 'dashboard/table.html'
726
attrs = {'class': 'table table-striped table-bordered'}

oscar/apps/dashboard/users/tables.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from django_tables2 import Table, LinkColumn, TemplateColumn, Column, A
1+
from django_tables2 import LinkColumn, TemplateColumn, Column, A
22

33
from oscar.core.loading import get_class
44

55
DashboardTable = get_class('dashboard.tables', 'DashboardTable')
66

77

8-
class UserTable(Table):
8+
class UserTable(DashboardTable):
99
check = TemplateColumn(
1010
template_name='dashboard/users/user_row_checkbox.html',
1111
verbose_name=' ', orderable=False)
@@ -21,5 +21,7 @@ class UserTable(Table):
2121
template_name='dashboard/users/user_row_actions.html',
2222
verbose_name=' ')
2323

24+
icon = "group"
25+
2426
class Meta(DashboardTable.Meta):
2527
template = 'dashboard/users/table.html'

oscar/apps/dashboard/users/views.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,14 @@ def apply_search_filters(self, queryset, data):
9494

9595
return queryset
9696

97+
def get_table(self, **kwargs):
98+
table = super(IndexView, self).get_table(**kwargs)
99+
table.caption = self.desc_template % self.desc_ctx
100+
return table
101+
97102
def get_context_data(self, **kwargs):
98103
context = super(IndexView, self).get_context_data(**kwargs)
99104
context['form'] = self.form
100-
context['queryset_description'] = self.desc_template % self.desc_ctx
101-
context['queryset_icon'] = 'group'
102105
return context
103106

104107
def make_inactive(self, request, users):

oscar/templates/oscar/dashboard/table.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
<caption>
77
{% block table.caption %}
88
<h3 class="pull-left">
9-
<i class="icon-{{ queryset_icon|default:"sitemap" }} icon-large"></i>{{ queryset_description }}
9+
{% if table.icon %}
10+
<i class="icon-{{ table.icon }} icon-large"></i>
11+
{% endif %}
12+
{{ table.caption }}
1013
</h3>
1114
{% endblock %}
1215
</caption>

0 commit comments

Comments
 (0)