From 9cae226e66940fc5e10789eae4bc5566e49801f5 Mon Sep 17 00:00:00 2001 From: duker33 Date: Sun, 27 Jan 2019 13:29:58 +0300 Subject: [PATCH] #708 Fix tags context for category page (#710) * #708 Fix tags context for category page * #708 Grade refarm dep to `0.4.28` --- docker/docker-compose.yml | 13 +++++++------ requirements.txt | 2 +- shopelectro/tests/tests_views.py | 15 +++++++++++++++ shopelectro/views/catalog.py | 21 ++++++++++++--------- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index a5d0d372..2c79dced 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -37,12 +37,13 @@ services: # contains refarm-site modules - $DEPS_DIR # Thus, you can work with apps related to the refarm-site - # - $REFARM_DIR/catalog:/usr/local/lib/python3.7/site-packages/catalog - # - $REFARM_DIR/images:/usr/local/lib/python3.7/site-packages/images - # - $REFARM_DIR/ecommerce:/usr/local/lib/python3.7/site-packages/ecommerce - # - $REFARM_DIR/pages:/usr/local/lib/python3.7/site-packages/pages - # - $REFARM_DIR/search:/usr/local/lib/python3.7/site-packages/search - # - $REFARM_DIR/refarm_test_utils:/usr/local/lib/python3.7/site-packages/refarm_test_utils + #- $REFARM_DIR/catalog:/usr/local/lib/python3.7/site-packages/catalog + #- $REFARM_DIR/images:/usr/local/lib/python3.7/site-packages/images + #- $REFARM_DIR/ecommerce:/usr/local/lib/python3.7/site-packages/ecommerce + #- $REFARM_DIR/pages:/usr/local/lib/python3.7/site-packages/pages + #- $REFARM_DIR/search:/usr/local/lib/python3.7/site-packages/search + #- $REFARM_DIR/refarm_test_utils:/usr/local/lib/python3.7/site-packages/refarm_test_utils + #- $REFARM_DIR/refarm_pagination:/usr/local/lib/python3.7/site-packages/refarm_pagination app-drone: <<: *app-base diff --git a/requirements.txt b/requirements.txt index 2cc9e028..78810ec7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,4 +23,4 @@ ua-parser==0.8.0 user-agents==1.1.0 sorl-thumbnail==12.5.0 https://github.com/selwin/django-user_agents/archive/master.zip -https://github.com/fidals/refarm-site/archive/0.4.27.zip +https://github.com/fidals/refarm-site/archive/0.4.28.zip diff --git a/shopelectro/tests/tests_views.py b/shopelectro/tests/tests_views.py index bdc954e6..d38ebe42 100644 --- 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): diff --git a/shopelectro/views/catalog.py b/shopelectro/views/catalog.py index 4c89e4e5..e830b0ed 100644 --- 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)