Repository: fidals/shopelectro

Issue:
Reorder category links in the top menu
---
Убираем радиоприемники. расставляем как на скрине 

![image](https://user-images.githubusercontent.com/3526796/56789032-a2fd1480-6809-11e9-86e4-db107487c542.png)

From this doc:
https://docs.google.com/document/d/1y4xUQhQ4V_Ln5xwcEB2zMp7KTUQnkn9agKTTAU1z0-c/edit



Discussion:
Page model have a position and an active state, so we can make this fully from admin panel

@duker33 Should we make радиоприемники inactive or just hide them from header?
@artemiy312 , just hide them from header.
We should sort menu items in the right way. Let's do it with a settings option.
Maybe some markup issues will be raised after such sorting. For example menu items can take more then two strings after sorting
>Let's do it with a settings option.

Right now they are sorted by `position` fields, so we can just update these values to solve the issue.
@duker33  May we make it or is there any reason to avoid it? 
@artemiy312 , let's do it. Try it directly on the prod and check if markup looks good

Resolving patch:
diff --git a/shopelectro/settings/base.py b/shopelectro/settings/base.py
--- a/shopelectro/settings/base.py
+++ b/shopelectro/settings/base.py
@@ -81,7 +81,7 @@
 # https://docs.djangoproject.com/en/1.11/ref/middleware/#middleware-ordering
 MIDDLEWARE = [
     'django.middleware.security.SecurityMiddleware',
-    # 'django.middleware.cache.UpdateCacheMiddleware',
+    'django.middleware.cache.UpdateCacheMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.locale.LocaleMiddleware',
     'django.middleware.common.CommonMiddleware',
@@ -89,7 +89,7 @@
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
-    # 'django.middleware.cache.FetchFromCacheMiddleware',
+    'django.middleware.cache.FetchFromCacheMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
     'django_user_agents.middleware.UserAgentMiddleware',
     'debug_toolbar.middleware.DebugToolbarMiddleware',
@@ -468,6 +468,11 @@ def get_robots_content():
     ],
 }
 
+HEADER_LINKS = {
+    'exclude': ['radiopriiomniki'],
+    'add': ['remont-akkumulyatorov'],
+}
+
 FOOTER_LINKS = {
     'services': [
         {'name': 'Ремонт аккумуляторов', 'slug': 'remont-akkumulyatorov'},
diff --git a/shopelectro/templatetags/se_extras.py b/shopelectro/templatetags/se_extras.py
--- a/shopelectro/templatetags/se_extras.py
+++ b/shopelectro/templatetags/se_extras.py
@@ -1,5 +1,6 @@
 import datetime
 import math
+from itertools import chain
 
 from django import template
 from django.conf import settings
@@ -10,7 +11,7 @@
 from images.models import ImageMixin
 from pages.models import Page
 
-from shopelectro.models import Category
+from shopelectro.models import CategoryPage
 
 register = template.Library()
 
@@ -18,14 +19,16 @@
 @register.simple_tag
 def roots():
     return sorted(
-        filter(
-            lambda x: x.page.is_active,
-            Category.objects
-            .select_related('page')
-            # about get_cached_trees: https://goo.gl/rFKiku
-            .get_cached_trees()
+        chain(
+            Page.objects.filter(slug__in=settings.HEADER_LINKS['add']),
+            filter(
+                lambda x: x.is_active,
+                CategoryPage.objects.exclude(slug__in=settings.HEADER_LINKS['exclude'])
+                # about get_cached_trees: https://goo.gl/rFKiku
+                .get_cached_trees()
+            ),
         ),
-        key=lambda x: x.page.position,
+        key=lambda x: x.position,
     )
 
 


Test patch:
diff --git a/shopelectro/tests/tests_templatetags.py b/shopelectro/tests/tests_templatetags.py
--- a/shopelectro/tests/tests_templatetags.py
+++ b/shopelectro/tests/tests_templatetags.py
@@ -1,6 +1,6 @@
 from django.test import TestCase, tag
 
-from shopelectro.models import Category
+from shopelectro.models import CategoryPage
 from shopelectro.templatetags.se_extras import roots
 
 
@@ -12,14 +12,14 @@ class TemplateTags(TestCase):
     def test_root_categories(self):
         """All root categories are presented in result of calling the `roots` tag."""
         self.assertFalse(
-            set(Category.objects.get_cached_trees()) ^ set(roots()),
+            set(CategoryPage.objects.get_cached_trees()) ^ set(roots()),
         )
 
     def test_exclusions_of_root_categories(self):
         """Excluded categories are not presented in result of calling the `roots` tag."""
-        excluded_category, *_ = categories = Category.objects.get_cached_trees()
-        excluded_category.page.is_active = False
-        excluded_category.page.save()
+        excluded_category, *_ = categories = CategoryPage.objects.get_cached_trees()
+        excluded_category.is_active = False
+        excluded_category.save()
 
         self.assertTrue(
             excluded_category in set(categories) ^ set(roots())