Repository: fidals/shopelectro

Issue:
Stale links on main page
Links to category with slug `akkumulators-aaa` and maybe some similar


Discussion:
we should change settings.MAIN_PAGE_TILE struct to inject links on tags, but not on categories.
I'll do it in the same task
links to change:
```
Фонари
https://www.shopelectro.ru/catalog/categories/fonari-226/tags/ruchnoi/
https://www.shopelectro.ru/catalog/categories/fonari-226/tags/nalobnyi/
https://www.shopelectro.ru/catalog/categories/fonari-226/tags/kempingovye/
https://www.shopelectro.ru/catalog/categories/avtomobilnye-fonari/

Аккумуляторы
https://www.shopelectro.ru/catalog/categories/akkumuliatory-270/tags/aa/
https://www.shopelectro.ru/catalog/categories/akkumuliatory-270/tags/aaa/
```

Resolving patch:
diff --git a/shopelectro/settings/base.py b/shopelectro/settings/base.py
--- a/shopelectro/settings/base.py
+++ b/shopelectro/settings/base.py
@@ -410,11 +410,14 @@ def get_robots_content():
     },
 }
 
-# Tile on main page
+# Tile on main page.
+# Every link should have either slug or url.
 MAIN_PAGE_TILE = {
     'accumulators': [
-        {'name': 'Типоразмера AA', 'slug': 'tiporazmer-aa'},
-        {'name': 'Типоразмера ААА', 'slug': 'akkumuliatory-aaa'},
+        {'name': 'Типоразмера AA',
+         'url': '/catalog/categories/akkumuliatory-270/tags/aa/'},
+        {'name': 'Типоразмера ААА',
+         'url': '/catalog/categories/akkumuliatory-270/tags/aaa/'},
         {'name': 'Для радиомоделей и игрушек',
          'slug': 'akkumuliatory-dlia-radioupravliaemykh-modelei-i-igrushek'},
         {'name': 'Свинцово-кислотные',
@@ -435,9 +438,10 @@ def get_robots_content():
         {'name': 'Дисковые литиевые', 'slug': 'diskovye-litievye-batareiki-3-v'},
     ],
     'lights': [
-        {'name': 'Ручные', 'slug': 'ruchnye-fonari'},
-        {'name': 'Налобные', 'slug': 'nalobnye-fonari'},
-        {'name': 'Туристические', 'slug': 'turisticheskie-kempingovye-fonari'},
+        {'name': 'Ручные', 'url': '/catalog/categories/fonari-226/tags/ruchnoi/'},
+        {'name': 'Налобные', 'url': '/catalog/categories/fonari-226/tags/nalobnyi/'},
+        {'name': 'Туристические', 'url': '/categories/fonari-226/tags/kempingovye/'},
+        {'name': 'Автомобильные', 'slug': 'avtomobilnye-fonari'},
     ],
     'chargers': [
         {'name': 'Для фотоаппаратов',
diff --git a/shopelectro/views/catalog.py b/shopelectro/views/catalog.py
--- a/shopelectro/views/catalog.py
+++ b/shopelectro/views/catalog.py
@@ -3,6 +3,7 @@
 from django import http
 from django.conf import settings
 from django.shortcuts import render, get_object_or_404
+from django.urls import reverse
 from django.views.decorators.http import require_POST
 from django_user_agents.utils import get_user_agent
 
@@ -10,7 +11,6 @@
 from catalog.views import catalog
 from images.models import Image
 from pages import views as pages_views
-
 from shopelectro import models, context as se_context
 from shopelectro.views.helpers import set_csrf_cookie
 
@@ -170,6 +170,17 @@ def render_siblings_on_404(
 @set_csrf_cookie
 class IndexPage(pages_views.CustomPageView):
 
+    @staticmethod
+    def get_categories_tile():
+        """Patch every link with url, generated from slug."""
+        return {section: [
+            {**link, 'url': (
+                reverse('category', kwargs={'slug': link['slug']})
+                if link.get('slug') and not link.get('url')
+                else link['url']
+            )} for link in links
+        ] for section, links in settings.MAIN_PAGE_TILE.items()}
+
     def get_context_data(self, **kwargs):
         """Extended method. Add product's images to context."""
         context_ = super(IndexPage, self).get_context_data(**kwargs)
@@ -193,7 +204,7 @@ def get_context_data(self, **kwargs):
             **context_,
             **images_ctx,
             'tile_title': 'ТОП 10 ТОВАРОВ',
-            'category_tile': settings.MAIN_PAGE_TILE,
+            'category_tile': self.get_categories_tile(),
             'tile_products': tile_products,
         }
 


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
@@ -6,25 +6,24 @@
 """
 import json
 from functools import partial
-from itertools import chain
 from operator import attrgetter
-from xml.etree import ElementTree as ET
 from urllib.parse import urlparse, quote
+from xml.etree import ElementTree as ET
 
 from bs4 import BeautifulSoup
 from django.conf import settings
 from django.db.models import Q
 from django.http import HttpResponse
-from django.test import TestCase, tag
+from django.test import override_settings, TestCase, tag
 from django.urls import reverse
 from django.utils.translation import ugettext as _
+from itertools import chain
 
 from catalog.helpers import reverse_catalog_url
-
 from shopelectro import models
-from shopelectro.views.service import generate_md5_for_ya_kassa, YANDEX_REQUEST_PARAM
+from shopelectro import views
 from shopelectro.tests.helpers import create_doubled_tag
-
+from shopelectro.views.service import generate_md5_for_ya_kassa, YANDEX_REQUEST_PARAM
 
 CANONICAL_HTML_TAG = '<link rel="canonical" href="{path}">'
 
@@ -527,6 +526,26 @@ def test_page_db_template_with_special_chars(self):
         self.assertEqual(200, response.status_code)
 
 
+@tag('fast')
+class IndexPage(TestCase):
+
+    MAIN_PAGE_TILE = {
+        'some_section': [
+            {'name': 'Has url', 'url': '/section/first/'},
+            {'name': 'Has url and slug', 'slug': 'slug', 'url': '/section/second/'},
+            {'name': 'Has slug, but not url', 'slug': 'third'},
+        ]
+    }
+
+    def test_get_category_tile(self):
+        with override_settings(MAIN_PAGE_TILE=self.MAIN_PAGE_TILE):
+            tile = views.IndexPage.get_categories_tile()
+        first_url, second_url, third_url = [link['url'] for link in tile['some_section']]
+        self.assertEqual('/section/first/', first_url)
+        self.assertEqual('/section/second/', second_url)
+        self.assertEqual('/catalog/categories/third/', third_url)
+
+
 @tag('fast')
 class ProductPage(TestCase):