Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add filter to Orders
michal50c112951ff1
diff --git a/saleor/dashboard/discount/filters.py b/saleor/dashboard/discount/filters.pyindex 385a299f4a..8b760f3563 100644--- a/saleor/dashboard/discount/filters.py+++ b/saleor/dashboard/discount/filters.py@@ -1,8 +1,7 @@from __future__ import unicode_literals-from django_filters import (FilterSet, RangeFilter, OrderingFilter)+from django_filters import (FilterSet, OrderingFilter)from django.utils.translation import pgettext_lazy-from django_prices.models import PriceFieldfrom ...discount.models import Sale, Voucherdiff --git a/saleor/dashboard/order/filters.py b/saleor/dashboard/order/filters.pynew file mode 100644index 0000000000..058e2258b6--- /dev/null+++ b/saleor/dashboard/order/filters.py@@ -0,0 +1,30 @@+from __future__ import unicode_literals++from django_filters import (FilterSet, RangeFilter, OrderingFilter)+from django.utils.translation import pgettext_lazy+from django_prices.models import PriceField++from ...order.models import Order+++SORT_BY_FIELDS = {+ 'status': pgettext_lazy('Sale list sorting option', 'status'),+ 'created': pgettext_lazy('Sale list sorting option', 'created'),+ 'user_email': pgettext_lazy('Sale list sorting option', 'user_email')}+++class OrderFilter(FilterSet):+ sort_by = OrderingFilter(+ label=pgettext_lazy('Sale list sorting form', 'Sort by'),+ fields=SORT_BY_FIELDS.keys(),+ field_labels=SORT_BY_FIELDS+ )++ class Meta:+ model = Order+ fields = ['status', 'created', 'user_email', 'total_net']+ filter_overrides = {+ PriceField: {+ 'filter_class': RangeFilter+ }+ }diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 23f73a1156..097637ec58 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -298,10 +298,6 @@ PAYMENT_STATUS_CHOICES = [] + PaymentStatus.CHOICES-class OrderFilterForm(forms.Form):- status = forms.ChoiceField(choices=ORDER_STATUS_CHOICES)--class PaymentFilterForm(forms.Form):status = forms.ChoiceField(choices=PAYMENT_STATUS_CHOICES)diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex f5249357d2..b953e2c1df 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -14,6 +14,7 @@ from payments import PaymentStatusfrom prices import Pricefrom satchless.item import InsufficientStock+from .filters import OrderFilterfrom .forms import (AddVariantToDeliveryGroupForm, CancelGroupForm, CancelLinesForm,CancelOrderForm, CapturePaymentForm, ChangeStockForm, ChangeQuantityForm,@@ -22,7 +23,6 @@ from .forms import (from .utils import (create_packing_slip_pdf, create_invoice_pdf,get_statics_absolute_url)-from ..order.forms import OrderFilterFormfrom ..views import staff_member_requiredfrom ...core.utils import get_paginator_itemsfrom ...order import OrderStatus@@ -34,19 +34,13 @@ from ...userprofile.i18n import AddressForm@staff_member_required@permission_required('order.view_order')def order_list(request):- orders_all = Order.objects.prefetch_related(- 'groups', 'payments', 'groups__items', 'user')- status = request.GET.get('status')- if status:- orders = orders_all.filter(status=status)- else:- orders = orders_all.all()+ orders = (Order.objects.prefetch_related(+ 'groups', 'payments', 'groups__items', 'user').order_by('-pk'))+ order_filter = OrderFilter(request.GET, queryset=orders)orders = get_paginator_items(- orders, settings.DASHBOARD_PAGINATE_BY, request.GET.get('page'))- form = OrderFilterForm(- request.POST or None, initial={'status': status or None})- ctx = {'orders': orders,- 'form': form}+ order_filter.qs, settings.DASHBOARD_PAGINATE_BY,+ request.GET.get('page'))+ ctx = {'orders': orders, 'filter': order_filter}return TemplateResponse(request, 'dashboard/order/list.html', ctx)diff --git a/templates/dashboard/_filters.html b/templates/dashboard/_filters.htmldeleted file mode 100644index 49b91997ff..0000000000--- a/templates/dashboard/_filters.html+++ /dev/null@@ -1,39 +0,0 @@-{% load i18n %}-{% load materializecss %}--<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>- <a href="?">- <span class="clear-filters float-right">{% trans 'Clear filters' context 'Category page filters' %}</span>- </a>- <form method="get">- {% for field in filter.form %}- <div class="col s4">- {% if field.name == 'sort_by' %}- <!--Field 'sort_by' is hidden because it is rendered in header.- This is required if you want to have sorting to be kept during further filtering.-->- <input type="hidden" name="sort_by"- value="{% if request.GET.sort_by %}{{ request.GET.sort_by }}{% endif %}">- {% elif field.name != 'price' %}- {{ field|materializecss }}- {% endif %}- </div>- {% endfor %}- {% for field in filter.form %}- {% if field.name == 'price' %}- <div class="col s4">- {{ field|materializecss }}- </div>- {% endif %}- {% endfor %}- <div class="row">- <button class="btn primary" type="submit">{% trans 'Filter' context 'Category price filter' %}</button>- </div>- </form>- </div>- </div>- </div>-</div>diff --git a/templates/dashboard/discount/sale/list.html b/templates/dashboard/discount/sale/list.htmlindex 9c3eb3d6d2..d3680f159e 100644--- a/templates/dashboard/discount/sale/list.html+++ b/templates/dashboard/discount/sale/list.html@@ -24,7 +24,7 @@{% block menu_discounts_class %} active{% endblock %}{% block content %}- {% include './../../_filters.html' %}+ {% include 'dashboard/includes/_filters.html' %}{% if perms.discount.edit_sale %}<div class="fixed-action-btn"><a href="{% url 'dashboard:sale-add' %}" class="btn-fab btn-floating btn-large waves-effect waves-light"diff --git a/templates/dashboard/discount/voucher/list.html b/templates/dashboard/discount/voucher/list.htmlindex 2913644591..468911a2d2 100644--- a/templates/dashboard/discount/voucher/list.html+++ b/templates/dashboard/discount/voucher/list.html@@ -25,7 +25,7 @@{% block menu_discounts_class %} active{% endblock %}{% block content %}- {% include './../../_filters.html' %}+ {% include 'dashboard/includes/_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"diff --git a/templates/dashboard/includes/_filters.html b/templates/dashboard/includes/_filters.htmlnew file mode 100644index 0000000000..971d4da279--- /dev/null+++ b/templates/dashboard/includes/_filters.html@@ -0,0 +1,36 @@+{% load i18n %}+{% load materializecss %}+++<div class="card">+ <form method="get">+ <div class="card-content row">+ <span class="card-title">{% trans 'Filters' context 'Dashboard card title' %}</span>+ {% for field in filter.form %}+ <div class="col s4">+ {% if field.name == 'sort_by' %}+ <!--Field 'sort_by' is hidden because it is rendered in header.+ This is required if you want to have sorting to be kept during further filtering.-->+ <input type="hidden" name="sort_by"+ value="{% if request.GET.sort_by %}{{ request.GET.sort_by }}{% endif %}">+ {% elif field.name != 'price' %}+ {{ field|materializecss }}+ {% endif %}+ </div>+ {% endfor %}+ {% for field in filter.form %}+ {% if field.name == 'price' %}+ <div class="col s4">+ {{ field|materializecss }}+ </div>+ {% endif %}+ {% endfor %}+ <div class="row">+ <a href="?">+ <span class="clear-filters float-right">{% trans 'Clear filters' context 'Category page filters' %}</span>+ </a>+ <button class="btn primary" type="submit">{% trans 'Filter' context 'Category price filter' %}</button>+ </div>+ </div>+ </form>+</div>diff --git a/templates/dashboard/includes/_status_filters.html b/templates/dashboard/includes/_status_filters.htmldeleted file mode 100644index 76e2dcc4d7..0000000000--- a/templates/dashboard/includes/_status_filters.html+++ /dev/null@@ -1,15 +0,0 @@-{% load i18n %}-{% load materializecss %}--<div class="card">- <form method="get" novalidate>- <div class="card-content row">- {{ form|materializecss:"s12 m10" }}- <div class="col s12 m2">- <button type="submit" class="btn bwaves-effect waves-light btn--inline">- {% trans "Filter" context "Status filter action" %}- </button>- </div>- </div>- </form>-</div>diff --git a/templates/dashboard/order/list.html b/templates/dashboard/order/list.htmlindex 3b56d7afa4..5522bd08c0 100644--- a/templates/dashboard/order/list.html+++ b/templates/dashboard/order/list.html@@ -18,7 +18,7 @@{% block content %}{% if orders %}- {% include "dashboard/includes/_status_filters.html" %}+ {% include "dashboard/includes/_filters.html" %}{% if orders %}<div class="card">{% include "dashboard/includes/_orders_table.html" with orders=orders %}diff --git a/templates/dashboard/product/list.html b/templates/dashboard/product/list.htmlindex b4fe8b83c6..3367f0c7b9 100644--- a/templates/dashboard/product/list.html+++ b/templates/dashboard/product/list.html@@ -45,7 +45,7 @@{% block content %}{% if products %}- {% include './../_filters.html' %}+ {% include 'dashboard/includes/_filters.html' %}<div class="row"><div class="col s12"><div class="card">