Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add filters to voucher list
michal02b103ddc66b
diff --git a/saleor/dashboard/discount/filters.py b/saleor/dashboard/discount/filters.pyindex 755083a701..080f2df99a 100644--- a/saleor/dashboard/discount/filters.py+++ b/saleor/dashboard/discount/filters.py@@ -4,26 +4,44 @@ from django_filters import (FilterSet, RangeFilter, OrderingFilter)from django.utils.translation import pgettext_lazyfrom django_prices.models import PriceField-from ...discount.models import Sale+from ...discount.models import Sale, Voucher-SORT_BY_FIELDS = {- 'name': pgettext_lazy('Product list sorting option', 'name'),- 'value': pgettext_lazy('Product list sorting option', 'value')}+SORT_BY_FIELDS_SALE = {+ 'name': pgettext_lazy('Sale list sorting option', 'name'),+ 'value': pgettext_lazy('Sale list sorting option', 'value')}++SORT_BY_FIELDS_VOUCHER = {+ 'name': pgettext_lazy('Voucher list sorting option', 'name'),+ 'discount_value': pgettext_lazy(+ 'Voucher list sorting option', 'discount_value'),+ 'apply_to': pgettext_lazy('Voucher list sorting option', 'apply_to'),+ 'start_date': pgettext_lazy('Voucher list sorting option', 'start_date'),+ 'end_date': pgettext_lazy('Voucher list sorting option', 'end_date'),+ 'used': pgettext_lazy('Voucher list sorting option', 'used'),+ 'limit': pgettext_lazy('Voucher list sorting option', 'limit')}class SaleFilter(FilterSet):sort_by = OrderingFilter(- label=pgettext_lazy('Product list sorting form', 'Sort by'),- fields=SORT_BY_FIELDS.keys(),- field_labels=SORT_BY_FIELDS+ label=pgettext_lazy('Sale list sorting form', 'Sort by'),+ fields=SORT_BY_FIELDS_SALE.keys(),+ field_labels=SORT_BY_FIELDS_SALE)class Meta:model = Salefields = ['categories', 'type', 'value']- filter_overrides = {- PriceField: {- 'filter_class': RangeFilter- }- }+++class VoucherFilter(FilterSet):+ sort_by = OrderingFilter(+ label=pgettext_lazy('Voucher list sorting form', 'Sort by'),+ fields=SORT_BY_FIELDS_VOUCHER.keys(),+ field_labels=SORT_BY_FIELDS_VOUCHER+ )++ class Meta:+ model = Voucher+ fields = ['name', 'discount_value_type', 'discount_value', 'apply_to',+ 'start_date', 'end_date', 'used', 'limit']diff --git a/saleor/dashboard/discount/views.py b/saleor/dashboard/discount/views.pyindex 56d2e050a1..87a2d9a6ed 100644--- a/saleor/dashboard/discount/views.py+++ b/saleor/dashboard/discount/views.py@@ -11,7 +11,8 @@ from ...core.utils import get_paginator_itemsfrom ...core.utils.filters import get_now_sorted_byfrom ...discount.models import Sale, Voucherfrom ..views import staff_member_required-from .filters import SaleFilter, SORT_BY_FIELDS+from .filters import (+ SaleFilter, VoucherFilter, SORT_BY_FIELDS_SALE, SORT_BY_FIELDS_VOUCHER)from . import forms@@ -22,7 +23,7 @@ def sale_list(request):sale_filter = SaleFilter(request.GET, queryset=sales)sales = get_paginator_items(sale_filter.qs, settings.DASHBOARD_PAGINATE_BY, request.GET.get('page'))- now_sorted_by = get_now_sorted_by(sale_filter, SORT_BY_FIELDS)+ now_sorted_by = get_now_sorted_by(sale_filter, SORT_BY_FIELDS_VOUCHER)arg_sort_by = request.GET.get('sort_by')is_descending = arg_sort_by.startswith('-') if arg_sort_by else Falsectx = {@@ -71,9 +72,14 @@ def sale_delete(request, pk):def voucher_list(request):vouchers = (Voucher.objects.select_related('product', 'category').order_by('name'))+ voucher_filter = VoucherFilter(request.GET, queryset=vouchers)vouchers = get_paginator_items(- vouchers, settings.DASHBOARD_PAGINATE_BY, request.GET.get('page'))- ctx = {'vouchers': vouchers}+ voucher_filter.qs, DASHBOARD_PAGINATE_BY, request.GET.get('page'))+ now_sorted_by = get_now_sorted_by(voucher_filter, SORT_BY_FIELDS_VOUCHER)+ arg_sort_by = request.GET.get('sort_by')+ is_descending = arg_sort_by.startswith('-') if arg_sort_by else False++ ctx = {'vouchers': vouchers, 'filter': voucher_filter}return TemplateResponse(request, 'dashboard/discount/voucher/list.html', ctx)diff --git a/templates/dashboard/discount/voucher/list.html b/templates/dashboard/discount/voucher/list.htmlindex 838ea6a5bd..2913644591 100644--- a/templates/dashboard/discount/voucher/list.html+++ b/templates/dashboard/discount/voucher/list.html@@ -1,6 +1,7 @@{% extends "dashboard/base.html" %}{% load i18n %}{% load prices_i18n %}+{% load shop %}{% load materializecss %}{% load utils %}{% load static %}@@ -24,6 +25,7 @@{% block menu_discounts_class %} active{% endblock %}{% block content %}+ {% include './../../_filters.html' %}{% if perms.discount.edit_voucher %}<div class="fixed-action-btn"><a href="{% url 'dashboard:voucher-add' %}" class="btn-fab btn-floating btn-large waves-effect waves-light"@@ -39,25 +41,39 @@<thead><tr><th class="wide">- {% trans "Name" context "Voucher table header" %}+ <a href="{% get_sort_by_url_toggle 'name' %}">+ {% trans "Name" context "Voucher table header" %}+ </a></th><th class="right-align">- {% trans "Discount" context "Voucher table header" %}+ <a href="{% get_sort_by_url_toggle 'discount_value' %}">+ {% trans "Discount" context "Voucher table header" %}+ </a></th><th>- {% trans "Apply to" context "Voucher table header" %}+ <a href="{% get_sort_by_url_toggle 'apply_to' %}">+ {% trans "Apply to" context "Voucher table header" %}+ </a></th><th>- {% trans "Start date" context "Voucher table header" %}+ <a href="{% get_sort_by_url_toggle 'start_date' %}">+ {% trans "Start date" context "Voucher table header" %}+ </a></th><th>- {% trans "End date" context "Voucher table header" %}+ <a href="{% get_sort_by_url_toggle 'end_date' %}">+ {% trans "End date" context "Voucher table header" %}+ </a></th><th class="right-align">- {% trans "Used" context "Voucher table header" %}+ <a href="{% get_sort_by_url_toggle 'used' %}">+ {% trans "Used" context "Voucher table header" %}+ </a></th><th class="right-align">- {% trans "Limit" context "Voucher table header" %}+ <a href="{% get_sort_by_url_toggle 'limit' %}">+ {% trans "Limit" context "Voucher table header" %}+ </a></th></tr></thead>