From d03a11f87d90c2ba60be52503a3b1f8a26cebdd6 Mon Sep 17 00:00:00 2001 From: michal Date: Fri, 6 Oct 2017 13:25:48 +0200 Subject: Review changes Change everything from last review. --- saleor/product/filters.py | 38 ++++++++++++++++++-------------- saleor/product/templatetags/sort_by.py | 8 +++---- saleor/product/templatetags/sort_menu.py | 14 +++++++----- saleor/product/views.py | 3 +-- templates/category/index.html | 2 +- 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/saleor/product/filters.py b/saleor/product/filters.py index 300480ee..32f4cec2 100644 --- a/saleor/product/filters.py +++ b/saleor/product/filters.py @@ -24,10 +24,10 @@ class ProductFilter(FilterSet): def __init__(self, *args, **kwargs): self.category = kwargs.pop('category') super(ProductFilter, self).__init__(*args, **kwargs) - self.product_attributes, self.variant_attributes = \ - self._get_attributes(self.category) - self._add_product_attributes_filters() - self._add_product_variants_attributes_filters() + self.product_attributes, self.variant_attributes = ( + self._get_attributes()) + self.filters.update(self._add_product_attributes_filters()) + self.filters.update(self._add_product_variants_attributes_filters()) self.filters = OrderedDict(sorted(self.filters.items())) sort_by = OrderingFilter( @@ -44,34 +44,38 @@ class ProductFilter(FilterSet): } } - def _get_attributes(self, category): - product_attributes = \ - (ProductAttribute.objects.all() - .prefetch_related('values') - .filter(products_class__products__categories=category) - .distinct()) - variant_attributes = \ - (ProductAttribute.objects.all() - .prefetch_related('values') - .filter(product_variants_class__products__categories=category) - .distinct()) + def _get_attributes(self): + product_attributes = ( + ProductAttribute.objects.all() + .prefetch_related('values') + .filter(products_class__products__categories=self.category) + .distinct()) + variant_attributes = ( + ProductAttribute.objects.all() + .prefetch_related('values') + .filter(product_variants_class__products__categories=self.category) + .distinct()) return product_attributes, variant_attributes def _add_product_attributes_filters(self): + filters = {} for attribute in self.product_attributes: - self.filters[attribute.slug] = MultipleChoiceFilter( + filters[attribute.slug] = MultipleChoiceFilter( name='attributes__%s' % attribute.pk, label=attribute.name, widget=CheckboxSelectMultiple, choices=self._get_attribute_choices(attribute)) + return filters def _add_product_variants_attributes_filters(self): + filters = {} for attribute in self.variant_attributes: - self.filters[attribute.slug] = MultipleChoiceFilter( + filters[attribute.slug] = MultipleChoiceFilter( name='variants__attributes__%s' % attribute.pk, label=attribute.name, widget=CheckboxSelectMultiple, choices=self._get_attribute_choices(attribute)) + return filters def _get_attribute_choices(self, attribute): return [(choice.pk, choice.name) for choice in attribute.values.all()] diff --git a/saleor/product/templatetags/sort_by.py b/saleor/product/templatetags/sort_by.py index 8b590a3e..58395c07 100644 --- a/saleor/product/templatetags/sort_by.py +++ b/saleor/product/templatetags/sort_by.py @@ -5,7 +5,7 @@ register = template.Library() @register.inclusion_tag('category/_sort_by.html', takes_context=True) def sort_by(context, attribute): - context['label'] = attribute['label'] - context['ascending_attribute'] = attribute['value'] - context['descending_attribute'] = '-' + str(attribute['value']) - return context + return {'request': context['request'], + 'label': attribute['label'], + 'ascending_attribute': attribute['value'], + 'descending_attribute': '-' + str(attribute['value'])} diff --git a/saleor/product/templatetags/sort_menu.py b/saleor/product/templatetags/sort_menu.py index aa661fbf..abccd555 100644 --- a/saleor/product/templatetags/sort_menu.py +++ b/saleor/product/templatetags/sort_menu.py @@ -7,9 +7,11 @@ register = template.Library() @register.inclusion_tag('category/_sort_menu.html', takes_context=True) def sort_menu(context, attributes): - context['sort_by'] = \ - context['request'].GET.get('sort_by', DEFAULT_SORT).strip('-') - context['sort_by_choices'] = attributes - context['arrow_down'] = \ - (context['request'].GET.get('sort_by', DEFAULT_SORT).startswith('-')) - return context + ctx = { + 'request': context['request'], + 'sort_by': + context['request'].GET.get('sort_by', DEFAULT_SORT).strip('-'), + 'sort_by_choices': attributes, + 'arrow_down': + context['request'].GET.get('sort_by', DEFAULT_SORT).startswith('-')} + return ctx diff --git a/saleor/product/views.py b/saleor/product/views.py index e218285a..3581c867 100644 --- a/saleor/product/views.py +++ b/saleor/product/views.py @@ -130,6 +130,5 @@ def category_index(request, path, category_id): ctx = {'category': category, 'filter': product_filter, 'products': products_and_availability, 'products_paginated': products_paginated, - 'sort_by_choices': SORT_BY_FIELDS, - 'show_pagination': len(products) > PAGINATE_BY} + 'sort_by_choices': SORT_BY_FIELDS} return TemplateResponse(request, 'category/index.html', ctx) diff --git a/templates/category/index.html b/templates/category/index.html index aa511f1e..45be775c 100644 --- a/templates/category/index.html +++ b/templates/category/index.html @@ -132,7 +132,7 @@
- {% if show_pagination %} + {% if products_paginated.has_other_pages %} {% bootstrap_pagination products_paginated extra=request.GET.urlencode %} {% endif %}
-- cgit v1.2.3