Skip to content
Browse files

some start of the menu discover infrastructure

  • Loading branch information...
1 parent aa3b5dc commit 589de2cafe1f7d8b36d78a27f0b561dbba3f5cf8 @digi604 digi604 committed
Showing with 121 additions and 8 deletions.
  1. +50 −1 cms/menu.py
  2. +16 −0 menu/base.py
  3. +2 −0 menu/exceptions.py
  4. +25 −0 menu/menu.py
  5. +28 −7 menu/menu_pool.py
View
51 cms/menu.py
@@ -1 +1,50 @@
-from menu.menu_pool import menu_pool
+from menu.menu_pool import menu_pool
+from menu.base import Menu
+from cms.utils import get_language_from_request
+from cms.utils.moderator import get_page_queryset, get_title_queryset
+from django.conf import settings
+from django.contrib.sites.models import Site
+from cms.utils.i18n import get_fallback_languages
+
+class CMSMenu(Menu):
+
+ def get_nodes(self, request):
+ page_queryset = get_page_queryset(request)
+ site = Site.objects.get_current()
+ lang = get_language_from_request(request)
+ filters = {
+ 'in_navigation':True,
+ 'site':site,
+ }
+ if settings.CMS_HIDE_UNTRANSLATED:
+ filters['title_set__language'] = lang
+ if not request.user.is_authenticated():
+ filters['menu_login_required'] = False
+ pages = page_queryset.published().filter(filters)
+ ids = []
+ for page in pages:
+ ids.append(page.id)
+ titles = list(get_title_queryset(request).filter(page__in=ids, language=lang))
+ for page in pages:# add the title and slugs and some meta data
+ for title in titles:
+ if title.page_id == page.pk:
+ if not hasattr(page, "title_cache"):
+ page.title_cache = {}
+ page.title_cache[title.language] = title
+ ids.remove(page.pk)
+ if ids: # get fallback languages
+ fallbacks = get_fallback_languages(lang)
+ for l in fallbacks:
+ titles = list(get_title_queryset(request).filter(page__in=ids, language=l))
+ for title in titles:
+ for page in pages:# add the title and slugs and some meta data
+ if title.page_id == page.pk:
+ if not hasattr(page, "title_cache"):
+ page.title_cache = {}
+ page.title_cache[title.language] = title
+ ids.remove(page.pk)
+ break
+ if not ids:
+ break
+
+menu_pool.register_menu(CMSMenu, "main")
View
16 menu/base.py
@@ -1,4 +1,20 @@
+
+class Menu(object):
+ nodes = []
+
+ def add_nodes(self, nodes):
+ self.nodes.append(nodes)
+
+ def get_nodes(self, request):
+ raise NotImplementedError
+
+class Modifier(object):
+
+ def modify(self, request, nodes, namespace):
+ raise NotImplementedError
+
+
class NavigationNode(object):
title = None
url = None
View
2 menu/exceptions.py
@@ -0,0 +1,2 @@
+class NamespaceAllreadyRegistered(Exception):
+ pass
View
25 menu/menu.py
@@ -0,0 +1,25 @@
+from menu.base import Modifier
+
+
+class Marker(Modifier):
+
+ def modify(self, request, nodes, namespace):
+ selected = None
+ for node in nodes:
+ if node.get_absolute_url() == request.path:
+ node.selected = True
+ n = node
+ while n.parent:
+ n = n.parent
+ n.ancestor = True
+ if node.parent.selected:
+ node.descendants = True
+ if selected and node.parent == selected.parent:
+ node.sibling = True
+
+
+class Cutter(Modifier):
+
+ def modify(self, request, nodes, namespace):
+ for node in nodes:
+
View
35 menu/menu_pool.py
@@ -1,10 +1,31 @@
+from django.conf import settings
+from menu.exceptions import NamespaceAllreadyRegistered
+from menu.base import Menu
-menu_pool = {
- "main":[]
-}
-class Menu(object):
- nodes = []
+class MenuPool(object):
+ def __init__(self):
+ self.menus = {}
+ self.modifiers = []
+ self.discovered = False
+
+ def discover_plugins(self):
+ if self.discovered:
+ return
+ for app in settings.INSTALLED_APPS:
+ __import__(app, {}, {}, ['menu'])
+ self.discovered = True
- def add_nodes(self, nodes):
- self.nodes.append(nodes)
+ def register_menu(self, menu, namespace):
+ assert issubclass(menu, Menu)
+ if namespace in self.menus.keys():
+ raise NamespaceAllreadyRegistered, "[%s] a menu namespace with this name is already registered" % namespace
+ self.menus[namespace] = menu
+
+ def register_extender(self, menu, namespace, parent_id):
+ pass
+
+ def register_modifier(self, modifier):
+ pass
+
+menu_pool = MenuPool()

0 comments on commit 589de2c

Please sign in to comment.
Something went wrong with that request. Please try again.