Repository: fidals/shopelectro

Issue:
product_404.html:14-17: Create styles for 404 products...
The puzzle `444-2276c763` from #444 has to be resolved:

https://github.com/fidals/shopelectro/blob/4db19ac9abcba2ce9849c5fd1210ba4ff7b0b8d3/templates/catalog/product_404.html#L14-L17

The puzzle was created by duker33 on 02-Aug-18. 

Estimate: 60 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/views/catalog.py b/shopelectro/views/catalog.py
--- a/shopelectro/views/catalog.py
+++ b/shopelectro/views/catalog.py
@@ -38,6 +38,21 @@ class CategoryTree(catalog.CategoryTree):
     category_model = models.Category
 
 
+def prepare_tile_products(products):
+    images = Image.objects.get_main_images_by_pages(
+        models.ProductPage.objects.filter(
+            shopelectro_product__in=products
+        )
+    )
+    categories = models.Category.objects.get_root_categories_by_products(
+        products
+    )
+    return [
+        (product, images.get(product.page), categories.get(product))
+        for product in products
+    ]
+
+
 @set_csrf_cookie
 class ProductPage(catalog.ProductPage):
     pk_url_kwarg = None
@@ -94,13 +109,22 @@ def render_siblings_on_404(
             page__is_active=False
         ).first()
         if inactive_product:
-            related_products = models.Product.objects.filter(
-                category=inactive_product.category,
-                page__is_active=True
-            )[:10]
+            related_products = (
+                models.Product.objects
+                .filter(
+                    category=inactive_product.category,
+                    page__is_active=True,
+                )
+                .prefetch_related('category')
+                .select_related('page')[:10]
+            )
             self.object = inactive_product
-            context = self.get_context_data(object=inactive_product, **url_kwargs)
-            context.update(related_products=related_products)
+            context = self.get_context_data(
+                object=inactive_product,
+                tile_products=prepare_tile_products(related_products),
+                tile_title='Возможно вас заинтересуют похожие товары:',
+                **url_kwargs,
+            )
             return render(request, 'catalog/product_404.html', context, status=404)
 
 
@@ -113,33 +137,21 @@ def get_context_data(self, **kwargs):
         context = super(IndexPage, self).get_context_data(**kwargs)
         mobile_view = get_user_agent(self.request).is_mobile
 
-        top_products = (
-            models.Product.objects
-            .filter(id__in=settings.TOP_PRODUCTS, page__is_active=True)
-            .prefetch_related('category')
-            .select_related('page')
-        )
-
-        images = Image.objects.get_main_images_by_pages(
-            models.ProductPage.objects.filter(
-                shopelectro_product__in=top_products
-            )
-        )
-
-        categories = models.Category.objects.get_root_categories_by_products(
-            top_products)
-
-        prepared_top_products = []
+        tile_products = []
         if not mobile_view:
-            prepared_top_products = [
-                (product, images.get(product.page), categories.get(product))
-                for product in top_products
-            ]
+            top_products = (
+                models.Product.objects
+                .filter(id__in=settings.TOP_PRODUCTS, page__is_active=True)
+                .prefetch_related('category')
+                .select_related('page')
+            )
+            tile_products = prepare_tile_products(top_products)
 
         return {
             **context,
+            'tile_title': 'ТОП 10 ТОВАРОВ',
             'category_tile': config.MAIN_PAGE_TILE,
-            'prepared_top_products': prepared_top_products,
+            'tile_products': tile_products,
         }
 
 
@@ -172,10 +184,14 @@ def get_context_data(self, **kwargs):
             page_number < 1 or
             products_on_page not in settings.CATEGORY_STEP_MULTIPLIERS
         ):
-            raise http.Http404('Page does not exist.')
+            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, ))
@@ -267,6 +283,7 @@ def load_more(request, category_slug, offset=0, limit=0, sorting=0, tags=None):
 
     all_products = (
         models.Product.objects
+        .filter(page__is_active=True)
         .prefetch_related('page__images')
         .select_related('page')
         .get_by_category(category, ordering=(sorting_option,))


Test patch: