Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add filters
michalc8b078025959
diff --git a/requirements.txt b/requirements.txtindex f8d2a551f0..f39524b0c6 100644--- a/requirements.txt+++ b/requirements.txt@@ -20,6 +20,7 @@ dj-email-url==0.0.10django-bootstrap3==9.0.0django-cache-url==1.3.1django-countries==4.6.1+django-filter==1.0.4django-mptt==0.8.7django-payments==0.11.0.3django-prices-openexchangerates==0.1.15diff --git a/saleor/product/filters.py b/saleor/product/filters.pynew file mode 100644index 0000000000..e67b476af4--- /dev/null+++ b/saleor/product/filters.py@@ -0,0 +1,34 @@+from django_filters import FilterSet, ModelMultipleChoiceFilter++from django.forms import CheckboxSelectMultiple++from .models import Category, Product, ProductAttribute, AttributeChoiceValue+++class CategoryFilter(FilterSet):+ class Meta:+ model = Category+ fields = ['name', 'parent']+++class ProductFilter(FilterSet):+ class Meta:+ model = Product+ fields = ['price', 'categories__name']+++class ProductAttributeFilter(FilterSet):+ def __init__(self, *args, **kwargs):+ self.attribute_name = kwargs.pop('attribute_name')+ super(ProductAttributeFilter, self).__init__(*args, **kwargs)+ self.filters['name'].queryset = \+ AttributeChoiceValue.objects.all().filter(+ attribute__name=self.attribute_name)++ name = ModelMultipleChoiceFilter(widget=CheckboxSelectMultiple)++ class Meta:+ model = ProductAttribute+ fields = ['name']++diff --git a/saleor/product/views.py b/saleor/product/views.pyindex 7bdc5b3971..540fc2375e 100644--- a/saleor/product/views.py+++ b/saleor/product/views.py@@ -4,13 +4,15 @@ import datetimeimport jsonfrom django.core.urlresolvers import reverse+from django.db.models import Prefetchfrom django.http import HttpResponsePermanentRedirect, JsonResponsefrom django.shortcuts import get_object_or_404, redirectfrom django.template.response import TemplateResponsefrom ..cart.utils import set_cart_cookiefrom ..core.utils import serialize_decimal-from .models import Category+from .filters import ProductAttributeFilter, CategoryFilter, ProductFilter+from .models import Category, Product, ProductAttribute, AttributeChoiceValuefrom .utils import (products_with_details, products_for_cart,handle_cart_form, get_availability,get_product_images, get_variant_picker_data,@@ -112,8 +114,31 @@ def product_add_to_cart(request, slug, product_id):def category_index(request, path, category_id):category = get_object_or_404(Category, id=category_id)actual_path = category.get_full_path()++ products = (Product.objects.all()+ .filter(categories__name=category)+ .prefetch_related(+ 'images', 'product_class__variant_attributes__values',+ 'product_class__product_attributes__values',+ 'variants__stock').all())++ filters = dict()+ for product in products:+ for attribute in product.product_class.variant_attributes.all():+ filters[attribute] = set()+ for attribute in product.product_class.product_attributes.all():+ filters[attribute] = set()++ for name, choices in filters.items():+ filters[name] = ProductAttributeFilter(+ request=request.GET,+ queryset=AttributeChoiceValue.objects.all(),+ attribute_name=name)+if actual_path != path:return redirect('product:category', permanent=True, path=actual_path,category_id=category_id)return TemplateResponse(request, 'category/index.html',- {'category': category})+ {'category': category,+ 'products': products,+ 'filters': filters})diff --git a/templates/category/index.html b/templates/category/index.htmlindex 67612d30a7..512e7b3763 100644--- a/templates/category/index.html+++ b/templates/category/index.html@@ -1,8 +1,11 @@{% extends "base.html" %}+{% load i18n %}{% load slice from shop %}{% load staticfiles %}{% load bootstrap_pagination from bootstrap3 %}{% load render_bundle from webpack_loader %}+{% load prices_i18n %}+{% load product_first_image from product_images %}{% block footer_scripts %}{{ block.super }}@@ -26,4 +29,112 @@{% block content %}+ <div class="category-page">+ <div class="category-top">+ <div class="row">+ <div class="col-md-7">+ <ul class="breadcrumbs list-unstyled d-none d-md-block">+ <li><a href="/">Home</a></li>+ {% if category.paren %}+ <li key=''><a href=''>{{ category.parent.name }}</a></li>+ {% endif %}+ <li><a href='{{ category.get_absolute_url }}'>{{ category.name }}</a></li>+ </ul>+ </div>+ <!--<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 d-sm-none" onClick={() => this.toggleMenu(filtersMenu)}>{pgettext('Category page filters', 'Filters')}</span>-->+ <!--{(variables.attributesFilter.length || variables.minPrice || variables.maxPrice) && (-->+ <!--<span class="filters-menu__icon d-sm-none"></span>-->+ <!--)}-->+ <!--</div>-->+ <!--<div class="col-6 col-md-10 col-lg-6">-->+ <!--<SortBy sortedValue={variables.sortBy} setSorting={this.setSorting} />-->+ <!--</div>-->+ <!--</div>-->+ <!--</div>-->+ </div>+ </div>+ <div class="row">+ <div class="col-md-4 col-lg-3">+ <div class="product-filters">+ {% for name, filter in filters.items %}+ <form method="get">+ {{ name }}+ {{ filter.form.as_p }}+ <button type="submit">Search</button>+ </form>+ {% endfor %}+ </div>+ <div>+ <h2>+ {% trans 'Filters' context 'Filter heading title' %}+ <span class="clear-filters float-right" onClick="">{% trans 'Clear filters' context 'Category page filters' %}</span>+ </h2>+ <div class="product-filters">+ <div class="product-filters__attributes">+ <div key={{ attribute.pk }}>+ <h3 onClick="">+ {{ title }}+ <div class="collapse-filters-icon">+ <InlineSVG key={key} src={imageSrc} />+ </div>+ </h3>+ <ul id={{ attribute.slug }}>+ <li key={value.id} class="item">++ </li>+ </ul>+ </div>+ </div>+ <PriceFilter+ onFilterChanged={this.updatePriceFilter}+ maxPrice={variables.maxPrice}+ minPrice={variables.minPrice}+ />+ </div>+ </div>+ </div>+ <div class="col-md-8 col-lg-9 category-list">+ <div>+ <!--ProductList-->+ {% for product in products %}+ <div class="row">++ <!--ProductItem-->+ <div class="col-6 col-md-4 product-list">+ <a href="{{ product.get_absolute_url }}">+ <div class="text-center">+ <div>+ <img class="img-responsive" src="{% product_first_image product size='60x60' method='crop' %}"+ srcset="{% product_first_image product size='120x120' method='crop' %} 1x, {% product_first_image product size='180x180' method='crop' %} 2x"+ alt="" />+ <span class="product-list-item-name" title={{ product.name }}>{{ product.name }}</span>+ </div>+ <div class="panel-footer">+ <div>+ <span>+ {% if product.get_price_range and product.get_gross_price_range|first != product.get_gross_price_range|last %}+ <span>FROM </span>{% gross product.price html=True %}+ {% endif %}+ </span>++ {% if product %}+ <div class="product-list__sale">+ <InlineSVG src={SaleImg} />+ <span class="">Sale</span>+ </div>+ {% endif %}+ </div>+ </div>+ </div>+ </a>+ </div>+ </div>+ {% endfor %}+ </div>+ </div>+ </div>+ </div>{% endblock content %}