Repository: fidals/shopelectro

Issue:
context.py:7: Remove the ProductImages in favor of...
The puzzle `683-0642f414` from #683 has to be resolved: 

https://github.com/fidals/shopelectro/blob/7f3bf67792297934c45486e6de8df62915076d32/shopelectro/context.py#L7-L7

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
@@ -1,34 +0,0 @@
-import typing
-
-from catalog.context import AbstractProductsListContext
-from catalog.models import ProductQuerySet
-from images.models import Image
-
-# @todo #683:60m Remove the ProductImages in favor of catalog.newcontext.ProductImages
-
-
-class ProductImages(AbstractProductsListContext):
-
-    @property
-    def images(self) -> typing.Dict[int, Image]:
-        assert isinstance(self.products, ProductQuerySet)
-
-        images = {}
-        if self.product_pages:
-            images = Image.objects.get_main_images_by_pages(
-                self.product_pages.filter(shopelectro_product__in=self.products)
-            )
-
-        return {
-            product.id: images.get(product.page)
-            for product in self.products
-        }
-
-    def get_context_data(self):
-        return {
-            'product_images': self.images,
-            **(
-                self.super.get_context_data()
-                if self.super else {}
-            ),
-        }
diff --git a/shopelectro/views/catalog.py b/shopelectro/views/catalog.py
--- a/shopelectro/views/catalog.py
+++ b/shopelectro/views/catalog.py
@@ -12,7 +12,6 @@
 from images.models import Image
 from pages import views as pages_views
 
-from shopelectro import context as se_context
 from shopelectro import models
 from shopelectro.views.helpers import set_csrf_cookie
 
@@ -129,31 +128,23 @@ def get_context_data(self, **kwargs):
         tile_products = self.product.get_siblings(
             offset=settings.PRODUCT_SIBLINGS_COUNT
         )
-        product_images = self.get_images_context_data(tile_products)['product_images']
+        product_images = self.get_images_context_data(tile_products)
 
         return {
             **context_,
+            **product_images,
             'price_bounds': settings.PRICE_BOUNDS,
             'group_tags_pairs': self.product.get_params(),
-            'product_images': product_images,
             'tile_products': tile_products,
         }
 
     def get_images_context_data(self, products) -> dict:
         """Return images for given products."""
         products_to_filter = [self.product, *products]
-        product_ids_to_filter = [p.id for p in products_to_filter]
-        return (
-            se_context.ProductImages(
-                url_kwargs={},
-                request=self.request,
-                page=self.product.page,
-                products=models.Product.objects.filter(id__in=product_ids_to_filter),
-                product_pages=models.ProductPage.objects.filter(
-                    shopelectro_product__in=products_to_filter
-                ),
-            ).get_context_data()
-        )
+        return newcontext.products.ProductImages(
+            newcontext.Products(products_to_filter),
+            Image.objects.all(),
+        ).context()
 
     def render_siblings_on_404(
         self, request, **url_kwargs
@@ -176,9 +167,7 @@ def render_siblings_on_404(
                 **url_kwargs,
             )
 
-            context_['product_images'] = (
-                self.get_images_context_data(siblings)['product_images']
-            )
+            context_.update(self.get_images_context_data(siblings))
             return render(request, 'catalog/product_404.html', context_, status=404)
 
 
@@ -201,29 +190,18 @@ def get_context_data(self, **kwargs):
         if not mobile_view:
             tile_products = top_products
 
+        images_ctx = newcontext.products.ProductImages(
+            newcontext.Products(tile_products),
+            Image.objects.all(),
+        ).context()
         return {
             **context_,
+            **images_ctx,
             'tile_title': 'ТОП 10 ТОВАРОВ',
             'category_tile': settings.MAIN_PAGE_TILE,
             'tile_products': tile_products,
-            'product_images': (
-                self.get_products_context_data(
-                    products=top_products
-                )['product_images']
-            )
         }
 
-    def get_products_context_data(self, products=None, product_pages=None) -> dict:
-        return (
-            se_context.ProductImages(
-                url_kwargs={},  # Ignore CPDBear
-                request=self.request,
-                page=self.object,
-                products=products or models.Product.objects.all(),
-                product_pages=product_pages or models.ProductPage.objects.all(),
-            ).get_context_data()
-        )
-
 
 @set_csrf_cookie
 class CategoryPage(catalog.CategoryPage):


Test patch: