Skip to content

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

Rewrite chip creation

dominik-zeglenba3354b6948b
diff --git a/saleor/dashboard/chips.py b/saleor/dashboard/chips.py
new file mode 100644
index 0000000000..6ec3a2563d
--- /dev/null
+++ b/saleor/dashboard/chips.py
@@ -0,0 +1,54 @@
+from django.utils.translation import gettext
+from django.conf import settings
+
+PATTERN = '%s: %s'
+
+
+def handle_default(field, chips_list):
+ chips = chips_list[:]
+ chips.append({'content': PATTERN % (field.label, field.value()),
+ 'name': field.name, 'value': field.value()})
+ return chips
+
+
+def handle_multiplechoice(field, chips_list, context):
+ chips = chips_list[:]
+ for partial_value in [f for f in field.value() if f]:
+ value = context[partial_value]
+ chips.append({'content': PATTERN % (field.label, value),
+ 'name': field.name, 'value': partial_value})
+ return chips
+
+
+def handle_nullboolean(field, chips_list):
+ chips = chips_list[:]
+ values = [
+ gettext('No'),
+ gettext('Yes')
+ ]
+ value = values[1] if field.value() else values[0]
+ chips.append({'content': PATTERN % (field.label, value),
+ 'name': field.name, 'value': 1 if field.value() else 0})
+ return chips
+
+
+def handle_range(field, chips_list):
+ chips = chips_list[:]
+ values = [f if f else None for f in field.value()]
+ text = [gettext('From'), gettext('To')]
+ for i, value in enumerate(values):
+ if value:
+ chips.append(
+ {'content': PATTERN % (field.label, text[i] + ' ' + value),
+ 'name': '%s_%i' % (field.name, i),
+ 'value': field.value()[i]})
+ return chips
+
+
+def handle_choice(field, chips_list):
+ chips = chips_list[:]
+ value = list(filter(lambda x: x[0] == field.value(),
+ field.field._choices))[0][1]
+ chips.append({'content': PATTERN % (field.label, value),
+ 'name': field.name, 'value': field.value()})
+ return chips
diff --git a/saleor/dashboard/product/chips.py b/saleor/dashboard/product/chips.py
deleted file mode 100644
index e40ce936ce..0000000000
--- a/saleor/dashboard/product/chips.py
+++ /dev/null
@@ -1,78 +0,0 @@
-from django.utils.translation import gettext
-from django.conf import settings
-
-PATTERN = '%s: %s'
-
-
-def find_type_in_list(needle, haystack):
- matches = list(filter(lambda x: x in str(needle), haystack))
- return matches[0] if len(matches) else 'default'
-
-
-def handle_default(field, chips, context):
- in_context = field.name in context.keys()
- value = context[field.name][
- str(field.value())] if in_context else field.value()
- chips.append({'content': PATTERN % (field.label, value),
- 'name': field.name, 'value': field.value()})
-
-
-def handle_multiple_select(field, chips, context):
- in_context = field.name in context.keys()
- for partial_value in [f for f in field.value() if f]:
- value = context[field.name][
- partial_value] if in_context else partial_value
- chips.append({'content': PATTERN % (field.label, value),
- 'name': field.name, 'value': partial_value})
-
-
-def handle_boolean(field, chips, context):
- values = context[field.name] if field.name in context.keys() else [
- gettext('No'),
- gettext('Yes')
- ]
- value = values[1] if field.value() else values[0]
- chips.append({'content': PATTERN % (field.label, value),
- 'name': field.name, 'value': 1 if field.value() else 0})
-
-
-def handle_range(field, chips, context):
- values = [
- '%s <span class="currency">%s</span>' % (f, settings.DEFAULT_CURRENCY)
- if f else None for f in field.value()]
- text = [gettext('From'), gettext('To')]
- for i, value in enumerate(values):
- if value:
- chips.append(
- {'content': PATTERN % (field.label, text[i] + ' ' + value),
- 'name': '%s_%i' % (field.name, i),
- 'value': field.value()[i]})
-
-
-DEFAULT_HANDLERS = {
- 'default': handle_default,
- 'NullBooleanField': handle_boolean,
- 'ModelMultipleChoiceField': handle_multiple_select,
- 'RangeField': handle_range,
-}
-
-
-class ChipFactory:
- def __init__(self, form, context=None, handlers=None):
- self.chips = []
- self.context = context
- self.form = form
- factory_handlers = dict(DEFAULT_HANDLERS)
- factory_handlers.update(handlers)
- self.handlers = factory_handlers
-
- def make(self):
- for field in self.form:
- if field.value() not in ['', None]:
- field_type = str(type(field.field))
- if field.name in self.handlers.keys():
- self.handlers[field.name](field, self.chips, self.context)
- else:
- key = find_type_in_list(field_type, self.handlers.keys())
- self.handlers[key](field, self.chips, self.context)
- return self.chips
diff --git a/saleor/dashboard/product/filters.py b/saleor/dashboard/product/filters.py
index b3ba152a38..63dd52a6e2 100644
--- a/saleor/dashboard/product/filters.py
+++ b/saleor/dashboard/product/filters.py
@@ -1,13 +1,10 @@
from __future__ import unicode_literals
-from django_filters import (CharFilter, FilterSet, RangeFilter, OrderingFilter)
from django.utils.translation import pgettext_lazy
+from django_filters import (CharFilter, FilterSet, RangeFilter, OrderingFilter)
from django_prices.models import PriceField
-from django.urls import reverse
-
-from .chips import ChipFactory
-from ...product.models import Category, Product
+from ...product.models import Product
SORT_BY_FIELDS = {'name': pgettext_lazy('Product list sorting option', 'name'),
'price': pgettext_lazy(
@@ -31,13 +28,3 @@ class ProductFilter(FilterSet):
'filter_class': RangeFilter
}
}
-
- def get_chips(self):
- categories = Category.objects.all()
- context = {
- 'categories': {str(c.pk): c.name for c in categories}
- }
- handlers = {
- 'sort_by': lambda field, chips, context: None
- }
- return ChipFactory(self.form, context, handlers).make()
diff --git a/saleor/dashboard/templatetags/utils.py b/saleor/dashboard/templatetags/utils.py
index 352f72dcb7..971fffafe4 100644
--- a/saleor/dashboard/templatetags/utils.py
+++ b/saleor/dashboard/templatetags/utils.py
@@ -9,7 +9,12 @@ except ImportError:
from urllib import urlencode
from django.template import Library
+from django.forms.fields import NullBooleanField, ChoiceField
+from django.forms.models import ModelMultipleChoiceField
+from django_filters.fields import RangeField
from ...product.utils import get_margin_for_variant, get_variant_costs_data
+from ..chips import (handle_nullboolean, handle_default, handle_multiplechoice,
+ handle_range, handle_choice)
register = Library()
@@ -74,6 +79,26 @@ def margins_for_variant(variant):
@register.inclusion_tag('dashboard/includes/_filters.html', takes_context=True)
def add_filters(context, filter_set):
- chips = filter_set.get_chips()
- context['chips'] = chips
- return context
+ chips = []
+ data = filter_set.form.cleaned_data
+ for key in data.keys():
+ if key == 'sort_by':
+ continue
+ field = filter_set.form[key]
+ if field.value() not in ['', None]:
+ if isinstance(field.field, NullBooleanField):
+ chips = handle_nullboolean(field, chips)
+ elif isinstance(field.field, ModelMultipleChoiceField):
+ field_data = {str(o.pk): str(o) for o in data[key]}
+ chips = handle_multiplechoice(field, chips, field_data)
+ elif isinstance(field.field, RangeField):
+ chips = handle_range(field, chips)
+ elif isinstance(field.field, ChoiceField):
+ chips = handle_choice(field, chips)
+ else:
+ chips = handle_default(field, chips)
+ filter_context = {
+ 'chips': chips,
+ 'filter': filter_set
+ }
+ return filter_context
diff --git a/templates/dashboard/customer/list.html b/templates/dashboard/customer/list.html
index b0ccf6fcbb..cfa25488f1 100644
--- a/templates/dashboard/customer/list.html
+++ b/templates/dashboard/customer/list.html
@@ -103,7 +103,7 @@
{% paginate customers %}
</div>
<div class="col s12 l3" id="filters">
- {% include 'dashboard/includes/_filters.html' %}
+ {% add_filters filter %}
</div>
</div>
{% endblock %}
diff --git a/templates/dashboard/discount/sale/list.html b/templates/dashboard/discount/sale/list.html
index 81621f1f89..c35c7782c5 100644
--- a/templates/dashboard/discount/sale/list.html
+++ b/templates/dashboard/discount/sale/list.html
@@ -85,7 +85,7 @@
{% paginate sales %}
</div>
<div class="col s12 l3" id="filters">
- {% include 'dashboard/includes/_filters.html' %}
+ {% add_filters filter %}
</div>
</div>
{% endblock %}
diff --git a/templates/dashboard/discount/voucher/list.html b/templates/dashboard/discount/voucher/list.html
index b39dfabaf7..f3e8c149a6 100644
--- a/templates/dashboard/discount/voucher/list.html
+++ b/templates/dashboard/discount/voucher/list.html
@@ -135,7 +135,7 @@
{% paginate vouchers %}
</div>
<div class="col s12 l3" id="filters">
- {% include 'dashboard/includes/_filters.html' %}
+ {% add_filters filter %}
</div>
</div>
{% endblock %}
diff --git a/templates/dashboard/order/list.html b/templates/dashboard/order/list.html
index 7666346632..4e337cb296 100644
--- a/templates/dashboard/order/list.html
+++ b/templates/dashboard/order/list.html
@@ -35,7 +35,7 @@
{% endif %}
</div>
<div class="col s12 l3" id="filters">
- {% include "dashboard/includes/_filters.html" %}
+ {% add_filters filter %}
</div>
</div>
{% endblock %}
diff --git a/templates/dashboard/staff/list.html b/templates/dashboard/staff/list.html
index 4072e1bc8b..022ae591d7 100644
--- a/templates/dashboard/staff/list.html
+++ b/templates/dashboard/staff/list.html
@@ -94,7 +94,7 @@
{% paginate staff %}
</div>
<div class="col s12 l3" id="filters">
- {% include 'dashboard/includes/_filters.html' %}
+ {% add_filters filter %}
</div>
</div>
{% endblock %}