Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add chip factory
dominik-zeglenaa9075c7127a
diff --git a/saleor/dashboard/product/chips.py b/saleor/dashboard/product/chips.pynew file mode 100644index 0000000000..0f652d1fc3--- /dev/null+++ b/saleor/dashboard/product/chips.py@@ -0,0 +1,72 @@+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': field.name, '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+ self.handlers = {**DEFAULT_HANDLERS, **handlers}++ def make(self):+ for field in self.form:+ print(str(type(field.field)))+ if field.value() is not 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.chipsdiff --git a/saleor/dashboard/product/filters.py b/saleor/dashboard/product/filters.pyindex 5acc95235f..b3ba152a38 100644--- a/saleor/dashboard/product/filters.py+++ b/saleor/dashboard/product/filters.py@@ -3,7 +3,9 @@ from __future__ import unicode_literalsfrom django_filters import (CharFilter, FilterSet, RangeFilter, OrderingFilter)from django.utils.translation import pgettext_lazyfrom django_prices.models import PriceField+from django.urls import reverse+from .chips import ChipFactoryfrom ...product.models import Category, Product@@ -32,24 +34,10 @@ class ProductFilter(FilterSet):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+ 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/static/dashboard/scss/layouts/_products.scss b/saleor/static/dashboard/scss/layouts/_products.scssindex a1db5c4ae1..ca559f4112 100644--- a/saleor/static/dashboard/scss/layouts/_products.scss+++ b/saleor/static/dashboard/scss/layouts/_products.scss@@ -9,3 +9,10 @@}}}++.chips-container {+ margin-top: 18px;+ a {+ color: #999;+ }+}diff --git a/templates/dashboard/includes/_filters.html b/templates/dashboard/includes/_filters.htmlindex 9be4bda8a4..c0af53b6c0 100644--- a/templates/dashboard/includes/_filters.html+++ b/templates/dashboard/includes/_filters.html@@ -11,9 +11,9 @@<span>{% trans 'Total records found' context 'Dashboard filter' %}: {{ filter.qs|length }}</span><span class="collapse-activate hide-on-large-only"><svg data-src="{% static '/images/filter_icon.svg' %}" /></span></div>- <div class="col s12 hide-on-large-only">+ <div class="col s12 hide-on-large-only chips-container">{% for chip in chips %}- <div class="chip">{{ chip }}</div>+ <div class="chip">{{ chip.content|safe }} <a class="js-build-query" data-name="{{ chip.name }}" data-value="{{ chip.value }}">×</a></div>{% endfor %}</div><div class="clearfix"></div>diff --git a/templates/dashboard/product/list.html b/templates/dashboard/product/list.htmlindex f830fb8503..564acf54cd 100644--- a/templates/dashboard/product/list.html+++ b/templates/dashboard/product/list.html@@ -139,7 +139,6 @@</div><div class="col s12 l3" id="product-filters">{% add_filters filter %}- {#{% include 'dashboard/includes/_filters.html' %}#}</div></div><div id="modal-product-class" class="modal">