Rename variable for readability
This commit is contained in:
@@ -21,7 +21,7 @@ multi_object_navigation = {}
|
||||
model_list_columns = {}
|
||||
sidebar_templates = {}
|
||||
|
||||
link_binding = {}
|
||||
bound_links = {}
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -155,10 +155,10 @@ def bind_links(sources, links, menu_name=None, position=0):
|
||||
"""
|
||||
Associate a link to a model, a view, or an url
|
||||
"""
|
||||
link_binding.setdefault(menu_name, {})
|
||||
bound_links.setdefault(menu_name, {})
|
||||
for source in sources:
|
||||
link_binding[menu_name].setdefault(source, {'links': []})
|
||||
link_binding[menu_name][source]['links'].extend(links)
|
||||
bound_links[menu_name].setdefault(source, {'links': []})
|
||||
bound_links[menu_name][source]['links'].extend(links)
|
||||
|
||||
|
||||
def register_top_menu(name, link, position=None):
|
||||
@@ -204,7 +204,7 @@ def register_multi_item_links(sources, links, menu_name=None):
|
||||
multi_object_navigation[menu_name][source]['links'].extend(links)
|
||||
|
||||
|
||||
def get_context_navigation_links(context, menu_name=None, links_dict=link_binding):
|
||||
def get_context_navigation_links(context, menu_name=None, links_dict=bound_links):
|
||||
request = Variable('request').resolve(context)
|
||||
current_path = request.META['PATH_INFO']
|
||||
current_view = resolve_to_name(current_path)
|
||||
|
||||
@@ -3,15 +3,13 @@ from __future__ import absolute_import
|
||||
import copy
|
||||
import re
|
||||
import logging
|
||||
#import urlparse
|
||||
#import urllib
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.template import (TemplateSyntaxError, Library,
|
||||
Node, Variable, VariableDoesNotExist)
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from ..api import (link_binding, multi_object_navigation,
|
||||
from ..api import (bound_links, multi_object_navigation,
|
||||
sidebar_templates, get_context_navigation_links)
|
||||
from ..forms import MultiItemForm
|
||||
from ..utils import resolve_to_name, resolve_template_variable
|
||||
@@ -32,187 +30,8 @@ def get_top_menu_links(parser, token):
|
||||
return TopMenuNavigationNode()
|
||||
|
||||
|
||||
'''
|
||||
def resolve_arguments(context, src_args):
|
||||
args = []
|
||||
kwargs = {}
|
||||
if type(src_args) == type([]):
|
||||
for i in src_args:
|
||||
val = resolve_template_variable(context, i)
|
||||
if val:
|
||||
args.append(val)
|
||||
elif type(src_args) == type({}):
|
||||
for key, value in src_args.items():
|
||||
val = resolve_template_variable(context, value)
|
||||
if val:
|
||||
kwargs[key] = val
|
||||
else:
|
||||
val = resolve_template_variable(context, src_args)
|
||||
if val:
|
||||
args.append(val)
|
||||
|
||||
return args, kwargs
|
||||
|
||||
|
||||
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:
|
||||
# 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:
|
||||
if not link.get('dont_mark_active', False):
|
||||
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):
|
||||
print 'parsed_query_string', parsed_query_string
|
||||
new_link['url'] = urlquote(new_link['url'], parsed_query_string)
|
||||
except NoReverseMatch, err:
|
||||
new_link['url'] = '#'
|
||||
new_link['error'] = err
|
||||
elif 'url' in link:
|
||||
if not link.get('dont_mark_active', False):
|
||||
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
|
||||
|
||||
if 'conditional_highlight' in link:
|
||||
new_link['active'] = link['conditional_highlight'](context)
|
||||
|
||||
if 'conditional_disable' in link:
|
||||
new_link['disabled'] = link['conditional_disable'](context)
|
||||
else:
|
||||
new_link['disabled'] = False
|
||||
|
||||
if current_view in link.get('children_views', []):
|
||||
new_link['active'] = True
|
||||
|
||||
for child_url_regex in link.get('children_url_regex', []):
|
||||
if re.compile(child_url_regex).match(current_path.lstrip('/')):
|
||||
new_link['active'] = True
|
||||
|
||||
for children_view_regex in link.get('children_view_regex', []):
|
||||
if re.compile(children_view_regex).match(current_view):
|
||||
new_link['active'] = True
|
||||
|
||||
for cls in link.get('children_classes', []):
|
||||
obj, object_name = get_navigation_object(context)
|
||||
if type(obj) == cls or 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']
|
||||
current_view = resolve_to_name(current_path)
|
||||
context_links = []
|
||||
|
||||
# Don't fudge with the original global dictionary
|
||||
links_dict = links_dict.copy()
|
||||
|
||||
# Preserve unicode data in URL query
|
||||
previous_path = smart_unicode(urllib.unquote_plus(smart_str(request.get_full_path()) or smart_str(request.META.get('HTTP_REFERER', u'/'))))
|
||||
query_string = urlparse.urlparse(previous_path).query
|
||||
parsed_query_string = urlparse.parse_qs(query_string)
|
||||
|
||||
try:
|
||||
"""
|
||||
Override the navigation links dictionary with the provided
|
||||
link list
|
||||
"""
|
||||
navigation_object_links = Variable('overrided_object_links').resolve(context)
|
||||
if navigation_object_links:
|
||||
return [link for link in resolve_links(context, navigation_object_links, current_view, current_path, parsed_query_string)]
|
||||
except VariableDoesNotExist:
|
||||
pass
|
||||
|
||||
try:
|
||||
"""
|
||||
Check for and inject a temporary navigation dictionary
|
||||
"""
|
||||
temp_navigation_links = Variable('temporary_navigation_links').resolve(context)
|
||||
if temp_navigation_links:
|
||||
links_dict.update(temp_navigation_links)
|
||||
except VariableDoesNotExist:
|
||||
pass
|
||||
|
||||
try:
|
||||
links = links_dict[menu_name][current_view]['links']
|
||||
for link in resolve_links(context, links, current_view, current_path, parsed_query_string):
|
||||
context_links.append(link)
|
||||
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):
|
||||
context_links.append(link)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
return context_links
|
||||
|
||||
|
||||
def resolve_template_variable(context, name):
|
||||
try:
|
||||
return unescape_string_literal(name)
|
||||
except ValueError:
|
||||
#return Variable(name).resolve(context)
|
||||
#TODO: Research if should return always as a str
|
||||
return str(Variable(name).resolve(context))
|
||||
except TypeError:
|
||||
return name
|
||||
|
||||
|
||||
'''
|
||||
|
||||
class GetNavigationLinks(Node):
|
||||
def __init__(self, menu_name=None, links_dict=link_binding, var_name='object_navigation_links'):
|
||||
def __init__(self, menu_name=None, links_dict=bound_links, var_name='object_navigation_links'):
|
||||
self.menu_name = menu_name
|
||||
self.links_dict = links_dict
|
||||
self.var_name = var_name
|
||||
|
||||
Reference in New Issue
Block a user