Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add filters for product list in dashboard
michal492b2af15a6b
diff --git a/saleor/dashboard/product/filters.py b/saleor/dashboard/product/filters.pynew file mode 100644index 0000000000..64ba3fd6b2--- /dev/null+++ b/saleor/dashboard/product/filters.py@@ -0,0 +1,30 @@+from __future__ import unicode_literals++from django_filters import (FilterSet, MultipleChoiceFilter, RangeFilter,+ OrderingFilter)+from django.utils.translation import pgettext_lazy+from django_prices.models import PriceField++from ...product.models import Product, ProductAttribute+++SORT_BY_FIELDS = {'name': pgettext_lazy('Product list sorting option', 'name'),+ 'price': pgettext_lazy(+ 'Product list sorting option', 'price')}+++class ProductFilter(FilterSet):+ sort_by = OrderingFilter(+ label=pgettext_lazy('Product list sorting form', 'Sort by'),+ fields=SORT_BY_FIELDS.keys(),+ field_labels=SORT_BY_FIELDS+ )++ class Meta:+ model = Product+ fields = ['categories', 'is_published', 'is_featured', 'price']+ filter_overrides = {+ PriceField: {+ 'filter_class': RangeFilter+ }+ }diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex 9a511d180f..41afd84946 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -17,8 +17,14 @@ from ...product.models import (ProductImage, ProductVariant, Stock, StockLocation)from ...product.utils import (get_availability, get_product_costs_data, get_variant_costs_data)+<<<<<<< HEAD+=======+from ...product.filters import get_now_sorted_by+from ...settings import DASHBOARD_PAGINATE_BY+>>>>>>> Add filters for product list in dashboardfrom ..views import staff_member_requiredfrom . import forms+from . import filters@staff_member_required@@ -113,11 +119,21 @@ def product_list(request):if form.is_valid():return redirect('dashboard:product-add', class_pk=form.cleaned_data['product_cls'])++ product_filter = filters.ProductFilter(request.GET, queryset=products)+products = get_paginator_items(- products, settings.DASHBOARD_PAGINATE_BY, request.GET.get('page'))+ product_filter.qs, DASHBOARD_PAGINATE_BY, request.GET.get('page'))++ now_sorted_by = get_now_sorted_by(product_filter)+ arg_sort_by = request.GET.get('sort_by')+ is_descending = arg_sort_by.startswith('-') if arg_sort_by else False+ctx = {'bulk_action_form': forms.ProductBulkUpdate(), 'form': form,- 'products': products, 'product_classes': product_classes}+ 'products': products, 'product_classes': product_classes,+ 'filter': product_filter,+ 'now_sorted_by': now_sorted_by, 'is_descending': is_descending}return TemplateResponse(request, 'dashboard/product/list.html', ctx)diff --git a/templates/dashboard/product/list.html b/templates/dashboard/product/list.htmlindex 6e469dfc63..a07066dffd 100644--- a/templates/dashboard/product/list.html+++ b/templates/dashboard/product/list.html@@ -43,21 +43,35 @@{% block menu_catalogue_class %} active{% endblock %}{% block content %}- <div class="row">- <div class="col s12">- {% if products %}- <div class="card">- <form method="POST"- action="{% url "dashboard:product-bulk-update" %}"- novalidate- id="bulk-actions-form">- {% csrf_token %}- <input name="action" type="hidden" id="bulk-action" />- <nav class="data-table-header-alternative single">- <div class="bulk-info show-bulk">+ {% if products %}+ <div class="row">+ <div class="col s12">+ <div class="card">+ <div class="card-content">+ <span class="card-title">{% trans 'Filters' context 'Dashboard card title' %}</span>+ <form method="get">+ {% for field in filter.form %}+ <div class="col s4">+ {{ field|materializecss }}+ </div>+ {% endfor %}+ <button class="btn primary" type="submit">{% trans 'Update' context 'Category price filter' %}</button>+ </form>+ </div>+ </div>+ </div>+ </div>+ <div class="row">+ <div class="col s12">+ <div class="card">+ <form method="POST" action="{% url 'dashboard:product-bulk-update' %}" novalidate id="bulk-actions-form">+ {% csrf_token %}+ <input name="action" type="hidden" id="bulk-action" />+ <nav class="data-table-header-alternative single">+ <div class="bulk-info show-bulk"><div class="data-table-header-action-selected-items col inline"></div></div>- <div class="bulk-actions show-bulk nav-wrapper">+ <div class="bulk-actions show-bulk nav-wrapper"><ul class="right">{% for action, label in bulk_action_form.action.field.widget.choices %}<li>@@ -66,9 +80,9 @@{% endfor %}</ul></div>- </nav>- <div class="data-table-container">- <table class="bordered highlight responsive data-table">+ </nav>+ <div class="data-table-container">+ <table class="bordered highlight responsive data-table"><thead><tr><th class="bulk-checkbox">@@ -106,11 +120,15 @@{% endfor %}</tbody></table>- </div>- </form>+ </div>+ </form></div>{% paginate products %}- {% else %}+ </div>+ </div>+ {% else %}+ <div class="row">+ <div class="col s12"><div class="not-found"><p class="grey-text">{% trans "No products found." context "Empty product list message" %}@@ -121,9 +139,9 @@</p>{% endif %}</div>- {% endif %}+ </div></div>- </div>+ {% endif %}<div id="modal-product-class" class="modal"><form method="post" novalidate><div class="modal-content">