From 645782d2556f0b644c31ca8e828838ed687fc445 Mon Sep 17 00:00:00 2001 From: duker Date: Wed, 29 Aug 2018 17:32:08 +0300 Subject: [PATCH] #550 Fork SortingCategoryPage class --- shopelectro/views/catalog.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/shopelectro/views/catalog.py b/shopelectro/views/catalog.py index 34807a33..a32e7679 100644 --- a/shopelectro/views/catalog.py +++ b/shopelectro/views/catalog.py @@ -183,10 +183,8 @@ def merge_products_context(products): 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,) + self.object.model ) def get_context_data(self, **kwargs): @@ -278,11 +276,30 @@ def template_context(page, tag_titles, tags): return page -class PaginationCategoryPage(DBContextCategoryPage): +class SortingCategoryPage(DBContextCategoryPage): + + 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_sorting_index(self): return int(self.kwargs.get('sorting', 0)) + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + products = self.get_products() + return { + **context, + 'products_data': merge_products_context(products), + 'sort': self.get_sorting_index(), + } + + +class PaginationCategoryPage(SortingCategoryPage): + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) products_on_page = int(self.request.GET.get( @@ -312,10 +329,10 @@ def get_context_data(self, **kwargs): 'paginated_page': paginated_page, 'sorting_options': settings.CATEGORY_SORTING_OPTIONS.values(), 'limits': settings.CATEGORY_STEP_MULTIPLIERS, - 'sort': self.get_sorting_index(), } +# TODO make load_more one of the context classes def load_more(request, category_slug, offset=0, limit=0, sorting=0, tags=None): """ Load more products of a given category.