Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add standard_form to ProductFilter
michal9a1d52f14efc
diff --git a/saleor/product/filters.py b/saleor/product/filters.pyindex fbc5fafe72..390709b338 100644--- a/saleor/product/filters.py+++ b/saleor/product/filters.py@@ -1,5 +1,6 @@from __future__ import unicode_literalsfrom collections import OrderedDict+from copy import deepcopyfrom django_filters import FilterSet, MultipleChoiceFilter, RangeFilter, \OrderingFilter@@ -7,7 +8,7 @@ from django.forms import CheckboxSelectMultiplefrom django_prices.models import PriceField-from .models import Product, AttributeChoiceValue+from .models import Productclass ProductFilter(FilterSet):@@ -51,6 +52,20 @@ class ProductFilter(FilterSet):}}+ @property+ def standard_form(self):+ """+ This method returns only those filters that ware dynamically generated+ in __init__().+ In this case 'price' and 'sort_by' are rendered in template differently+ than rest of the filters.+ :return:+ """+ standard_form = deepcopy(self.form)+ del standard_form.fields['price']+ del standard_form.fields['sort_by']+ return standard_form+def get_attribute_choices(attribute):return [(choice.pk, choice.name) for choice in attribute.values.all()]diff --git a/saleor/product/views.py b/saleor/product/views.pyindex 3ded0cbd15..db0ed1dbb6 100644--- a/saleor/product/views.py+++ b/saleor/product/views.py@@ -11,7 +11,7 @@ from django.template.response import TemplateResponsefrom ..cart.utils import set_cart_cookiefrom ..core.utils import serialize_decimalfrom .filters import ProductFilter-from .models import Category, Product+from .models import Categoryfrom .utils import (products_with_details, products_for_cart,handle_cart_form, get_availability,get_product_images, get_variant_picker_data,@@ -116,6 +116,7 @@ def category_index(request, path, category_id):products = products_with_details(user=request.user).filter(categories__name=category)product_filter = ProductFilter(request.GET, queryset=products)+products = [(product, get_availability(product, request.discounts, request.currency))for product in product_filter.qs]diff --git a/templates/category/index.html b/templates/category/index.htmlindex cbacd7b6bb..426b700aa9 100644--- a/templates/category/index.html+++ b/templates/category/index.html@@ -42,8 +42,7 @@<div class="col-md-5"><div class="row"><div class="col-6 col-md-2 col-lg-6 filters-menu">- <span class="filters-menu__label hidden-sm-up">Filters</span>- <span class="filters-menu__icon hidden-sm-up"></span>+</div><div class="col-6 col-md-10 col-lg-6"><div class="sort-by">@@ -96,35 +95,31 @@<div class="product-filters"><div class="product-filters__attributes" data-icon-up="{% static "assets/chevron_up.svg" %}" data-icon-down="{% static "assets/chevron_down.svg" %}"><form method="get">- {% for field in filter.form %}- {% if field.label == 'Price' %}- <div class="toggle-filter product-filters__price-range">- <h3 class="filter-label">- {{ field.label }}- <div class="collapse-filters-icon">- <img src="{% static "assets/chevron_up.svg" %}">- </div>- </h3>- <div class="filter-form-field" style="display:block">- {{ field }}+ {% for field in filter.standard_form %}+ <div class="toggle-filter">+ <h3 class="filter-label">+ {{ field.label }}+ <div class="collapse-filters-icon">+ <img src="{% static "assets/chevron_up.svg" %}"></div>+ </h3>+ <div class="filter-form-field" style="display:block">+ {{ field }}</div>- {% elif field.label == 'Sort by' %}- {% else %}- <div class="toggle-filter">- <h3 class="filter-label">- {{ field.label }}- <div class="collapse-filters-icon">- <img src="{% static "assets/chevron_up.svg" %}">- </div>- </h3>- <div class="filter-form-field" style="display:block">- {{ field }}+ </div>+ {% endfor %}+ <div class="toggle-filter product-filters__price-range">+ <h3 class="filter-label">+ {{ filter.form.fields.price.label }}+ <div class="collapse-filters-icon">+ <img src="{% static "assets/chevron_up.svg" %}"></div>+ </h3>+ <div class="filter-form-field" style="display:block">+ <input id="price_0" name="price_0" type="number" min="0" class="form-control" placeholder="from"><span>-</span><input id="price_1" name="price_1" type="number" min="0" class="form-control" placeholder="to">+ <button class="btn primary" type="submit">Update</button></div>- {% endif %}- {% endfor %}- <button class="btn primary" type="submit">Search</button>+ </div></form></div></div>