PEP8 and Pylint cleanups
This commit is contained in:
+13
-13
@@ -8,24 +8,24 @@ model_list_columns = {}
|
||||
|
||||
def register_multi_item_links(src, links, menu_name=None):
|
||||
if menu_name in multi_object_navigation:
|
||||
if hasattr(src, '__iter__'):
|
||||
if hasattr(src, '__iter__'):
|
||||
for one_src in src:
|
||||
if one_src in object_navigation[menu_name]:
|
||||
multi_object_navigation[menu_name][one_src]['links'].extend(links)
|
||||
else:
|
||||
multi_object_navigation[menu_name][one_src] = {'links':copy.copy(links)}
|
||||
multi_object_navigation[menu_name][one_src] = {'links': copy.copy(links)}
|
||||
else:
|
||||
if src in multi_object_navigation[menu_name]:
|
||||
multi_object_navigation[menu_name][src]['links'].extend(links)
|
||||
else:
|
||||
multi_object_navigation[menu_name][src] = {'links':links}
|
||||
multi_object_navigation[menu_name][src] = {'links': links}
|
||||
else:
|
||||
multi_object_navigation[menu_name] = {}
|
||||
if hasattr(src, '__iter__'):
|
||||
for one_src in src:
|
||||
multi_object_navigation[menu_name][one_src] = {'links':links}
|
||||
multi_object_navigation[menu_name][one_src] = {'links': links}
|
||||
else:
|
||||
multi_object_navigation[menu_name] = {src:{'links':links}}
|
||||
multi_object_navigation[menu_name] = {src: {'links': links}}
|
||||
|
||||
|
||||
def register_links(src, links, menu_name=None):
|
||||
@@ -35,26 +35,26 @@ def register_links(src, links, menu_name=None):
|
||||
if one_src in object_navigation[menu_name]:
|
||||
object_navigation[menu_name][one_src]['links'].extend(links)
|
||||
else:
|
||||
object_navigation[menu_name][one_src] = {'links':copy.copy(links)}
|
||||
object_navigation[menu_name][one_src] = {'links': copy.copy(links)}
|
||||
else:
|
||||
if src in object_navigation[menu_name]:
|
||||
object_navigation[menu_name][src]['links'].extend(links)
|
||||
else:
|
||||
object_navigation[menu_name][src] = {'links':links}
|
||||
object_navigation[menu_name][src] = {'links': links}
|
||||
else:
|
||||
object_navigation[menu_name] = {}
|
||||
object_navigation[menu_name] = {}
|
||||
if hasattr(src, '__iter__'):
|
||||
for one_src in src:
|
||||
object_navigation[menu_name][one_src] = {'links':links}
|
||||
object_navigation[menu_name][one_src] = {'links': links}
|
||||
else:
|
||||
object_navigation[menu_name] = {src:{'links':links}}
|
||||
|
||||
object_navigation[menu_name] = {src: {'links': links}}
|
||||
|
||||
|
||||
def register_menu(links):
|
||||
for link in links:
|
||||
menu_links.append(link)
|
||||
|
||||
menu_links.sort(lambda x, y: 1 if x>y else -1, lambda x:x['position'] if 'position' in x else 1)
|
||||
|
||||
menu_links.sort(lambda x, y: 1 if x > y else -1, lambda x: x['position'] if 'position' in x else 1)
|
||||
|
||||
|
||||
def register_model_list_columns(model, columns):
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from django import forms
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.template.defaultfilters import capfirst
|
||||
|
||||
|
||||
class MultiItemForm(forms.Form):
|
||||
@@ -9,5 +10,5 @@ class MultiItemForm(forms.Form):
|
||||
choices = [('', '------')]
|
||||
choices.extend([(action[0], capfirst(action[1])) for action in actions])
|
||||
self.fields['action'].choices = choices
|
||||
|
||||
|
||||
action = forms.ChoiceField(label=_(u'Multi item action'))
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import copy
|
||||
import copy
|
||||
import re
|
||||
|
||||
from django.core.urlresolvers import reverse, NoReverseMatch
|
||||
@@ -32,24 +32,22 @@ def process_links(links, view_name, url):
|
||||
child_url = 'url' in child_link and child_link['url']
|
||||
if view_name == child_view or url == child_url:
|
||||
active = True
|
||||
active_item = item
|
||||
|
||||
items.append(
|
||||
{
|
||||
'first':count==0,
|
||||
'active':active,
|
||||
'url':item_view and reverse(item_view) or item_url or '#',
|
||||
'text':unicode(item['text']),
|
||||
'famfam':'famfam' in item and item['famfam'],
|
||||
}
|
||||
)
|
||||
active_item = item
|
||||
|
||||
items.append({
|
||||
'first': count == 0,
|
||||
'active': active,
|
||||
'url': item_view and reverse(item_view) or item_url or '#',
|
||||
'text': unicode(item['text']),
|
||||
'famfam': 'famfam' in item and item['famfam'],
|
||||
})
|
||||
return items, active_item
|
||||
|
||||
|
||||
class NavigationNode(Node):
|
||||
def __init__(self, navigation, *args, **kwargs):
|
||||
self.navigation = navigation
|
||||
|
||||
|
||||
def render(self, context):
|
||||
request = Variable('request').resolve(context)
|
||||
view_name = resolve_to_name(request.META['PATH_INFO'])
|
||||
@@ -59,7 +57,7 @@ class NavigationNode(Node):
|
||||
if active_item and 'links' in active_item:
|
||||
secondary_links, active_item = process_links(links=active_item['links'], view_name=view_name, url=request.META['PATH_INFO'])
|
||||
context['navigation_secondary_links'] = secondary_links
|
||||
return ''
|
||||
return ''
|
||||
|
||||
|
||||
@register.tag
|
||||
@@ -69,13 +67,14 @@ def main_navigation(parser, token):
|
||||
# if len(args) != 3 or args[1] != 'as':
|
||||
# raise TemplateSyntaxError("'get_all_states' requires 'as variable' (got %r)" % args)
|
||||
|
||||
#return NavigationNode(variable=args[2], navigation=navigation)
|
||||
return NavigationNode(navigation=menu_navigation)
|
||||
#return NavigationNode(variable=args[2], navigation=navigation)
|
||||
return NavigationNode(navigation=menu_navigation)
|
||||
|
||||
|
||||
#http://www.djangosnippets.org/snippets/1378/
|
||||
__all__ = ('resolve_to_name',)
|
||||
|
||||
|
||||
def _pattern_resolve_to_name(self, path):
|
||||
match = self.regex.search(path)
|
||||
if match:
|
||||
@@ -88,6 +87,7 @@ def _pattern_resolve_to_name(self, path):
|
||||
name = "%s.%s" % (self.callback.__module__, self.callback.func_name)
|
||||
return name
|
||||
|
||||
|
||||
def _resolver_resolve_to_name(self, path):
|
||||
tried = []
|
||||
match = self.regex.search(path)
|
||||
@@ -109,13 +109,16 @@ def _resolver_resolve_to_name(self, path):
|
||||
RegexURLPattern.resolve_to_name = _pattern_resolve_to_name
|
||||
RegexURLResolver.resolve_to_name = _resolver_resolve_to_name
|
||||
|
||||
|
||||
def resolve_to_name(path, urlconf=None):
|
||||
return get_resolver(urlconf).resolve_to_name(path)
|
||||
|
||||
|
||||
@register.filter
|
||||
def resolve_url_name(value):
|
||||
return resolve_to_name(value)
|
||||
|
||||
|
||||
def resolve_arguments(context, src_args):
|
||||
args = []
|
||||
kwargs = {}
|
||||
@@ -132,10 +135,10 @@ def resolve_arguments(context, src_args):
|
||||
else:
|
||||
val = resolve_template_variable(context, src_args)
|
||||
if val:
|
||||
args.append(val)
|
||||
args.append(val)
|
||||
|
||||
return args, kwargs
|
||||
|
||||
|
||||
|
||||
def resolve_links(context, links, current_view, current_path):
|
||||
context_links = []
|
||||
@@ -145,11 +148,11 @@ def resolve_links(context, links, current_view, current_path):
|
||||
args, kwargs = resolve_arguments(context, link.get('args', {}))
|
||||
except VariableDoesNotExist:
|
||||
args = []
|
||||
kwargs = {}
|
||||
|
||||
kwargs = {}
|
||||
|
||||
if 'view' in link:
|
||||
new_link['active'] = link['view'] == current_view
|
||||
|
||||
|
||||
try:
|
||||
if kwargs:
|
||||
new_link['url'] = reverse(link['view'], kwargs=kwargs)
|
||||
@@ -166,25 +169,25 @@ def resolve_links(context, links, current_view, current_path):
|
||||
new_link['url'] = link['url'] % args
|
||||
else:
|
||||
new_link['active'] = False
|
||||
context_links.append(new_link)
|
||||
context_links.append(new_link)
|
||||
return context_links
|
||||
|
||||
|
||||
def _get_object_navigation_links(context, menu_name=None, links_dict=object_navigation):
|
||||
current_path = Variable('request').resolve(context).META['PATH_INFO']
|
||||
current_view = resolve_to_name(current_path)#.get_full_path())
|
||||
context_links = []
|
||||
current_view = resolve_to_name(current_path)
|
||||
context_links = []
|
||||
|
||||
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
|
||||
|
||||
|
||||
try:
|
||||
links = links_dict[menu_name][current_view]['links']
|
||||
for link in resolve_links(context, links, current_view, current_path):
|
||||
@@ -233,19 +236,19 @@ def get_object_navigation_links(parser, token):
|
||||
if not m:
|
||||
raise TemplateSyntaxError("%r tag had invalid arguments" % tag_name)
|
||||
|
||||
menu_name, var_name = m.groups()
|
||||
menu_name, var_name = m.groups()
|
||||
return GetNavigationLinks(menu_name=menu_name, var_name=var_name)
|
||||
|
||||
|
||||
|
||||
|
||||
@register.inclusion_tag('generic_navigation.html', takes_context=True)
|
||||
def object_navigation_template(context):
|
||||
return {
|
||||
'request':context['request'],
|
||||
'horizontal':True,
|
||||
'object_navigation_links':_get_object_navigation_links(context)
|
||||
'request': context['request'],
|
||||
'horizontal': True,
|
||||
'object_navigation_links': _get_object_navigation_links(context)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@register.tag
|
||||
def get_multi_item_links(parser, token):
|
||||
tag_name, arg = token.contents.split(None, 1)
|
||||
@@ -261,9 +264,9 @@ 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)]),
|
||||
'title':_(u'Selected item actions:'),
|
||||
'form_action':reverse('multi_object_action_view'),
|
||||
'submit_method':'get',
|
||||
'form': MultiItemForm(actions=[(link['url'], link['text']) for link in _get_object_navigation_links(context, links_dict=multi_object_navigation)]),
|
||||
'title': _(u'Selected item actions:'),
|
||||
'form_action': reverse('multi_object_action_view'),
|
||||
'submit_method': 'get',
|
||||
})
|
||||
return new_context
|
||||
|
||||
Reference in New Issue
Block a user