Repository: fidals/shopelectro

Issue:
Not relevant tags
Every category on prod contains all tags, not only the relevant.

Example:
https://www.shopelectro.ru/catalog/categories/fonari-226/


Discussion:
i'll provide full realization at the same task. Prod waits it

Resolving patch:
diff --git a/shopelectro/views/catalog.py b/shopelectro/views/catalog.py
--- a/shopelectro/views/catalog.py
+++ b/shopelectro/views/catalog.py
@@ -44,18 +44,19 @@ def get_catalog_context(request, page, category, raw_tags, page_number, per_page
 
     # @todo #683:30m Remove *Tags and *Products suffixes from catalog.newcontext classes.
     #  Rename Checked404Tags to ExistingOr404.
+    products_by_category = newcontext.products.ProductsByCategory(
+        category=category,
+        products=newcontext.products.ActiveProducts(
+            newcontext.Products(
+                models.Product.objects.all(),
+            ),
+        ),
+    )
     products = newcontext.products.OrderedProducts(
         sorting_index=sorting_index,
         products=newcontext.products.TaggedProducts(
             tags=selected_tags,
-            products=newcontext.products.ProductsByCategory(
-                category=category,
-                products=newcontext.products.ActiveProducts(
-                    newcontext.Products(
-                        models.Product.objects.all(),
-                    ),
-                ),
-            ),
+            products=products_by_category
         ),
     )
 
@@ -68,7 +69,9 @@ def get_catalog_context(request, page, category, raw_tags, page_number, per_page
 
     images = newcontext.products.ProductImages(paginated_products, Image.objects.all())
     brands = newcontext.products.ProductBrands(paginated_products, all_tags)
-    grouped_tags = newcontext.tags.GroupedTags(all_tags)
+    grouped_tags = newcontext.tags.GroupedTags(
+        tags=newcontext.tags.TagsByProducts(all_tags, products_by_category.qs())
+    )
     page = se_context.Page(page, selected_tags)
 
     contexts = newcontext.Contexts(page, paginated_products, images, brands, grouped_tags)


Test patch:
diff --git a/shopelectro/tests/tests_views.py b/shopelectro/tests/tests_views.py
--- a/shopelectro/tests/tests_views.py
+++ b/shopelectro/tests/tests_views.py
@@ -131,6 +131,10 @@ def test_contains_product_with_certain_tags(self):
             models.Product.objects.filter(Q(tags=tags[0]) | Q(tags=tags[1]))
         )))
 
+        self.assertEqual(
+            products_count,
+            response.context['paginated']['total_products']
+        )
         self.assertContains(response, products_count)
 
     def test_tag_titles_content_disjunction(self):
@@ -215,6 +219,17 @@ def test_non_existing_tags_404(self):
         response = self.client.get(bad_tag_url)
         self.assertEqual(response.status_code, 404)
 
+    def test_category_tag_relation(self):
+        """Category page should contain only tags, related to the current category."""
+        category = models.Category.objects.get(
+            name='Category #0 of #Category #0 of #Category #0'
+        )
+        # category is not related to this tag at DB ...
+        tag = models.Tag.objects.exclude_by_products(category.products.all()).first()
+        # ... so category page should not contain this tag
+        response = self.client.get(category.url)
+        self.assertNotContains(response, tag.name)
+
 
 @tag('fast')
 class CatalogPagination(BaseCatalogTestCase):