From 5084f7c87108ad4834f4d13e167b33d55512d885 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Mon, 18 Apr 2011 04:30:36 -0400 Subject: [PATCH] Added support for per-view side bar templates --- apps/main/templates/base.html | 9 ++++++- apps/navigation/api.py | 9 +++++++ .../templatetags/navigation_tags.py | 25 ++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/apps/main/templates/base.html b/apps/main/templates/base.html index 9b9cfb9da5..e1de9223f7 100644 --- a/apps/main/templates/base.html +++ b/apps/main/templates/base.html @@ -143,7 +143,7 @@ {% endwith %} {% endwith %} {% endif %} - + {% get_object_navigation_links as object_navigation_links %} {% if object_navigation_links %}
@@ -176,6 +176,13 @@
{% endif %} + {% get_sidebar_templates as sidebar_templates %} + {% for template in sidebar_templates %} + {% with "true" as side_bar %} + {% include template %} + {% endwith %} + {% endfor %} + {% block sidebar %}{% endblock %} {% endblock %} diff --git a/apps/navigation/api.py b/apps/navigation/api.py index 45bd965ab7..633a3fff0f 100644 --- a/apps/navigation/api.py +++ b/apps/navigation/api.py @@ -4,6 +4,7 @@ object_navigation = {} multi_object_navigation = {} menu_links = [] model_list_columns = {} +sidebar_templates = {} def register_multi_item_links(src, links, menu_name=None): @@ -62,3 +63,11 @@ def register_model_list_columns(model, columns): model_list_columns[model].extend(columns) else: model_list_columns[model] = copy.copy(columns) + + +def register_sidebar_template(source_list, template_name): + for source in source_list: + if source in sidebar_templates: + sidebar_templates[source].append(template_name) + else: + sidebar_templates[source] = [template_name] diff --git a/apps/navigation/templatetags/navigation_tags.py b/apps/navigation/templatetags/navigation_tags.py index 1bd8b65111..a8260137e5 100644 --- a/apps/navigation/templatetags/navigation_tags.py +++ b/apps/navigation/templatetags/navigation_tags.py @@ -8,7 +8,7 @@ from django.utils.text import unescape_string_literal from django.utils.translation import ugettext as _ from navigation.api import object_navigation, multi_object_navigation, \ - menu_links as menu_navigation + menu_links as menu_navigation, sidebar_templates from navigation.forms import MultiItemForm from navigation.utils import resolve_to_name @@ -222,3 +222,26 @@ def get_multi_item_links_form(context): 'submit_method': 'get', }) return new_context + + +class GetSidebarTemplatesNone(Node): + def __init__(self, var_name='sidebar_templates'): + self.var_name = var_name + + def render(self, context): + request = Variable('request').resolve(context) + view_name = resolve_to_name(request.META['PATH_INFO']) + context[self.var_name] = sidebar_templates.get(view_name, []) + return '' + + +@register.tag +def get_sidebar_templates(parser, token): + tag_name, arg = token.contents.split(None, 1) + + m = re.search(r'("?\w+"?)?.?as (\w+)', arg) + if not m: + raise TemplateSyntaxError("%r tag had invalid arguments" % tag_name) + + menu_name, var_name = m.groups() + return GetSidebarTemplatesNone(var_name=var_name)