Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Small refactor of get_filter_chips

michal0ae7b0a1063c
diff --git a/saleor/dashboard/product/filters.py b/saleor/dashboard/product/filters.py
index 6cc541bf18..b566f6da1b 100644
--- a/saleor/dashboard/product/filters.py
+++ b/saleor/dashboard/product/filters.py
@@ -4,7 +4,7 @@ from django_filters import (FilterSet, RangeFilter, OrderingFilter)
from django.utils.translation import pgettext_lazy
from django_prices.models import PriceField
-from ...product.models import Product
+from ...product.models import Category, Product
SORT_BY_FIELDS = {'name': pgettext_lazy('Product list sorting option', 'name'),
@@ -26,3 +26,27 @@ class ProductFilter(FilterSet):
'filter_class': RangeFilter
}
}
+
+ def get_chips(self):
+ categories = Category.objects.all()
+ chips = []
+ for iterator, item in enumerate(self.form):
+ if item.name is not 'sort_by' and item.value():
+ label = item.label
+ if isinstance(item.value(), (list, tuple)):
+ if item.name == 'price':
+ if any(item.value()):
+ chips.append(label + ': ' + ' - '.join(item.value()))
+ elif item.name == 'categories':
+ for value in item.value():
+ chips.append(
+ label + ': ' + categories.get(pk=value).name)
+ else:
+ for value in item.value():
+ chips.append(label + ': ' + str(value))
+ else:
+ value = item.value()
+ chips.append(label + ': ' + str(value))
+ else:
+ continue
+ return chips
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 8cc1aa5129..2832c60433 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -110,7 +110,7 @@ def product_list(request):
products = Product.objects.prefetch_related('images')
products = products.order_by('name')
product_classes = ProductClass.objects.all()
- product_categories = Category.objects.all()
+
form = forms.ProductClassSelectorForm(
request.POST or None, product_classes=product_classes)
if form.is_valid():
@@ -121,36 +121,12 @@ def product_list(request):
product_filter.qs, DASHBOARD_PAGINATE_BY, request.GET.get('page'))
sort_by = request.GET.get('sort_by')
- def get_filter_chips(product_filter):
- chips = []
- for iterator, item in enumerate(product_filter.form):
- if item.name is not 'sort_by' and item.value():
- label = item.label
- if isinstance(item.value(), (list, tuple)):
- if item.name == 'price':
- if any(item.value()):
- chips.append(label + ': ' + ' - '.join(item.value()))
- elif item.name == 'categories':
- for value in item.value():
- chips.append(
- label + ': ' + product_categories.get(pk=value).name)
- else:
- for value in item.value():
- chips.append(label + ': ' + str(value))
- else:
- value = item.value()
- chips.append(label + ': ' + str(value))
- else:
- continue
- return chips
-
- chips = get_filter_chips(product_filter)
-
ctx = {
'is_descending': sort_by.startswith('-') if sort_by else False,
'bulk_action_form': forms.ProductBulkUpdate(), 'form': form,
'products': products, 'product_classes': product_classes,
- 'filter': product_filter, 'chips': chips}
+ 'filter': product_filter,
+ }
return TemplateResponse(request, 'dashboard/product/list.html', ctx)
diff --git a/saleor/dashboard/templatetags/utils.py b/saleor/dashboard/templatetags/utils.py
index 15906b9630..352f72dcb7 100644
--- a/saleor/dashboard/templatetags/utils.py
+++ b/saleor/dashboard/templatetags/utils.py
@@ -70,3 +70,10 @@ def margin_for_variant(stock):
def margins_for_variant(variant):
margins = get_variant_costs_data(variant)['margins']
return margins
+
+
[email protected]_tag('dashboard/includes/_filters.html', takes_context=True)
+def add_filters(context, filter_set):
+ chips = filter_set.get_chips()
+ context['chips'] = chips
+ return context
diff --git a/templates/dashboard/product/list.html b/templates/dashboard/product/list.html
index 43a7c01e4f..f830fb8503 100644
--- a/templates/dashboard/product/list.html
+++ b/templates/dashboard/product/list.html
@@ -138,7 +138,8 @@
{% endif %}
</div>
<div class="col s12 l3" id="product-filters">
- {% include 'dashboard/includes/_filters.html' %}
+ {% add_filters filter %}
+ {#{% include 'dashboard/includes/_filters.html' %}#}
</div>
</div>
<div id="modal-product-class" class="modal">