Skip to content

Commit

Permalink
#550 Create TaggedCategoryPage class
Browse files Browse the repository at this point in the history
  • Loading branch information
duker33 committed Aug 27, 2018
1 parent 8f7127f commit 32853e0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 36 deletions.
8 changes: 4 additions & 4 deletions shopelectro/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def cache_page(arg): # Ignore PyFlakesBear
catalog_urls = [
# "category" group
url(r'^categories/(?P<slug>[\w-]+)/$',
cached_2h(views.CategoryPage.as_view()), name='category'),
cached_2h(views.TaggedCategoryPage.as_view()), name='category'),
url(r'^categories/(?P<slug>[\w-]+)/tags/(?P<tags>[\w_-]+)/$',
cached_2h(views.CategoryPage.as_view()), name='category'),
cached_2h(views.TaggedCategoryPage.as_view()), name='category'),
url(r'^categories/(?P<slug>[\w-]+)/(?P<sorting>[0-9]*)/$',
views.CategoryPage.as_view(), name='category'),
views.TaggedCategoryPage.as_view(), name='category'),
url(r'^categories/(?P<slug>[\w-]+)/(?P<sorting>[0-9]*)/tags/(?P<tags>[\w_-]+)/$',
views.CategoryPage.as_view(), name='category'),
views.TaggedCategoryPage.as_view(), name='category'),
# "load more" group
url(r'categories/(?P<category_slug>[\w-]+)/load-more/'
r'(?P<offset>[0-9]+)/(?P<sorting>[0-9]*)/$',
Expand Down
91 changes: 59 additions & 32 deletions shopelectro/views/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from functools import partial

from django import http
from django.db.models import QuerySet
from django.conf import settings
from django.core.paginator import Paginator, InvalidPage
from django.shortcuts import render, get_object_or_404
Expand Down Expand Up @@ -178,49 +179,73 @@ def merge_products_context(products):
@set_csrf_cookie
class CategoryPage(catalog.CategoryPage):

def get_products(self) -> QuerySet:
sorting_index = int(self.kwargs.get('sorting', 0))
sorting_option = SortingOption(index=sorting_index)
return models.Product.actives.get_category_descendants(
self.object.model, ordering=(sorting_option.directed_field,)
)

def get_context_data(self, **kwargs):
"""Add sorting options and view_types in context."""
context = super().get_context_data(**kwargs)
products_on_page = int(self.request.GET.get(
'step', get_products_count(self.request),
))
page_number = int(self.request.GET.get('page', 1))
view_type = self.request.session.get('view_type', 'tile')
sorting_index = int(self.kwargs.get('sorting', 0))
sorting_option = SortingOption(index=sorting_index)
category = context['category']
if (
page_number < 1 or
products_on_page not in settings.CATEGORY_STEP_MULTIPLIERS
):
raise http.Http404('Page does not exist.') # Ignore CPDBear

all_products = models.Product.actives.get_category_descendants(
category, ordering=(sorting_option.directed_field, )
)
products = self.get_products()

group_tags_pairs = (
models.Tag.objects
.filter_by_products(all_products)
.filter_by_products(products)
.get_group_tags_pairs()
)

tags = self.kwargs.get('tags')
return {
**context,
'products_data': merge_products_context(products),
'group_tags_pairs': group_tags_pairs,
}

tag_titles = ''
if tags:
slugs = models.Tag.parse_url_tags(tags)
tags = models.Tag.objects.filter(slug__in=slugs)

all_products = (
all_products
class TaggedCategoryPage(CategoryPage):

def get_sorting_index(self):
return int(self.kwargs.get('sorting', 0))

def get_tags(self) -> typing.Union[models.TagQuerySet, None]:
tags = self.kwargs.get('tags')
if not tags:
return None
slugs = models.Tag.parse_url_tags(tags)
return models.Tag.objects.filter(slug__in=slugs)

def get_products(self):
products = super().get_products()
sorting_option = SortingOption(index=self.get_sorting_index())
tags = self.get_tags()
if tags:
products = (
products
.filter(tags__in=tags)
# Use distinct because filtering by QuerySet tags,
# that related with products by many-to-many relation.
.distinct(sorting_option.field)
)
return products

tag_titles = models.serialize_tags_to_title(tags)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
products_on_page = int(self.request.GET.get(
'step', get_products_count(self.request),
))
page_number = int(self.request.GET.get('page', 1))
view_type = self.request.session.get('view_type', 'tile')

if (
page_number < 1 or
products_on_page not in settings.CATEGORY_STEP_MULTIPLIERS
):
raise http.Http404('Page does not exist.') # Ignore CPDBear

products = self.get_products()

def template_context(page, tag_titles, tags):
return {
Expand All @@ -229,26 +254,28 @@ def template_context(page, tag_titles, tags):
'tags': tags,
}

page = context['page']
page.get_template_render_context = partial(
template_context, page, tag_titles, tags)
tags = self.get_tags()
if tags:
tag_titles = models.serialize_tags_to_title(tags)
page = context['page']
page.get_template_render_context = partial(
template_context, page, tag_titles, tags)

paginated_page = get_paginated_page_or_404(all_products, products_on_page, page_number)
total_products = all_products.count()
paginated_page = get_paginated_page_or_404(products, products_on_page, page_number)
total_products = products.count()
products = paginated_page.object_list
if not products:
raise http.Http404('Page without products does not exist.')

return {
**context,
'products_data': merge_products_context(products),
'group_tags_pairs': group_tags_pairs,
'total_products': total_products,
'products_count': (page_number - 1) * products_on_page + products.count(),
'paginated_page': paginated_page,
'sorting_options': settings.CATEGORY_SORTING_OPTIONS.values(),
'limits': settings.CATEGORY_STEP_MULTIPLIERS,
'sort': sorting_index,
'sort': self.get_sorting_index(),
'tags': tags,
'view_type': view_type,
'skip_canonical': bool(tags),
Expand Down

0 comments on commit 32853e0

Please sign in to comment.