Repository: fidals/shopelectro

Issue:
catalog.py:76-78: Create context class(es). Remove...
The puzzle `683-b33e7e30` from #683 has to be resolved: 

https://github.com/fidals/shopelectro/blob/7f3bf67792297934c45486e6de8df62915076d32/shopelectro/views/catalog.py#L76-L78

The puzzle was created by Artemiy on 18-Jan-19. 

  

If you have any technical questions, don't ask me, submit new tickets instead. The task will be \"done\" when the problem is fixed and the text of the puzzle is _removed_ from the source code. Here is more about [PDD](http://www.yegor256.com/2009/03/04/pdd.html) and [about me](http://www.yegor256.com/2017/04/05/pdd-in-action.html). 



Discussion:
@artemiyds2 @duker33 please pay attention to this new issue.

Resolving patch:
diff --git a/shopelectro/context.py b/shopelectro/context.py
--- a/shopelectro/context.py
+++ b/shopelectro/context.py
@@ -0,0 +1,28 @@
+from functools import partial
+
+from catalog.newcontext import Context, Tags
+
+
+class Page(Context):
+
+    def __init__(self, page, tags: Tags):
+        self._page = page
+        self._tags = tags
+
+    def context(self):
+        def template_context(page, tag_titles, tags):
+            return {
+                'page': page,
+                'tag_titles': tag_titles,
+                'tags': tags,
+            }
+
+        tags_qs = self._tags.qs()
+        self._page.get_template_render_context = partial(
+            template_context, self._page, tags_qs.as_title(), tags_qs
+        )
+
+        return {
+            'page': self._page,
+            'skip_canonical': tags_qs.exists(),
+        }
diff --git a/shopelectro/views/catalog.py b/shopelectro/views/catalog.py
--- a/shopelectro/views/catalog.py
+++ b/shopelectro/views/catalog.py
@@ -1,5 +1,4 @@
 import typing
-from functools import partial
 
 from django import http
 from django.conf import settings
@@ -12,7 +11,7 @@
 from images.models import Image
 from pages import views as pages_views
 
-from shopelectro import models
+from shopelectro import models, context as se_context
 from shopelectro.views.helpers import set_csrf_cookie
 
 PRODUCTS_ON_PAGE_PC = 48
@@ -34,7 +33,7 @@ def get_view_type(request):
 # @todo #683:60m Create context class(es) for catalog page representation.
 #  Created class(es) will compose the context classes for catalog views.
 #  Remove get_catalog_context in favor of created class(es).
-def get_catalog_context(request, category, raw_tags, page_number, per_page, sorting_index):
+def get_catalog_context(request, page, category, raw_tags, page_number, per_page, sorting_index):
     all_tags = newcontext.Tags(models.Tag.objects.all())
     selected_tags = newcontext.tags.ParsedTags(
         tags=all_tags,
@@ -70,17 +69,10 @@ def get_catalog_context(request, category, raw_tags, page_number, per_page, sort
     images = newcontext.products.ProductImages(paginated_products, Image.objects.all())
     brands = newcontext.products.ProductBrands(paginated_products, all_tags)
     grouped_tags = newcontext.tags.GroupedTags(all_tags)
+    page = se_context.Page(page, selected_tags)
 
-    contexts = newcontext.Contexts(paginated_products, images, brands, grouped_tags)
-    # @todo #683:60m Create context class(es).
-    #  Remove optional_context from get_catalog_context and
-    #  patching of page.get_template_render_context in favor of created class(es).
-    optional_context = {
-        'skip_canonical': selected_tags.qs().exists(),
-        'total_products': products.qs().count(),
-        'selected_tags': selected_tags.qs(),
-    }
-    return contexts, optional_context
+    contexts = newcontext.Contexts(page, paginated_products, images, brands, grouped_tags)
+    return contexts
 
 
 # CATALOG VIEWS
@@ -210,8 +202,9 @@ def get_context_data(self, **kwargs):
         """Add sorting options and view_types in context."""
         sorting_index = int(self.kwargs.get('sorting', 0))
 
-        contexts, optional_context = get_catalog_context(
+        contexts = get_catalog_context(
             request=self.request,
+            page=self.object,
             category=self.object.model,
             raw_tags=self.kwargs.get('tags'),
             page_number=int(self.request.GET.get('page', 1)),
@@ -221,24 +214,9 @@ def get_context_data(self, **kwargs):
             sorting_index=sorting_index,
         )
 
-        selected_tags = optional_context['selected_tags']
-        if selected_tags:
-            def template_context(page, tag_titles, tags):
-                return {
-                    'page': page,
-                    'tag_titles': tag_titles,
-                    'tags': tags,
-                }
-
-            page = self.object
-            page.get_template_render_context = partial(
-                template_context, page, selected_tags.as_title(), selected_tags
-            )
-
         return {
             **super().get_context_data(**kwargs),
             **contexts.context(),
-            **optional_context,
             'view_type': get_view_type(self.request),
             'sorting_options': settings.CATEGORY_SORTING_OPTIONS.values(),
             'limits': settings.CATEGORY_STEP_MULTIPLIERS,
@@ -266,12 +244,13 @@ def load_more(request, slug, offset=0, limit=0, sorting=0, tags=None):
     # 11 // 12 = 0, 0 // 12 = 0 but it should be the first page
     # 12 // 12 = 1, 23 // 12 = 1, but it should be the second page
     page_number = (offset // products_on_page) + 1
-    category = get_object_or_404(models.CategoryPage, slug=slug).model
+    page = get_object_or_404(models.CategoryPage, slug=slug)
     sorting_index = int(sorting)
 
-    contexts, _ = get_catalog_context(
+    contexts = get_catalog_context(
         request=request,
-        category=category,
+        page=page,
+        category=page.model,
         raw_tags=tags,
         page_number=page_number,
         per_page=products_on_page,


Test patch: