Skip to content

Commit

Permalink
fixes 77 menu items have a new attribute: menu_level
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Lauber committed Jun 18, 2009
1 parent d7f6486 commit cb4604a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 48 deletions.
23 changes: 1 addition & 22 deletions cms/templates/cms/sub_menu.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,2 @@
{% load cms_tags cache %}
{#% cache CMS_CONTENT_CACHE_DURATION menu page.id lang current_page.id %#}
{% for child in children %}
<li>
{% if not child.is_leaf_node %}+{% endif %}{{ child.level }}
<a href="{{ child.get_absolute_url }}">{{ child.get_title }}</a><span style="color:#999">
{% if child.selected %}selected{% endif %}
{% if child.ancestor %}ancestor{% endif %}
{% if child.sibling %}sibling{% endif %}
{% if child.descendant %}descendant{% endif %}
{% if child.soft_root %}(softroot}{% endif %}</span>
{% if child.childrens %}
<ul>
{% show_menu from_level to_level extra_inactive extra_active template child %}
</ul>
{% endif %}
</li>
{% endfor %}
{#% endcache %#}
{% for child in children %}

{% endfor %}
{% include "cms/menu.html" %}

73 changes: 49 additions & 24 deletions cms/templatetags/cms_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
register = template.Library()


def show_menu(context, from_level=0, to_level=100, extra_inactive=0, extra_active=100, template="cms/menu.html", next_page=None, root_page=None):
def show_menu(context, from_level=0, to_level=100, extra_inactive=0, extra_active=100, template="cms/menu.html", next_page=None, root_id=None):
"""
render a nested list of all children of the pages
from_level: is the start level
Expand All @@ -20,7 +20,6 @@ def show_menu(context, from_level=0, to_level=100, extra_inactive=0, extra_activ
"""
request = context['request']
site = Site.objects.get_current()
CMS_CONTENT_CACHE_DURATION = settings.CMS_CONTENT_CACHE_DURATION
lang = get_language_from_request(request)
current_page = request.current_page

Expand Down Expand Up @@ -48,51 +47,69 @@ def show_menu(context, from_level=0, to_level=100, extra_inactive=0, extra_activ
'sites__domain' : site.domain,
'level__lte' : to_level}
#check the ancestors for softroots
soft_root_start = 0
soft_root_pk = None
for p in alist:
ancestors.append(p[0])
if p[1]:
soft_root = Page.objects.get(pk=p[0])
filters['lft__gte'] = soft_root.lft
filters['rght__lte'] = soft_root.rght
filters['tree_id'] = soft_root.tree_id
from_level = soft_root.level
soft_root_start = soft_root.level
if current_page and current_page.soft_root:
filters['tree_id'] = current_page.tree_id
filters['lft__gte'] = current_page.lft
filters['rght__lte'] = current_page.rght
from_level = current_page.level
soft_root_start = current_page.level
if root_page:
soft_root_pk = p[0]
#modify filters if we don't start from the root
root_page = None
if root_id:
root_page = Page.objects.get(reverse_id=root_page)
else:
print current_page
print current_page.soft_root
if current_page and current_page.soft_root:
root_page = current_page
soft_root_pk = current_page.pk
elif soft_root_pk:
root_page = Page.objects.get(pk=soft_root_pk)
if root_page:
print "root page found"
filters['tree_id'] = root_page.tree_id
filters['lft__gt'] = root_page.lft
filters['rght__lt'] = root_page.rght
filters['level__lte'] = root_page.level + to_level
db_from_level = root_page.level + from_level
else:
db_from_level = from_level
if settings.CMS_HIDE_UNTRANSLATED:
filters['title_set__language'] = lang
pages = Page.objects.published().filter(**filters).order_by('tree_id',
'parent',
'lft')
pages = list(pages)
if root_page:
pages = [root_page] + pages
print pages
all_pages = pages[:]
root_level = getattr(root_page, 'level', None)

print root_level
print from_level
print "========="
for page in pages:# build the tree
if page.level >= from_level:

if page.level >= db_from_level:
ids.append(page.pk)
if page.level == 0 or page.level == soft_root_start or page.level-1 == getattr(root_page, 'level', None):
print page.level
if page.level == 0 or page.level == root_level:
print "found"
page.ancestors_ascending = []
page.menu_level = 0 - from_level
page.childrens = []
children.append(page)
if current_page and page.pk == current_page.pk and current_page.soft_root:
if page.pk == soft_root_pk:
page.soft_root = False #ugly hack for the recursive function
if current_page:
pk = current_page.pk
else:
pk = -1
find_children(page, pages, extra_inactive, extra_active, ancestors, pk, request=request, to_levels=to_level)
if current_page and page.pk == current_page.pk and current_page.soft_root:
if page.pk == soft_root_pk:
page.soft_root = True
if from_level > 0:
children = cut_levels(children, from_level)
if db_from_level > 0:
children = cut_levels(children, db_from_level)
titles = list(Title.objects.filter(page__in=ids, language=lang))
for page in all_pages:# add the title and slugs and some meta data
for title in titles:
Expand All @@ -115,8 +132,8 @@ def show_menu(context, from_level=0, to_level=100, extra_inactive=0, extra_activ
show_menu = register.inclusion_tag('cms/dummy.html', takes_context=True)(show_menu)


def show_menu_below_id(context, root_page=None, from_level=0, to_level=100, extra_inactive=0, extra_active=100, template_file="cms/menu.html", next_page=None):
return show_menu(context, from_level, to_level, extra_inactive, extra_active, template_file, next_page, root_page=root_page)
def show_menu_below_id(context, root_id=None, from_level=0, to_level=100, extra_inactive=0, extra_active=100, template_file="cms/menu.html", next_page=None):
return show_menu(context, from_level, to_level, extra_inactive, extra_active, template_file, next_page, root_id=root_id)
register.inclusion_tag('cms/dummy.html', takes_context=True)(show_menu_below_id)


Expand All @@ -143,11 +160,19 @@ def show_sub_menu(context, levels=100, template="cms/sub_menu.html"):
pages = list(pages)
all_pages = pages[:]
page.ancestors_ascending = []
page.childrens = []
for p in pages:
p.descendant = True
ids.append(p.pk)
page.selected = True
page.menu_level = -1
was_soft_root = False
if page.soft_root:
was_soft_root = True
page.soft_root = False
find_children(page, pages, levels, levels, [], page.pk, request=request)
if was_soft_root:
page.soft_root = True
children = page.childrens
titles = Title.objects.filter(page__in=ids, language=lang)
for p in all_pages:# add the title and slugs and some meta data
Expand Down
1 change: 1 addition & 0 deletions cms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def find_children(target, pages, levels=100, active_levels=0, ancestors=None, se
page.descendant = True
if len(target.childrens):
target.childrens[-1].last = False
page.menu_level = target.menu_level + 1
page.ancestors_ascending = list(target.ancestors_ascending) + [target]
page.last = True
target.childrens.append(page)
Expand Down
1 change: 0 additions & 1 deletion example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@
CMS_NAVIGATION_EXTENDERS = (('example.categories.navigation.get_nodes', 'Categories'),)

CMS_SOFTROOT = True
CMS_FLAT_URLS = True
CMS_REDIRECTS = True
CMS_SEO_FIELDS = True
CMS_MENU_TITLE_OVERWRITE = True
Expand Down
18 changes: 18 additions & 0 deletions example/templates/cms/menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% load cms_tags cache %}
{% for child in children %}
<li>
{% 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 %}
{% if child.sibling %}sibling{% endif %}
{% if child.descendant %}descendant{% endif %}
{% if child.soft_root %}(softroot){% endif %}
(Level: {{ child.level }})</span>
{% if child.childrens %}
<ul>
{% show_menu from_level to_level extra_inactive extra_active template child %}
</ul>
{% endif %}
</li>
{% endfor %}
3 changes: 3 additions & 0 deletions example/templates/cms/sub_menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% include "cms/menu.html" %}


2 changes: 1 addition & 1 deletion mptt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def get_root(self):
"""
if self.is_root_node():
return self

opts = self._meta
return self._default_manager.get(**{
opts.tree_id_attr: getattr(self, opts.tree_id_attr),
Expand Down

1 comment on commit cb4604a

@philippbosch
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

templatetags/cms_tags.py:58 should be changed to

root_page = Page.objects.get(reverse_id=root_id)

was:

root_page = Page.objects.get(reverse_id=root_page)

Please sign in to comment.