Skip to content

Commit

Permalink
Added has_children_in_menu and num_children_in_menu attributes to pag…
Browse files Browse the repository at this point in the history
…e objects in the page_menu template tag, for determining valid children in the context of a menu and the in_emnus field. Closes stephenmcd#413.
  • Loading branch information
stephenmcd committed Oct 1, 2012
1 parent f484d5f commit 7198f76
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 32 deletions.
16 changes: 10 additions & 6 deletions docs/content-architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,21 +345,25 @@ helping you to build advanced menus.
* ``branch_level`` - an integer for the current branch depth
* ``page_branch_in_menu`` - a boolean for whether this branch should
be in the menu (see "filtering menus" below)
* ``page.has_children`` - a boolean for whether the branch page has
any child pages
* ``page.num_children`` - an integer for the number of child pages the
branch page has
* ``page.in_menu`` - a boolean for whether the branch page should
be in the menu (see "filtering menus" below)
* ``page.has_children`` - a boolean for whether the branch page has
any child pages at all, disregarding the current menu
* ``page.has_children_in_menu`` - a boolean for whether the branch
page has any child pages that appear in the current menu
* ``page.num_children`` - an integer for the number of child pages the
branch page has in total, disregarding the current menu
* ``page.num_children_in_menu`` - an integer for the number of child
pages the branch page has, that also appear in the current menu
* ``page.is_current_child`` - a boolean for whether the branch page
is a child of the current page being viewed
* ``page.is_current_sibling`` - a boolean for whether the branch page
is a sibling (has the same parent) of the current page being viewed
* ``page.is_current_or_ascendant`` - a boolean for whether the branch
page is the current page being viewed, or an ascendant (parent,
grand-parent, etc) of the current page being viewed
* ``page.is_current_sibling`` - a boolean for whether the branch page
is a parimary page (has no parent)
* ``page.is_primary`` - a boolean for whether the branch page
is a primary page (has no parent)
* ``page.html_id`` - a unique string that can be used as the HTML ID
attribute
* ``page.branch_level`` - an integer for the branch page's depth
Expand Down
8 changes: 8 additions & 0 deletions mezzanine/pages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _

from mezzanine.conf import settings
from mezzanine.core.models import Displayable, Orderable, RichText
from mezzanine.pages.fields import MenusField
from mezzanine.pages.managers import PageManager
Expand Down Expand Up @@ -206,6 +207,13 @@ def is_c_or_a(page_id):
# Default branch level - gets assigned in the page_menu tag.
self.branch_level = 0

def in_menu_template(self, template_name):
if self.in_menus is not None:
for i, l, t in settings.PAGE_MENU_TEMPLATES:
if not unicode(i) in self.in_menus and t == template_name:
return False
return True


class RichTextPage(Page, RichText):
"""
Expand Down
4 changes: 2 additions & 2 deletions mezzanine/pages/templates/pages/menus/dropdown.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
id="dropdown-menu-{{ page.html_id }}">
<a href="{{ page.get_absolute_url }}">
{{ page.title }}
{% if page.has_children %}<span class="caret"></span>{% endif %}
{% if page.has_children_in_menu %}<span class="caret"></span>{% endif %}
</a>
{% if page.has_children %}{% page_menu page %}{% endif %}
{% if page.has_children_in_menu %}{% page_menu page %}{% endif %}
</li>
<li class="divider-vertical"></li>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion mezzanine/pages/templates/pages/menus/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<a href="{{ page.get_absolute_url }}">{{ page.title }}</a></li>
{% endif %}

{% if page.in_menu and page.has_children %}{% page_menu page %}{% endif %}
{% if page.in_menu and page.has_children_in_menu %}{% page_menu page %}{% endif %}

{% if page.in_menu %}
{% if forloop.first and not page.parent.in_menu and not page.is_primary %}
Expand Down
2 changes: 1 addition & 1 deletion mezzanine/pages/templates/pages/menus/footer_tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<a href="{{ page.get_absolute_url }}">{{ page.title }}</a>
{# remove this if tag to always show all nav items #}
{% if settings.PAGES_MENU_SHOW_ALL or page.is_current_or_ascendant %}
{% if page.has_children %}{% page_menu page %}{% endif %}
{% if page.has_children_in_menu %}{% page_menu page %}{% endif %}
{% endif %}
</li>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion mezzanine/pages/templates/pages/menus/mobile.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% endif %}

{% if page.is_current_or_ascendant and not page.is_current_child %}
{% if page.has_children %}{% page_menu page %}{% endif %}
{% if page.has_children_in_menu %}{% page_menu page %}{% endif %}
{% endif %}

{% if page.is_current_child %}
Expand Down
2 changes: 1 addition & 1 deletion mezzanine/pages/templates/pages/menus/tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<a href="{{ page.get_absolute_url }}">{{ page.title }}</a>
{# remove this if tag to always show all nav items #}
{% if settings.PAGES_MENU_SHOW_ALL or page.is_current_or_ascendant %}
{% if page.has_children %}{% page_menu page %}{% endif %}
{% if page.has_children_in_menu %}{% page_menu page %}{% endif %}
{% endif %}
</li>
{% endif %}
Expand Down
45 changes: 25 additions & 20 deletions mezzanine/pages/templatetags/pages_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django.template.loader import get_template
from django.utils.translation import ugettext_lazy as _

from mezzanine.conf import settings
from mezzanine.pages.models import Page
from mezzanine.utils.urls import admin_url
from mezzanine import template
Expand Down Expand Up @@ -89,30 +88,36 @@ def page_menu(context, token):
context["branch_level"] = getattr(parent_page, "branch_level", 0) + 1
parent_page_id = parent_page.id

# Build the ``page_branch`` template variable, which is the list of
# pages for the current parent. Here we also assign the attributes
# to the page object that determines whether it belongs in the
# current menu template being rendered.
context["page_branch"] = context["menu_pages"].get(parent_page_id, [])
context["page_branch_in_menu"] = False
for page in context["page_branch"]:
# footer/nav for backward compatibility. Also check that in_menus
# has a value, as it may not have been populated correctly
# if migrations weren't run when it was added.
page.in_footer = page.in_navigation = page.in_menu = True
if page.in_menus is not None:
for i, l, t in settings.PAGE_MENU_TEMPLATES:
if not unicode(i) in page.in_menus and t == template_name:
page.in_navigation = page.in_menu = False
if "footer" in template_name:
page.in_footer = False
break
page.in_menu = page.in_menu_template(template_name)
page.num_children_in_menu = 0
if page.in_menu:
context["page_branch_in_menu"] = True
# Backwards compatibility
context['page_branch_in_navigation'] = context["page_branch_in_menu"]
context['page_branch_in_footer'] = (context["page_branch_in_menu"] and
template_name == "pages/menu/footer.html")

for i, page in enumerate(context["page_branch"]):
context["page_branch"][i].branch_level = context["branch_level"]
context["page_branch"][i].parent = parent_page
for child in context["menu_pages"].get(page.id, []):
if child.in_menu_template(template_name):
page.num_children_in_menu += 1
page.has_children_in_menu = page.num_children_in_menu > 0
page.branch_level = context["branch_level"]
page.parent = parent_page

# Prior to pages having the ``in_menus`` field, pages had two
# boolean fields ``in_navigation`` and ``in_footer`` for
# controlling menu inclusion. Attributes and variables
# simulating these are maintained here for backwards
# compatibility in templates, but will be removed eventually.
page.in_navigation = page.in_menu
page.in_footer = not (not page.in_menu and "footer" in template_name)
if page.in_navigation:
context["page_branch_in_navigation"] = True
if page.in_footer:
context["page_branch_in_footer"] = True

t = get_template(template_name)
return t.render(context)

Expand Down

0 comments on commit 7198f76

Please sign in to comment.