Repository: fidals/shopelectro

Issue:
Products re-linking as on stb
We already have this functinality on stb.

[Reqs from seo guys](https://docs.google.com/document/d/14ZcX5ujkQdIHfZ1tL31Nr1cHnwJ5N037Blsqf5A0jpE/edit?ts=5b13c914#).

Screen [is from this page](http://www.stroyprombeton.ru/gbi/products/638/)

![image](https://user-images.githubusercontent.com/3526796/41849042-4833b5de-7888-11e8-817d-3b18f98ef147.png)



Discussion:
i take this task, because it's about requirements clarifying

Resolving patch:
diff --git a/shopelectro/models.py b/shopelectro/models.py
--- a/shopelectro/models.py
+++ b/shopelectro/models.py
@@ -119,6 +119,16 @@ def feedback(self):
     def params(self):
         return Tag.objects.filter(products=self).get_group_tags_pairs()
 
+    # @todo #388:30m Move Product.get_siblings method to refarm-site
+    #  And reuse it on STB.
+    def get_siblings(self, offset):
+        return (
+            self.__class__.actives
+            .filter(category=self.category)
+            .prefetch_related('category')
+            .select_related('page')[:offset]
+        )
+
 
 class ProductFeedback(models.Model):
     product = models.ForeignKey(
diff --git a/shopelectro/settings/base.py b/shopelectro/settings/base.py
--- a/shopelectro/settings/base.py
+++ b/shopelectro/settings/base.py
@@ -374,3 +374,5 @@ def get_robots_content():
 
 # default for local tests. Prod's one may differ
 YANDEX_KASSA_LINK = 'https://money.yandex.ru/eshop.xml'
+
+PRODUCT_SIBLINGS_COUNT = 10
diff --git a/shopelectro/views/catalog.py b/shopelectro/views/catalog.py
--- a/shopelectro/views/catalog.py
+++ b/shopelectro/views/catalog.py
@@ -89,14 +89,17 @@ def get_context_data(self, **kwargs):
 
         group_tags_pairs = (
             models.Tag.objects
-            .filter(products=self.object)
+            .filter(products=product)
             .get_group_tags_pairs()
         )
 
         return {
             **context,
             'price_bounds': config.PRICE_BOUNDS,
-            'group_tags_pairs': group_tags_pairs
+            'group_tags_pairs': group_tags_pairs,
+            'tile_products': prepare_tile_products(
+                product.get_siblings(offset=settings.PRODUCT_SIBLINGS_COUNT)
+            ),
         }
 
     def render_siblings_on_404(
@@ -109,19 +112,14 @@ 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,
-                )
-                .prefetch_related('category')
-                .select_related('page')[:10]
-            )
             self.object = inactive_product
             context = self.get_context_data(
                 object=inactive_product,
-                tile_products=prepare_tile_products(related_products),
+                tile_products=prepare_tile_products(
+                    inactive_product.get_siblings(
+                        offset=settings.PRODUCT_SIBLINGS_COUNT
+                    )
+                ),
                 tile_title='Возможно вас заинтересуют похожие товары:',
                 **url_kwargs,
             )


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
@@ -428,23 +428,33 @@ class ProductPage(TestCase):
 
     fixtures = ['dump.json']
 
+    def setUp(self):
+        self.product = Product.objects.first()
+
     def test_orphan_product(self):
-        product = Product.objects.first()
-        product.category = None
-        product.save()
+        self.product.category = None
+        self.product.save()
 
-        response = self.client.get(product.url)
+        response = self.client.get(self.product.url)
         self.assertEqual(response.status_code, 404)
 
-    def test_related_products_on_404(self):
+    def test_related_products(self):
         """404 page of sometimes removed product should contain product's siblings."""
-        product = Product.objects.first()
-        product.page.is_active = False
-        product.save()  # saves product.page too
+        response = self.client.get(self.product.url)
+        # 404 page should show 10 siblings. We'll check the last one
+        sibling_product = self.product.category.products.all()[9]
+        self.assertTrue(
+            sibling_product.name in str(response.content)
+        )
 
-        response = self.client.get(product.url)
+    def test_related_products_on_404(self):
+        """404 page of some time ago removed product should contain product's siblings."""
+        self.product.page.is_active = False
+        self.product.save()  # saves product.page too
+
+        response = self.client.get(self.product.url)
         # 404 page should show 10 siblings. We'll check the last one
-        sibling_product = product.category.products.all()[9]
+        sibling_product = self.product.category.products.all()[9]
         self.assertEqual(response.status_code, 404)
         self.assertTrue(
             sibling_product.name in str(response.content)