Permalink
Browse files

menu_levels working. nav-extenders correctly hiding if not assigned

  • Loading branch information...
1 parent b2d4270 commit bda49b26cd5f1216dfadb756ec3b693348295d3f @digi604 digi604 committed Feb 10, 2010
View
@@ -19,6 +19,8 @@
from django.forms.widgets import HiddenInput
from django.contrib.sites.models import Site
from django.core.exceptions import ValidationError
+from menus.menu_pool import menu_pool
+from django.utils.functional import lazy
class PageAddForm(forms.ModelForm):
@@ -76,6 +78,7 @@ def clean_language(self):
class PageForm(PageAddForm):
+
APPLICATION_URLS = (('', '----------'), ) + settings.CMS_APPLICATIONS_URLS
menu_title = forms.CharField(label=_("Menu Title"), widget=forms.TextInput(),
@@ -98,7 +101,14 @@ class PageForm(PageAddForm):
help_text=_('A description of the page sometimes used by search engines.'))
meta_keywords = forms.CharField(label='Keywords meta tag', max_length=255, required=False,
help_text=_('A list of comma seperated keywords sometimes used by search engines.'))
-
+
+ navigation_extenders = forms.ChoiceField(choices=(), required=False)
+
+ def __init__(self, *args, **kwargs):
+ super(PageForm, self).__init__(*args, **kwargs)
+ # set the nav extenders for the form
+ self.fields['navigation_extenders'].choices = [('', "---------")] + menu_pool.get_cms_enabled_menus()
+
def clean(self):
cleaned_data = super(PageForm, self).clean()
id = cleaned_data['reverse_id']
View
@@ -40,6 +40,7 @@
from django.utils.encoding import force_unicode
from django.utils.text import capfirst
from django.utils.translation import ugettext as _
+from menus.menu_pool import menu_pool
import os
@@ -83,8 +84,8 @@ class PageAdmin(model_admin):
advanced_fields.append('publication_date')
if settings.CMS_SHOW_END_DATE:
advanced_fields.append( 'publication_end_date')
- if settings.CMS_NAVIGATION_EXTENDERS:
- advanced_fields.append('navigation_extenders')
+ #if menu_pool.get_cms_enabled_menus():
+ advanced_fields.append('navigation_extenders')
if settings.CMS_MODERATOR:
additional_hidden_fields.extend(('moderator_state', 'moderator_message'))
if settings.CMS_APPLICATIONS_URLS:
View
@@ -71,7 +71,6 @@ def page_to_node(self, page, home, cut):
parent_id = page.parent_id
if home and page.parent_id == home.pk and cut:
parent_id = None
- print "cut parent",page
attr = {'navigation_extenders':page.navigation_extenders}
n = NavigationNode(page.get_menu_title(),
page.get_absolute_url(),
@@ -87,19 +86,45 @@ def page_to_node(self, page, home, cut):
class NavExtender(Modifier):
- def modify(self, request, nodes, namespace, id, post_cut):
+ def modify(self, request, nodes, namespace, id, post_cut, breadcrumb):
+ if post_cut:
+ return nodes
+ exts = []
+ # rearrange the parent relations
for node in nodes:
ext = node.attr.get("navigation_extenders", None)
if ext:
- found = False
+ if not ext in exts:
+ exts.append(ext)
for n in nodes:
if n.namespace == ext and not n.parent_id:
- found = True
n.parent_id = node.id
n.parent_namespace = node.namespace
n.parent = node
node.children.append(n)
- assert found
+ removed = []
+ # find all not assigned nodes
+ for menu in menu_pool.menus.items():
+ if menu[1].cms_enabled and not menu[0] in exts:
+ for node in nodes:
+ if node.namespace == menu[0]:
+ removed.append(node)
+ if removed:
+ # has home a nav extender and is home not in navigation?
+ page_queryset = get_page_queryset(request)
+ try:
+ home = page_queryset.get_home()
+ except NoHomeFound:
+ home = None
+ if home and not home.in_navigation and home.navigation_extenders:
+ n_removed = removed
+ removed = []
+ for node in n_removed:
+ if node.namespace != home.navigation_extenders:
+ removed.append(node)
+ # remove all nodes that are nav_extenders and not assigned
+ for node in removed:
+ nodes.remove(node)
return nodes
menu_pool.register_modifier(NavExtender)
View
@@ -0,0 +1,10 @@
+from menus.base import Menu
+from django.core.exceptions import ValidationError
+
+class CMSAttachMenu(Menu):
+ cms_enabled = True
+
+ def __init__(self, *args, **kwargs):
+ super(CMSAttachMenu, self).__init__(*args, **kwargs)
+ if self.cms_enabled and not self.name:
+ raise ValidationError("the menu %s is a CMSAttachMenu but has no name defined!" % self.__class__.__name__)
View
@@ -17,6 +17,8 @@
from cms.exceptions import NoHomeFound
from cms.utils.helpers import reversion_register
from cms.utils.i18n import get_fallback_languages
+from menus.menu_pool import menu_pool
+from django.utils.functional import lazy
class Page(MpttPublisher):
"""
@@ -45,7 +47,7 @@ class Page(MpttPublisher):
in_navigation = models.BooleanField(_("in navigation"), default=True, db_index=True)
soft_root = models.BooleanField(_("soft root"), db_index=True, default=False, help_text=_("All ancestors will not be displayed in the navigation"))
reverse_id = models.CharField(_("id"), max_length=40, db_index=True, blank=True, null=True, help_text=_("An unique identifier that is used with the page_url templatetag for linking to this page"))
- navigation_extenders = models.CharField(_("navigation extenders"), max_length=80, db_index=True, blank=True, null=True, choices=settings.CMS_NAVIGATION_EXTENDERS)
+ navigation_extenders = models.CharField(_("navigation extenders"), max_length=80, db_index=True, blank=True, null=True)
published = models.BooleanField(_("is published"), blank=True)
template = models.CharField(_("template"), max_length=100, choices=settings.CMS_TEMPLATES, help_text=_('The template used to render the content.'))
View
@@ -25,6 +25,8 @@
settings.CMS_MENU_TITLE_OVERWRITE = True
settings.CMS_HIDE_UNTRANSLATED = False
settings.CMS_URL_OVERWRITE = True
+if not "example.sampleapp" in settings.INSTALLED_APPS:
+ settings.INSTALLED_APPS = list(settings.INSTALLED_APPS) + ["example.sampleapp"]
def suite():
s = unittest.TestSuite()
View
@@ -138,6 +138,17 @@ def test_10_show_breadcrumb(self):
nodes = show_breadcrumb(context, 1)['ancestors']
self.assertEqual(len(nodes), 0)
+ page1 = Page.objects.get(pk=self.page1.pk)
+ page1.in_navigation = False
+ page1.save()
+ page2 = Page.objects.get(pk=self.page2.pk)
+ context = self.get_context(path=self.page2.get_absolute_url())
+ nodes = show_breadcrumb(context)['ancestors']
+ self.assertEqual(len(nodes), 2)
+ print nodes
+ self.assertEqual(nodes[0].get_absolute_url(), "/")
+ self.assertEqual(nodes[1].get_absolute_url(), page2.get_absolute_url())
+
def test_11_language_chooser(self):
context = self.get_context(path=self.page3.get_absolute_url())
new_context = language_chooser(context)
@@ -189,4 +200,14 @@ def test_15_home_not_in_menu(self):
context = self.get_context()
nodes = show_menu(context, 0, 100, 100, 100)['children']
self.assertEqual(len(nodes), 2)
+
+ def test_15_empty_menu(self):
+ Page.objects.all().delete()
+ request = self.get_request()
+ nodes = menu_pool.get_nodes(request)
+ context = self.get_context()
+ nodes = show_menu(context, 0, 100, 100, 100)['children']
+
+ def test_16_softroot(self):
+ self.assertEqual(1, 2)
View
@@ -75,4 +75,14 @@ def test_04_extenders_on_child(self):
self.assertEqual(len(nodes), 2)
self.assertEqual(len(nodes[0].children), 4)
self.assertEqual(nodes[0].children[1].get_absolute_url(), "/" )
+
+ def test_05_incorrect_nav_extender_in_db(self):
+ self.create_some_nodes()
+ page2 = Page.objects.get(pk=self.page2.pk)
+ page2.navigation_extenders = "SomethingWrong"
+ page2.save()
+ context = self.get_context()
+ nodes = show_menu(context)['children']
+ self.assertEqual(len(nodes), 2)
+
@@ -1,10 +1,10 @@
from cms.app_base import CMSApp
-from example.sampleapp.menu import StaticMenu, CategoryMenu
+from example.sampleapp.menu import SampleAppMenu
from cms.apphook_pool import apphook_pool
class SampleApp(CMSApp):
name = _("Sample App")
- urls = "sampleapp.urls"
- menus = [CategoryMenu, StaticMenu]
+ urls = ["sampleapp.urls"]
+ menus = [SampleAppMenu]
apphook_pool.register(SampleApp)
View
@@ -4,21 +4,15 @@
from menus.menu_pool import menu_pool
from cms.app_base import CMSApp
from django.utils.translation import ugettext_lazy as _
+from cms.menu_bases import CMSAttachMenu
-class CategoryMenu(Menu):
+class SampleAppMenu(Menu):
def get_nodes(self, request):
nodes = []
for cat in Category.objects.all():
n = NavigationNode(cat.name, cat.get_absolute_url(), "sampleapp", cat.pk, cat.parent_id, "sampleapp")
nodes.append(n)
- return nodes
-
-menu_pool.register_menu(CategoryMenu)
-
-class StaticMenu(Menu):
- def get_nodes(self, request):
- nodes = []
try:
n = NavigationNode(_('sample root page'), reverse('sample-root'), 1)
n2 = NavigationNode(_('sample settings page'), reverse('sample-settings'), 2)
@@ -31,8 +25,38 @@ def get_nodes(self, request):
except NoReverseMatch:
pass
return nodes
+
+menu_pool.register_menu(SampleAppMenu)
+
+class StaticMenu(CMSAttachMenu):
+ name = _("Static Menu")
+ def get_nodes(self, request):
+ nodes = []
+ n = NavigationNode('static root page', "/fresh/", 1)
+ n2 = NavigationNode('static settings page', "/bye/", 2)
+ n3 = NavigationNode('static account page', "/hello/", 3)
+ n4 = NavigationNode('static my profile page', "/hello/world/", 4, 3)
+ nodes.append(n)
+ nodes.append(n2)
+ nodes.append(n3)
+ nodes.append(n4)
+ return nodes
menu_pool.register_menu(StaticMenu)
+class StaticMenu2(CMSAttachMenu):
+ name = _("Static Menu2")
+ def get_nodes(self, request):
+ nodes = []
+ n = NavigationNode('static2 root page', "/fresh/", 1)
+ n2 = NavigationNode('static2 settings page', "/bye/", 2)
+ n3 = NavigationNode('static2 account page', "/hello/", 3)
+ n4 = NavigationNode('static2 my profile page', "/hello/world/", 4, 3)
+ nodes.append(n)
+ nodes.append(n2)
+ nodes.append(n3)
+ nodes.append(n4)
+ return nodes
+menu_pool.register_menu(StaticMenu2)
@@ -1,7 +1,7 @@
{% load menu_tags cache %}
{% for child in children %}
<li>
- {% if not child.is_leaf_node %}+{% endif %}{{ child.level }}
+ {% if not child.is_leaf_node %}+{% endif %}{{ child.menu_level }}
<a href="{{ child.get_absolute_url }}">{{ child.get_menu_title }}</a> <span style="color:#999">
{% if child.selected %}selected{% endif %}
{% if child.ancestor %}ancestor{% endif %}
View
@@ -2,13 +2,11 @@
class Menu(object):
- name = None
- cms_enabled = False
- namespace = "main"
+ namespace = None
def __init__(self):
- if self.cms_enabled and not self.name:
- raise ValidationError("the menu %s is cms_enabled but has no name" % self.__class__.__name__)
+ if not self.namespace:
+ self.namespace = self.__class__.__name__
def get_nodes(self, request):
"""
@@ -17,11 +15,6 @@ def get_nodes(self, request):
raise NotImplementedError
class Modifier(object):
- pre_cut = True
- post_cut = False
-
- def set_nodes(self, nodes):
- self.nodes = nodes
def modify(self, request, nodes, namespace, id, post_cut):
pass
@@ -39,6 +32,7 @@ class NavigationNode(object):
parent_namespace = None
parent = None # do not touch
reverse_id = None
+ longer_url_match = False
selected = False
Oops, something went wrong.

0 comments on commit bda49b2

Please sign in to comment.