Skip to content

Saleor Core: the high performance, composable, headless commerce API.

WIP: Add collection view with associated filtering

akjanik5fc479dc5746
diff --git a/saleor/product/filters.py b/saleor/product/filters.py
index d992157f4f..b82efed4c6 100644
--- a/saleor/product/filters.py
+++ b/saleor/product/filters.py
@@ -25,7 +25,7 @@ class ProductFilter(SortedFilterSet):
filter_overrides = {PriceField: {'filter_class': RangeFilter}}
def __init__(self, *args, **kwargs):
- self.category = kwargs.pop('category')
+ self.category = kwargs.pop('category', None)
super().__init__(*args, **kwargs)
self.product_attributes, self.variant_attributes = (
self._get_attributes())
@@ -77,3 +77,24 @@ class ProductFilter(SortedFilterSet):
'Validation error for sort_by filter',
'%(value)s is not a valid sorting option'),
params={'value': value})
+
+
+class ProductCollectionFilter(ProductFilter):
+
+ def __init__(self, *args, **kwargs):
+ self.collection = kwargs.pop('collection', None)
+ super().__init__(*args, **kwargs)
+
+ def _get_attributes(self):
+ product_attributes = (
+ ProductAttribute.objects.all()
+ .prefetch_related('values')
+ .filter(product_types__products__collections=self.collection)
+ .distinct())
+ variant_attributes = (
+ ProductAttribute.objects.all()
+ .prefetch_related('values')
+ .filter(
+ product_variant_types__products__collections=self.collection)
+ .distinct())
+ return product_attributes, variant_attributes
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 702b73a56b..7961945b0e 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -396,4 +396,5 @@ class Collection(models.Model):
def get_absolute_url(self):
return reverse(
'product:collection',
- kwargs={'slug': self.slug, 'collection_id': self.id})
+ kwargs={'pk': self.id})
+
diff --git a/saleor/product/urls.py b/saleor/product/urls.py
index d8b68e6dc9..1408106392 100644
--- a/saleor/product/urls.py
+++ b/saleor/product/urls.py
@@ -9,4 +9,6 @@ urlpatterns = [
views.category_index, name='category'),
url(r'(?P<slug>[a-z0-9-_]+?)-(?P<product_id>[0-9]+)/add/$',
views.product_add_to_cart, name="add-to-cart"),
-]
+ url(r'^collection/(?P<pk>[0-9]+)/$',
+ views.collection_index, name='collection')]
+
diff --git a/saleor/product/views.py b/saleor/product/views.py
index 3928cf9d8d..3f2f99a061 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -10,8 +10,8 @@ from django.urls import reverse
from ..cart.utils import set_cart_cookie
from ..core.utils import get_paginator_items, serialize_decimal
from ..core.utils.filters import get_now_sorted_by
-from .filters import SORT_BY_FIELDS, ProductFilter
-from .models import Category
+from .filters import ProductFilter, ProductCollectionFilter, SORT_BY_FIELDS
+from .models import Category, Collection
from .utils import (
get_availability, get_product_attributes_data, get_product_images,
get_variant_picker_data, handle_cart_form, product_json_ld,
@@ -132,3 +132,25 @@ def category_index(request, path, category_id):
'now_sorted_by': now_sorted_by,
'is_descending': is_descending}
return TemplateResponse(request, 'category/index.html', ctx)
+
+
+def collection_index(request, pk):
+ collection = get_object_or_404(Collection, id=pk)
+ products = products_with_details(user=request.user).filter(
+ collections__id=collection.id).order_by('name')
+ product_filter = ProductCollectionFilter(
+ request.GET, queryset=products, collection=collection)
+ products_paginated = get_paginator_items(
+ product_filter.qs, settings.PAGINATE_BY, request.GET.get('page'))
+ products_and_availability = list(products_with_availability(
+ products_paginated, request.discounts, request.currency))
+ 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 = {'collection': collection, 'filter_set': product_filter,
+ 'products': products_and_availability,
+ 'products_paginated': products_paginated,
+ 'sort_by_choices': SORT_BY_FIELDS,
+ 'now_sorted_by': now_sorted_by,
+ 'is_descending': is_descending}
+ return TemplateResponse(request, 'collection/index.html', ctx)
diff --git a/templates/collection/_items.html b/templates/collection/_items.html
new file mode 100644
index 0000000000..549d6ac251
--- /dev/null
+++ b/templates/collection/_items.html
@@ -0,0 +1,40 @@
+{% load i18n %}
+{% load staticfiles %}
+{% load price_range from price_ranges %}
+{% load product_first_image from product_images %}
+{% load get_thumbnail from product_images %}
+
+{% for product, availability in products %}
+<div class="col-6 col-lg-3 product-list">
+ <a href="{{ product.get_absolute_url }}" class="link--clean">
+ <div class="text-center">
+ <div>
+ <img class="img-responsive"
+ src="{% product_first_image product method="crop" size="255x255" %}"
+ srcset="{% product_first_image product method="crop" size="255x255" %} 1x, {% product_first_image product method="crop" size="510x510" %} 2x"
+ alt="">
+ <span class="product-list-item-name" title="{{ product }}">{{ product }}</span>
+ </div>
+
+ <div class="panel-footer">
+ {% if availability.available %}
+ {% price_range availability.price_range %}
+ {% if availability.discount %}
+ {% if availability.price_range_undiscounted.min_price != availability.price_range.min_price %}
+ <div class="product-list__sale">
+ <svg data-src="{% static "images/sale-bg.svg" %}" />
+ <span class="product-list__sale__text">
+ {% comment %}Translators: Layout may break if character length is different than four.{% endcomment %}
+ {% trans "Sale" context "Sale (discount) label for item in product list" %}
+ </span>
+ </div>
+ {% endif %}
+ {% endif %}
+ {% else %}
+ &nbsp;
+ {% endif %}
+ </div>
+ </div>
+ </a>
+</div>
+{% endfor %}
diff --git a/templates/collection/_list.html b/templates/collection/_list.html
new file mode 100644
index 0000000000..20d36c4a27
--- /dev/null
+++ b/templates/collection/_list.html
@@ -0,0 +1,12 @@
+{#{% load mptt_tags %}#}
+{##}
+{#{% recursetree collections %}#}
+{# <li>#}
+{# <a href="{{ node.get_absolute_url }}">{{ node }}</a>#}
+{# {% if not node.is_leaf_node %}#}
+{# <ul class="children">#}
+{# {{ children }}#}
+{# </ul>#}
+{# {% endif %}#}
+{# </li>#}
+{#{% endrecursetree %}#}
diff --git a/templates/collection/index.html b/templates/collection/index.html
new file mode 100644
index 0000000000..1716fb7db8
--- /dev/null
+++ b/templates/collection/index.html
@@ -0,0 +1,226 @@
+{% extends "base.html" %}
+{% load bootstrap_pagination from bootstrap4 %}
+{% load i18n %}
+{% load shop %}
+{% load staticfiles %}
+{% load prices_i18n %}
+{% load build_absolute_uri from urls %}
+
+{% block footer_scripts %}
+ {{ block.super }}
+{% endblock footer_scripts %}
+
+{% block title %}{{ collection }} — {{ block.super }}{% endblock %}
+
+{% block meta_tags %}
+ <meta property="og:title" content="{{ collection }}">
+ <meta property="og:description" content="{{ collection.description }}">
+ <meta property="og:image" content="{% static "images/block1.png" %}">
+
+ {% build_absolute_uri request=request location=collection.get_absolute_url as collection_url %}
+ <meta property="og:url" content="{{ collection_url }}">
+ <link rel="canonical" href="{{ collection_url }}?page={{ products_paginated.number }}">
+
+ {% if products_paginated.has_previous %}
+ <link rel="prev" href="{{ collection_url }}?page={{ products_paginated.previous_page_number }}">
+ {% endif %}
+
+ {% if products_paginated.has_next %}
+ <link rel="next" href="{{ collection_url }}?page={{ products_paginated.next_page_number }}">
+ {% endif %}
+{% endblock meta_tags %}
+
+{% block breadcrumb %}
+ {{ block.super }}
+ {% for breadcrumb in breadcrumbs %}
+ <li{% if forloop.last %} class="active"{% endif %}>
+ {% if not forloop.last %}
+ <a href="{{ breadcrumb.get_absolute_url }}">{{ breadcrumb }}</a>
+ {% else %}
+ <span>{{ breadcrumb }}</span>
+ {% endif %}
+ </li>
+ {% endfor %}
+{% endblock breadcrumb %}
+
+{% block content %}
+ <div id="collection-page">
+ <div class="collection-top">
+ <div class="row">
+ <div class="col-md-7">
+ <ul class="breadcrumbs list-unstyled d-none d-md-block">
+ <li><a href="{% url 'home' %}">{% trans "Home" context 'collection breadcrumbs home' %}</a></li>
+ {% for ancestor in collection.get_ancestors %}
+ <li><a href='{{ ancestor.get_absolute_url }}'>{{ ancestor.name }}</a></li>
+ {% endfor %}
+ <li><a href='{{ collection.get_absolute_url }}'>{{ collection.name }}</a></li>
+ </ul>
+ </div>
+ <div class="col-md-5 filters-menu__wrapper">
+ <div class="row">
+ <div class="col-6 col-md-2 col-lg-6 filters-menu">
+ <span class="filters-menu__label d-md-none">Filters</span>
+ </div>
+ <div class="col-6 col-md-10 col-lg-6">
+ <div class="sort-by">
+ <div class="click-area d-none"></div>
+ <button class="btn btn-link">
+ <div>
+ <span>
+ {% if now_sorted_by == 'name' %}
+ {% blocktrans trimmed %}
+ Sort by: <b>name</b>
+ {% endblocktrans %}
+ {% elif now_sorted_by == 'price' %}
+ {% blocktrans trimmed %}
+ Sort by: <b>price</b>
+ {% endblocktrans %}
+ {% endif %}
+ </span>
+ <div class="sort-order-icon">
+ {% if is_descending %}
+ <svg data-src="{% static "images/arrow-down.svg" %}"/>
+ {% else %}
+ <svg data-src="{% static "images/arrow-up.svg" %}"/>
+ {% endif %}
+ </div>
+ </div>
+ </button>
+ <ul class="sort-list d-none">
+ {% for choice, label in sort_by_choices.items %}
+ <li>
+ <div class="row">
+ <div class="col-6">
+ {% trans 'Sort by' context 'collection page filters' %}: <strong>{{ label }}</strong>
+ </div>
+ <div class="col-6">
+ <div>
+ <a href="{% get_sort_by_url choice %}">
+ <span>{% trans 'ascending' context 'sort by option' %}</span>
+ <div class="sort-order-icon float-right">
+ <img src="{% static "images/arrow-up.svg" %}">
+ </div>
+ </a>
+ <a href="{% get_sort_by_url choice descending=True %}">
+ <span>{% trans 'descending' context 'sort by option' %}</span>
+ <div class="sort-order-icon float-right">
+ <img src="{% static "images/arrow-down.svg" %}">
+ </div>
+ </a>
+ </div>
+ </div>
+ </div>
+ </li>
+ {% endfor %}
+ </ul>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div class="row">
+ <div class="col-md-4 col-lg-3">
+ <div class="product-filters">
+ <div class="product-filters__collections">
+ <h2>
+ <strong>{{ collection.name }}</strong>
+ </h2>
+ <ul class="product-filters__collections__childs no-parent">
+ {% for child in collection.get_children %}
+ <li>
+ <a href="{{ child.get_absolute_url }}">{{ child.name }}</a>
+ </li>
+ {% endfor %}
+ </ul>
+ </div>
+ </div>
+ <div class="filters-menu__body d-none d-md-block">
+ <h2>
+ {% trans 'Filters' context 'Filter heading title' %}
+ {% if filter.is_bound_unsorted %}
+ <a href=".">
+ <span class="clear-filters float-right">{% trans 'Clear filters' context 'collection page filters' %}</span>
+ </a>
+ {% endif %}
+ </h2>
+ <div class="product-filters">
+ <div class="product-filters__attributes" data-icon-up="{% static "images/chevron-up.svg" %}" data-icon-down="{% static "images/chevron-down.svg" %}">
+ <form method="get">
+ {% for field in filter_set.form %}
+ {% if field.name == 'sort_by' %}
+ {% comment %}
+ 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.
+ {% endcomment %}
+ <input type="hidden" name="sort_by" value="{% if request.GET.sort_by %}{{ request.GET.sort_by }}{% endif %}">
+ {% elif field.name == 'price' %}
+ <div class="toggle-filter product-filters__price-range">
+ <h3 class="filter-label">
+ {{ field.label }}
+ <div class="collapse-filters-icon">
+ <img src="{% static "images/chevron-up.svg" %}">
+ </div>
+ </h3>
+ <div class="filter-form-field" style="display:block">
+ <div class="mb-3">
+ <input id="{{ field.auto_id }}_0" name="{{ field.name }}_0"
+ value="{% if field.value.0 %}{{ field.value.0 }}{% endif %}" type="number" min="0"
+ class="form-control d-inline"
+ placeholder="{% trans 'from' context 'collection price filter' %}"><span>-</span><input id="{{ field.auto_id }}_1" name="{{ field.name }}_1"
+ value="{% if field.value.1 %}{{ field.value.1 }}{% endif %}" type="number" min="0"
+ class="form-control d-inline" placeholder="{% trans 'to' context 'collection price filter' %}">
+ </div>
+ </div>
+ </div>
+ {% else %}
+ <div class="toggle-filter">
+ <h3 class="filter-label">
+ {{ field.label }}
+ <div class="collapse-filters-icon">
+ <img src="{% static "images/chevron-up.svg" %}">
+ </div>
+ </h3>
+ <div class="filter-form-field" style="display:block">
+ {{ field }}
+ </div>
+ </div>
+ {% endif %}
+ {% endfor %}
+ <div class="product-filters">
+ <button class="btn primary" type="submit">{% trans 'Update' context 'collection price filter' %}</button>
+ </div>
+ </form>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div class="col-md-8 col-lg-9 collection-list">
+ <div>
+ <div>
+ {% if products %}
+ <div class="row">
+ {% include "collection/_items.html" with products=products %}
+ </div>
+ <div class="row">
+ <div class="m-auto">
+ {% if products_paginated.has_other_pages %}
+ {% bootstrap_pagination products_paginated extra=request.GET.urlencode %}
+ {% endif %}
+ </div>
+ </div>
+ {% else %}
+ <div class="row no-results">
+ <div class="col-12">
+ <svg data-src="{% static "images/no-results-bg.svg" %}" width="360" height="360"/>
+ <p>{% trans "Sorry, no matches found for your request." context "Search without results text" %}</p>
+ <p>{% trans "Try again or shop new arrivals." context "Search without results text" %}</p>
+ </div>
+ </div>
+ {% endif %}
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+{% endblock content %}