From 69c354b11621065dd4ed4f010ed2a31240c19a6c Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Tue, 28 Jun 2011 01:05:57 -0400 Subject: [PATCH] Merged conditional display code from the navigation template into the navigation tag, added support for conditional disabling of links --- .../templates/generic_link_instance.html | 6 +- .../templates/generic_navigation.html | 2 + .../templates/generic_subnavigation.html | 19 ++-- .../templatetags/navigation_tags.py | 92 +++++++++---------- 4 files changed, 56 insertions(+), 63 deletions(-) diff --git a/apps/navigation/templates/generic_link_instance.html b/apps/navigation/templates/generic_link_instance.html index 42cbf4916b..92b5db3c72 100644 --- a/apps/navigation/templates/generic_link_instance.html +++ b/apps/navigation/templates/generic_link_instance.html @@ -1 +1,5 @@ -{% if link.famfam %}{% endif %}{{ link.text|capfirst }}{% if link.error %} - {{ link.error }}{% endif %}{% if link.active and not hide_active_anchor %}{% endif %}{% if horizontal %}{% if not forloop.last %} | {% endif %}{% endif %} +{% if link.disabled %} + {% if link.famfam %}{% endif %}{{ link.text|capfirst }}{% if link.error %} - {{ link.error }}{% endif %}{% if link.active and not hide_active_anchor %}{% endif %}{% if horizontal %}{% if not forloop.last %} | {% endif %}{% endif %} +{% else %} + {% if link.famfam %}{% endif %}{{ link.text|capfirst }}{% if link.error %} - {{ link.error }}{% endif %}{% if link.active and not hide_active_anchor %}{% endif %}{% if horizontal %}{% if not forloop.last %} | {% endif %}{% endif %} +{% endif %} diff --git a/apps/navigation/templates/generic_navigation.html b/apps/navigation/templates/generic_navigation.html index 87e15234f7..a806b642d1 100644 --- a/apps/navigation/templates/generic_navigation.html +++ b/apps/navigation/templates/generic_navigation.html @@ -1,5 +1,7 @@ {% for link in object_navigation_links %} {% include "generic_subnavigation.html" %} +{% empty %} +none {% endfor %} diff --git a/apps/navigation/templates/generic_subnavigation.html b/apps/navigation/templates/generic_subnavigation.html index cb2fcaac0b..cac3301f00 100644 --- a/apps/navigation/templates/generic_subnavigation.html +++ b/apps/navigation/templates/generic_subnavigation.html @@ -2,19 +2,16 @@ {% load navigation_tags %} {% with link.permissions as permissions %} -{% with link.condition as condition %} - {% check_permissions request.user permissions %} {% if permission %} - {% evaluate_link condition as conditional_display %} - {% if conditional_display %} - {% if as_li %} -
  • - {% endif %} - {% include "generic_link_instance.html" %} - {% if as_li %}
  • {% endif %} + {% if as_li %} +
  • {% endif %} + + {% include "generic_link_instance.html" %} + + {% if as_li %} +
  • + {% endif %} {% endif %} - -{% endwith %} {% endwith %} diff --git a/apps/navigation/templatetags/navigation_tags.py b/apps/navigation/templatetags/navigation_tags.py index 612bb9cff7..8740f97699 100644 --- a/apps/navigation/templatetags/navigation_tags.py +++ b/apps/navigation/templatetags/navigation_tags.py @@ -70,39 +70,55 @@ def resolve_arguments(context, src_args): def resolve_links(context, links, current_view, current_path, parsed_query_string=None): + """ + Express a list of links from definition to final values + """ context_links = [] for link in links: - new_link = copy.copy(link) - try: - args, kwargs = resolve_arguments(context, link.get('args', {})) - except VariableDoesNotExist: - args = [] - kwargs = {} - - if 'view' in link: - new_link['active'] = link['view'] == current_view - + # Check to see if link has conditional display + if 'condition' in link: + condition_result = link['condition'](context) + else: + condition_result = True + + if condition_result: + new_link = copy.copy(link) try: + args, kwargs = resolve_arguments(context, link.get('args', {})) + except VariableDoesNotExist: + args = [] + kwargs = {} + + if 'view' in link: + new_link['active'] = link['view'] == current_view + + try: + if kwargs: + new_link['url'] = reverse(link['view'], kwargs=kwargs) + else: + new_link['url'] = reverse(link['view'], args=args) + if link.get('keep_query', False): + new_link['url'] = urlquote(new_link['url'], parsed_query_string) + except NoReverseMatch, err: + new_link['url'] = '#' + new_link['error'] = err + elif 'url' in link: + new_link['active'] = link['url'] == current_path if kwargs: - new_link['url'] = reverse(link['view'], kwargs=kwargs) + new_link['url'] = link['url'] % kwargs else: - new_link['url'] = reverse(link['view'], args=args) + new_link['url'] = link['url'] % args if link.get('keep_query', False): new_link['url'] = urlquote(new_link['url'], parsed_query_string) - except NoReverseMatch, err: - new_link['url'] = '#' - new_link['error'] = err - elif 'url' in link: - new_link['active'] = link['url'] == current_path - if kwargs: - new_link['url'] = link['url'] % kwargs else: - new_link['url'] = link['url'] % args - if link.get('keep_query', False): - new_link['url'] = urlquote(new_link['url'], parsed_query_string) - else: - new_link['active'] = False - context_links.append(new_link) + new_link['active'] = False + + if 'conditional_disable' in link: + new_link['disabled'] = link['conditional_disable'](context) + else: + new_link['disabled'] = False + + context_links.append(new_link) return context_links @@ -241,29 +257,3 @@ def get_sidebar_templates(parser, token): menu_name, var_name = m.groups() return GetSidebarTemplatesNone(var_name=var_name) - - -class EvaluateLinkNone(Node): - def __init__(self, condition, var_name): - self.condition = condition - self.var_name = var_name - - def render(self, context): - condition = Variable(self.condition).resolve(context) - if condition: - context[self.var_name] = condition(Context(context)) - return u'' - else: - context[self.var_name] = True - return u'' - - -@register.tag -def evaluate_link(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) - - condition, var_name = m.groups() - return EvaluateLinkNone(condition=condition, var_name=var_name)