From ee46653578738b7cdfb2da54799c0271068ebd81 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sat, 9 Jul 2011 04:31:08 -0400 Subject: [PATCH] Added support for handling navigation for view with more than one main object class --- .../templates/generic_list_subtemplate.html | 7 +- apps/common/templatetags/variable_tags.py | 42 ++++++ apps/main/templates/base.html | 120 +++++++++++++----- .../templatetags/navigation_tags.py | 35 +++-- 4 files changed, 157 insertions(+), 47 deletions(-) create mode 100644 apps/common/templatetags/variable_tags.py diff --git a/apps/common/templates/generic_list_subtemplate.html b/apps/common/templates/generic_list_subtemplate.html index 2ac52b2dd7..29213c1d97 100644 --- a/apps/common/templates/generic_list_subtemplate.html +++ b/apps/common/templates/generic_list_subtemplate.html @@ -3,6 +3,7 @@ {% load pagination_tags %} {% load navigation_tags %} {% load non_breakable %} +{% load variable_tags %} {% if side_bar %}
@@ -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 %} {% if navigation_object_links %} {% with navigation_object_links as overrided_object_links %} {% object_navigation_template %} {% endwith %} {% else %} - {% object_navigation_template %} + {% object_navigation_template %} {% endif %} {% endif %} diff --git a/apps/common/templatetags/variable_tags.py b/apps/common/templatetags/variable_tags.py new file mode 100644 index 0000000000..b483834c3a --- /dev/null +++ b/apps/common/templatetags/variable_tags.py @@ -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) diff --git a/apps/main/templates/base.html b/apps/main/templates/base.html index deb49781c8..5c2c4ca43a 100644 --- a/apps/main/templates/base.html +++ b/apps/main/templates/base.html @@ -5,6 +5,8 @@ {% load settings %} {% load search_tags %} {% load main_settings_tags %} +{% load variable_tags %} + {% block web_theme_head %} {% if new_window_url %}