Added support for handling navigation for view with more than one main object class

This commit is contained in:
Roberto Rosario
2011-07-09 04:31:08 -04:00
parent 1e944c7130
commit ee46653578
4 changed files with 157 additions and 47 deletions

View File

@@ -3,6 +3,7 @@
{% load pagination_tags %}
{% load navigation_tags %}
{% load non_breakable %}
{% load variable_tags %}
{% if side_bar %}
<div class="block">
@@ -122,13 +123,17 @@
{% endif %}
{% endfor %}
{% if not hide_links %}
{% if list_object_variable_name %}
{% copy_variable object as list_object_variable_name %}
{% copy_variable list_object_variable_name as "navigation_object_name" %}
{% endif %}
<td class="last">
{% if navigation_object_links %}
{% with navigation_object_links as overrided_object_links %}
{% object_navigation_template %}
{% endwith %}
{% else %}
{% object_navigation_template %}
{% object_navigation_template %}
{% endif %}
</td>
{% endif %}

View File

@@ -0,0 +1,42 @@
import re
from django.template import Node, TemplateSyntaxError, Library, Variable
register = Library()
class CopyNode(Node):
def __init__(self, source_variable, var_name, delete_old=False):
self.source_variable = source_variable
self.var_name = var_name
self.delete_old = delete_old
def render(self, context):
context[Variable(self.var_name).resolve(context)] = Variable(self.source_variable).resolve(context)
if self.delete_old:
context[Variable(self.source_variable).resolve(context)] = u''
return ''
@register.tag
def copy_variable(parser, token):
return parse_tag(parser, token)
@register.tag
def rename_variable(parser, token):
return parse_tag(parser, token, {'delete_old': True})
def parse_tag(parser, token, *args, **kwargs):
# This version uses a regular expression to parse tag contents.
try:
# Splitting by None == splitting by spaces.
tag_name, arg = token.contents.split(None, 1)
except ValueError:
raise TemplateSyntaxError('%r tag requires arguments' % token.contents.split()[0])
m = re.search(r'(.*?) as ([\'"]*\w+[\'"]*)', arg)
if not m:
raise TemplateSyntaxError('%r tag had invalid arguments' % tag_name)
source_variable, var_name = m.groups()
return CopyNode(source_variable, var_name, *args, **kwargs)

View File

@@ -5,6 +5,8 @@
{% load settings %}
{% load search_tags %}
{% load main_settings_tags %}
{% load variable_tags %}
{% block web_theme_head %}
{% if new_window_url %}
<script type="text/javascript">
@@ -154,23 +156,47 @@
{% endblock %}
{% block web_theme_secondary_navigation %}
{% get_object_navigation_links "form_header" as form_navigation_links %}
{% if form_navigation_links %}
<div class="secondary-navigation">
<ul class="wat-cf">
{% with form_navigation_links as object_navigation_links %}
{% with "true" as as_li %}
{% with "true" as hide_active_anchor %}
{% with "active" as li_class_active %}
{% with "first" as li_class_first %}
{% include "generic_navigation.html" %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
</ul>
</div>
{% if navigation_object_list %}
{% for navigation_object_dict in navigation_object_list %}
{% copy_variable navigation_object_dict.object as "navigation_object_name" %}
{% get_object_navigation_links "form_header" as form_navigation_links %}
{% if form_navigation_links %}
<div class="secondary-navigation">
<ul class="wat-cf">
{% with form_navigation_links as object_navigation_links %}
{% with "true" as as_li %}
{% with "true" as hide_active_anchor %}
{% with "active" as li_class_active %}
{% with "first" as li_class_first %}
{% include "generic_navigation.html" %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
</ul>
</div>
{% endif %}
{% endfor %}
{% else %}
{% get_object_navigation_links "form_header" as form_navigation_links %}
{% if form_navigation_links %}
<div class="secondary-navigation">
<ul class="wat-cf">
{% with form_navigation_links as object_navigation_links %}
{% with "true" as as_li %}
{% with "true" as hide_active_anchor %}
{% with "active" as li_class_active %}
{% with "first" as li_class_first %}
{% include "generic_navigation.html" %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
</ul>
</div>
{% endif %}
{% endif %}
{% endblock %}
@@ -199,27 +225,51 @@
</ul>
</div>
{% endif %}
{% get_object_navigation_links as object_navigation_links %}
{% if object_navigation_links %}
<div class="block">
{% if object %}
{% if object_name %}
<h3>{% blocktrans %}Actions for {{ object_name }}: {{ object }}{% endblocktrans %}</h3>
{% if navigation_object_list %}
{% for navigation_object_dict in navigation_object_list %}
{% copy_variable navigation_object_dict.object as "navigation_object_name" %}
{% get_object_navigation_links as object_navigation_links %}
{% if object_navigation_links %}
<div class="block">
{% if navigation_object %}
{% if navigation_object_dict.name %}
<h3>{% blocktrans with navigation_object_dict.name as name %}Actions for {{ name }}: {{ navigation_object }}{% endblocktrans %}</h3>
{% else %}
<h3>{% blocktrans %}Actions for: {{ navigation_object }}{% endblocktrans %}</h3>
{% endif %}
{% else %}
<h3>{% trans "Available actions" %}</h3>
{% endif %}
<ul class="navigation">
{% with "true" as as_li %}
{% include "generic_navigation.html" %}
{% endwith %}
</ul>
</div>
{% endif %}
{% endfor %}
{% else %}
{% get_object_navigation_links as object_navigation_links %}
{% if object_navigation_links %}
<div class="block">
{% if navigation_object %}
{% if object_name %}
<h3>{% blocktrans %}Actions for {{ object_name }}: {{ navigation_object }}{% endblocktrans %}</h3>
{% else %}
<h3>{% blocktrans %}Actions for: {{ navigation_object }}{% endblocktrans %}</h3>
{% endif %}
{% else %}
<h3>{% blocktrans %}Actions for: {{ object }}{% endblocktrans %}</h3>
<h3>{% trans "Actions" %}</h3>
{% endif %}
{% else %}
<h3>}{% trans "Actions" %}</h3>
{% endif %}
<ul class="navigation">
{% with "true" as as_li %}
{% include "generic_navigation.html" %}
{% endwith %}
</ul>
</div>
<ul class="navigation">
{% with "true" as as_li %}
{% include "generic_navigation.html" %}
{% endwith %}
</ul>
</div>
{% endif %}
{% endif %}
{% get_object_navigation_links "sidebar" as object_navigation_links %}
{% if object_navigation_links %}
<div class="block">

View File

@@ -129,12 +129,31 @@ def resolve_links(context, links, current_view, current_path, parsed_query_strin
for child_url_regex in link.get('children_url_regex', []):
if re.compile(child_url_regex).match(current_path.lstrip('/')):
new_link['active'] = True
new_link['active'] = True
for cls in link.get('children_classes', []):
obj, object_name = get_navigation_object(context)
if type(obj) == cls:
new_link['active'] = True
context_links.append(new_link)
return context_links
def get_navigation_object(context):
try:
object_name = Variable('navigation_object_name').resolve(context)
except VariableDoesNotExist:
object_name = 'object'
try:
obj = Variable(object_name).resolve(context)
except VariableDoesNotExist:
obj = None
return obj, object_name
def _get_object_navigation_links(context, menu_name=None, links_dict=object_navigation):
request = Variable('request').resolve(context)
current_path = request.META['PATH_INFO']
@@ -158,16 +177,6 @@ def _get_object_navigation_links(context, menu_name=None, links_dict=object_navi
except VariableDoesNotExist:
pass
try:
object_name = Variable('navigation_object_name').resolve(context)
except VariableDoesNotExist:
object_name = 'object'
try:
obj = Variable(object_name).resolve(context)
except VariableDoesNotExist:
obj = None
try:
"""
Check for and inject a temporary navigation dictionary
@@ -185,6 +194,8 @@ def _get_object_navigation_links(context, menu_name=None, links_dict=object_navi
except KeyError:
pass
obj, object_name = get_navigation_object(context)
try:
links = links_dict[menu_name][type(obj)]['links']
for link in resolve_links(context, links, current_view, current_path, parsed_query_string):
@@ -216,6 +227,8 @@ class GetNavigationLinks(Node):
def render(self, context):
menu_name = resolve_template_variable(context, self.menu_name)
context[self.var_name] = _get_object_navigation_links(context, menu_name, links_dict=self.links_dict)
obj, object_name = get_navigation_object(context)
context['navigation_object'] = obj
return ''