Updated all link references to the new class based links

This commit is contained in:
Roberto Rosario
2012-03-16 16:20:09 -04:00
parent d028f54c27
commit 568611927b
11 changed files with 137 additions and 202 deletions

View File

@@ -1,7 +1,12 @@
from __future__ import absolute_import
import urlparse
import urllib
import logging
from django.template import (TemplateSyntaxError, Library,
VariableDoesNotExist, Node, Variable)
from django.utils.encoding import smart_str, force_unicode, smart_unicode
from common.utils import urlquote
@@ -16,21 +21,28 @@ top_menu_entries = []
link_binding = {}
logger = logging.getLogger(__name__)
class ResolvedLink(object):
active = False
class Link(object):
def __init__(self, text, view, args=None, kwargs=None, sprite=None, icon=None, permissions=None, condition=None):
def __init__(self, text, view, klass=None, args=None, sprite=None, icon=None, permissions=None, condition=None, conditional_disable=None, description=None, dont_mark_active=False, children_view_regex=None, keep_query=False):
self.text = text
self.view = view
self.args = args or []
self.kwargs = kwargs or {}
self.args = args or {}
#self.kwargs = kwargs or {}
self.sprite = sprite
self.icon = icon
self.permissions = permissions or []
self.condition = condition
self.conditional_disable = conditional_disable
self.description = description
self.dont_mark_active = dont_mark_active
self.children_view_regex = children_view_regex
self.klass = klass
self.keep_query = keep_query
def resolve(self, context):
request = Variable('request').resolve(context)
@@ -52,62 +64,63 @@ class Link(object):
#new_link = {}#copy.copy(link)
resolved_link = ResolvedLink()
try:
args, kwargs = resolve_arguments(context, link.get('args', {}))
#args, kwargs = resolve_arguments(context, self.get('args', {}))
args, kwargs = resolve_arguments(context, self.args)
except VariableDoesNotExist:
args = []
kwargs = {}
if 'view' in link:
if not link.get('dont_mark_active', False):
if self.view:
if not self.dont_mark_active:
#new_link['active'] = link['view'] == current_view
resolved_link.active = link['view'] == current_view
resolved_link.active = self.view == current_view
try:
if kwargs:
#new_link['url'] = reverse(link['view'], kwargs=kwargs)
resolved_link.url = reverse(link['view'], kwargs=kwargs)
resolved_link.url = reverse(self.view, kwargs=kwargs)
else:
# new_link['url'] = reverse(link['view'], args=args)
resolved_link.url = reverse(link['view'], args=args)
if link.get('keep_query', False):
resolved_link.url = reverse(self.view, args=args)
if self.keep_query:
#print 'parsed_query_string', parsed_query_string
#new_link['url'] = urlquote(new_link['url'], parsed_query_string)
resolved_link.url = urlquote(new_link['url'], parsed_query_string)
resolved_link.url = urlquote(resolved_link.url, parsed_query_string)
except NoReverseMatch, exc:
#new_link['url'] = '#'
resolved_link.url = '#'
#new_link['error'] = err
resolved_link.error = exc
elif 'url' in link:
if not link.get('dont_mark_active', False):
elif self.url:
if not self.dont_mark_active:
#new_link['active'] = link['url'] == current_path
resolved_link.url.active = link['url'] == current_path
resolved_link.url.active = self.url == current_path
if kwargs:
#new_link['url'] = link['url'] % kwargs
resolved_link.url = link['url'] % kwargs
resolved_link.url = self.url % kwargs
else:
#new_link['url'] = link['url'] % args
resolved_link.url = link['url'] % args
if link.get('keep_query', False):
resolved_link.url = self.url % args
if link.keep_query:
#new_link['url'] = urlquote(new_link['url'], parsed_query_string)
resolved_link.url = urlquote(new_link['url'], parsed_query_string)
resolved_link.url = urlquote(resolved_link.url, parsed_query_string)
else:
#new_link['active'] = False
resolved_link.active = False
if 'conditional_highlight' in link:
if self.conditional_highlight:
#new_link['active'] = link['conditional_highlight'](context)
resolved_link.active = link['conditional_highlight'](context)
resolved_link.active = self.conditional_highlight(context)
if 'conditional_disable' in link:
if self.conditional_disable:
#new_link['disabled'] = link['conditional_disable'](context)
resolved_link.disabled = link['conditional_disable'](context)
resolved_link.disabled = self.conditional_disable(context)
else:
#new_link['disabled'] = False
resolved_link.disabled = False
if current_view in link.get('children_views', []):
if current_view in self.children_views:
#new_link['active'] = True
resolved_link.active = True
@@ -121,7 +134,7 @@ class Link(object):
# #new_link['active'] = True
# resolved_link.active = True
for cls in link.get('children_classes', []):
for cls in self.children_classes:
object_list = get_navigation_objects(context)
if object_list:
if type(object_list[0]['object']) == cls or object_list[0]['object'] == cls:
@@ -132,7 +145,7 @@ class Link(object):
#context_links.append(new_link)
def bind_links(sources, links, menu_name=None):
def bind_links(sources, links, menu_name=None, position=0):
"""
Associate a link to a model, a view, or an url
"""
@@ -207,7 +220,7 @@ def register_sidebar_template(source_list, template_name):
sidebar_templates[source].append(template_name)
def get_object_navigation_links(context, menu_name=None, links_dict=object_navigation):
def get_context_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)
@@ -226,9 +239,10 @@ def get_object_navigation_links(context, menu_name=None, links_dict=object_navig
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)]
#navigation_object_links = Variable('overrided_object_links').resolve(context)
return 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
@@ -244,16 +258,18 @@ def get_object_navigation_links(context, menu_name=None, links_dict=object_navig
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)
#for link in resolve_links(context, links, current_view, current_path, parsed_query_string):
# context_links.append(link)
context_links.extend(links)
except KeyError:
pass
for resolved_object in get_navigation_objects(context):
try:
links = links_dict[menu_name][type(resolved_object['object'])]['links']
for link in resolve_links(context, links, current_view, current_path, parsed_query_string):
context_links.append(link)
#for link in resolve_links(context, links, current_view, current_path, parsed_query_string):
# context_links.append(link)
context_links.extend(links)
except KeyError:
pass

View File

@@ -15,10 +15,10 @@ from django.utils.encoding import smart_str, force_unicode, smart_unicode
from common.utils import urlquote
from ..api import (object_navigation, multi_object_navigation,
top_menu_entries, sidebar_templates)
top_menu_entries, sidebar_templates, get_context_object_navigation_links)
from ..forms import MultiItemForm
from ..utils import (resolve_to_name, resolve_arguments, resolve_template_variable,
get_navigation_objects, get_object_navigation_links)
get_navigation_objects)
register = Library()
logger = logging.getLogger(__name__)
@@ -30,22 +30,23 @@ class TopMenuNavigationNode(Node):
current_path = request.META['PATH_INFO']
current_view = resolve_to_name(current_path)
all_menu_links = [entry.get('link', {}) for entry in top_menu_entries]
menu_links = resolve_links(context, all_menu_links, current_view, current_path)
#all_menu_links = []#[entry.get('link', {}) for entry in top_menu_entries]
#menu_links = resolve_links(context, all_menu_links, current_view, current_path)
for index, link in enumerate(top_menu_entries):
if current_view in link.get('children_views', []):
menu_links[index]['active'] = True
#if current_view in link.get('children_views', []):
# menu_links[index]['active'] = True
for child_path_regex in link.get('children_path_regex', []):
if re.compile(child_path_regex).match(current_path.lstrip('/')):
menu_links[index]['active'] = True
#for child_path_regex in link.get('children_path_regex', []):
# if re.compile(child_path_regex).match(current_path.lstrip('/')):
# menu_links[index]['active'] = True
for children_view_regex in link.get('children_view_regex', []):
if re.compile(children_view_regex).match(current_view):
menu_links[index]['active'] = True
#for children_view_regex in link.get('children_view_regex', []):
# if re.compile(children_view_regex).match(current_view):
# menu_links[index]['active'] = True
pass
context['menu_links'] = menu_links
context['menu_links'] = []#menu_links
return ''
@@ -53,83 +54,6 @@ class TopMenuNavigationNode(Node):
def get_top_menu_links(parser, token):
return TopMenuNavigationNode()
"""
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', []):
object_list = get_navigation_objects(context)
if object_list:
if type(object_list[0]['object']) == cls or object_list[0]['object'] == cls:
new_link['active'] = True
context_links.append(new_link)
return context_links
"""
class GetNavigationLinks(Node):
def __init__(self, menu_name=None, links_dict=object_navigation, var_name='object_navigation_links'):
@@ -139,7 +63,7 @@ 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)
context[self.var_name] = get_context_object_navigation_links(context, menu_name, links_dict=self.links_dict)
object_list = get_navigation_objects(context)
if object_list:
context['navigation_object'] = object_list[0]['object']
@@ -161,10 +85,10 @@ def get_object_navigation_links(parser, token):
@register.inclusion_tag('generic_navigation.html', takes_context=True)
def object_navigation_template(context):
new_context = copy.copy(context)
new_context.update({
'horizontal': True,
'object_navigation_links': _get_object_navigation_links(context)
})
#new_context.update({
# 'horizontal': True,
# 'object_navigation_links': get_object_navigation_links(context)
#})
return new_context
@@ -183,7 +107,7 @@ def get_multi_item_links(parser, token):
def get_multi_item_links_form(context):
new_context = copy.copy(context)
new_context.update({
'form': MultiItemForm(actions=[(link['url'], link['text']) for link in _get_object_navigation_links(context, links_dict=multi_object_navigation)]),
'form': MultiItemForm(actions=[(link['url'], link['text']) for link in get_context_object_navigation_links(context, links_dict=multi_object_navigation)]),
'title': _(u'Selected item actions:'),
'form_action': reverse('multi_object_action_view'),
'submit_method': 'get',

View File

@@ -42,7 +42,7 @@ def render_widget(request, link):
link = links[0]
return mark_safe(u'<a style="text-decoration:none; margin-right: 10px;" href="%(url)s"><button style="vertical-align: top; padding: 1px; width: 110px; height: 100px; margin: 10px;"><img src="%(static_url)simages/icons/%(icon)s" alt="%(image_alt)s" /><p style="margin: 0px 0px 0px 0px;">%(string)s</p></button></a>' % {
'url': reverse(link['view']) if 'view' in link else link['url'],
'icon': link.get('icon', 'link_button.png'),
'icon': link.getattr('icon', 'link_button.png'),
'static_url': settings.STATIC_URL,
'string': capfirst(link['text']),
'image_alt': _(u'icon'),