Skip to content

Commit

Permalink
#550 Fork PaginatedCatalogPage class
Browse files Browse the repository at this point in the history
  • Loading branch information
duker33 committed Aug 29, 2018
1 parent 1dbb068 commit f2313ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 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.DBContextCategoryPage.as_view()), name='category'),
cached_2h(views.PaginationCategoryPage.as_view()), name='category'),
url(r'^categories/(?P<slug>[\w-]+)/tags/(?P<tags>[\w_-]+)/$',
cached_2h(views.DBContextCategoryPage.as_view()), name='category'),
cached_2h(views.PaginationCategoryPage.as_view()), name='category'),
url(r'^categories/(?P<slug>[\w-]+)/(?P<sorting>[0-9]*)/$',
views.DBContextCategoryPage.as_view(), name='category'),
views.PaginationCategoryPage.as_view(), name='category'),
url(r'^categories/(?P<slug>[\w-]+)/(?P<sorting>[0-9]*)/tags/(?P<tags>[\w_-]+)/$',
views.DBContextCategoryPage.as_view(), name='category'),
views.PaginationCategoryPage.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
38 changes: 23 additions & 15 deletions shopelectro/views/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,29 @@ def get_context_data(self, **kwargs):
class DBContextCategoryPage(TaggedCategoryPage):
"""Process db page data as db context."""

def get_object(self, queryset=None):
page = super().get_object(queryset)

def template_context(page, tag_titles, tags):
return {
'page': page,
'tag_titles': tag_titles,
'tags': tags,
}

tags = super().get_tags()
if tags:
# TODO - move to QuerySet
tag_titles = models.serialize_tags_to_title(tags)
page.get_template_render_context = partial(
template_context, page, tag_titles, tags
)

return page


class PaginationCategoryPage(DBContextCategoryPage):

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

Expand All @@ -275,21 +298,6 @@ def get_context_data(self, **kwargs):

products = self.get_products()

def template_context(page, tag_titles, tags):
return {
'page': page,
'tag_titles': tag_titles,
'tags': tags,
}

tags = super().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(products, products_on_page, page_number)
total_products = products.count()
products = paginated_page.object_list
Expand Down

0 comments on commit f2313ab

Please sign in to comment.