Repository: fidals/shopelectro

Issue:
catalog.py:189-190: Implement a new method for a...
The puzzle `470-c5d3123c` from #470 has to be resolved:

https://github.com/fidals/shopelectro/blob/814eee587fcffe14d3c2ca276eb50f53fb0af0a4/shopelectro/views/catalog.py#L189-L190

The puzzle was created by Artemiy on 06-Aug-18. 

Estimate: 15 minutes,     

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/models.py b/shopelectro/models.py
--- a/shopelectro/models.py
+++ b/shopelectro/models.py
@@ -15,6 +15,7 @@
     AbstractCategory,
     AbstractProduct,
     CategoryManager,
+    ProductActiveManager,
     ProductManager,
 )
 from ecommerce.models import Order as ecOrder
@@ -54,15 +55,6 @@ def get_absolute_url(self):
         return reverse('category', args=(self.page.slug,))
 
 
-class ProductActiveManager(models.Manager):
-    def get_queryset(self):
-        return (
-            super(ProductActiveManager, self)
-            .get_queryset()
-            .filter(page__is_active=True)
-        )
-
-
 class Product(AbstractProduct, SyncPageMixin):
 
     # That's why we are needed to explicitly add objects manager here
diff --git a/shopelectro/views/catalog.py b/shopelectro/views/catalog.py
--- a/shopelectro/views/catalog.py
+++ b/shopelectro/views/catalog.py
@@ -195,15 +195,8 @@ def get_context_data(self, **kwargs):
         ):
             raise http.Http404('Page does not exist.')  # Ignore CPDBear
 
-        # @todo #470:15m Implement a new method for a Product's manager to get all_products
-        #  as below.
-
-        all_products = (
-            models.Product.objects
-            .filter(page__is_active=True)
-            .prefetch_related('page__images')
-            .select_related('page')
-            .get_by_category(category, ordering=(sorting_option.directed_field, ))
+        all_products = models.Product.actives.get_category_descendants(
+            category, ordering=(sorting_option.directed_field, )
         )
 
         group_tags_pairs = (
@@ -290,12 +283,8 @@ def load_more(request, category_slug, offset=0, limit=0, sorting=0, tags=None):
     category = get_object_or_404(models.CategoryPage, slug=category_slug).model
     sorting_option = SortingOption(index=int(sorting))
 
-    all_products = (  # Ignore CPDBear
-        models.Product.objects
-        .filter(page__is_active=True)
-        .prefetch_related('page__images')
-        .select_related('page')
-        .get_by_category(category, ordering=(sorting_option.directed_field,))
+    all_products = models.Product.actives.get_category_descendants(
+        category, ordering=(sorting_option.directed_field,)
     )
 
     if 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
@@ -81,7 +81,11 @@ def test_category_page_contains_all_tags(self):
         response = self.get_category_page()
 
         tags = set(chain.from_iterable(map(
-            lambda x: x.tags.all(), Product.objects.get_by_category(self.category)
+            lambda x: x.tags.all(), (
+                Product.objects
+                .get_by_category(self.category)
+                .prefetch_related('tags')
+            )
         )))
 
         tag_names = list(map(attrgetter('name'), tags))