Start top menu refactor, added elementtree to requirements
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from elementtree.ElementTree import Element
|
||||
|
||||
main_menu = Element('root')
|
||||
|
||||
@@ -7,11 +7,15 @@ import logging
|
||||
from django.template import (TemplateSyntaxError, Library,
|
||||
VariableDoesNotExist, Node, Variable)
|
||||
from django.utils.encoding import smart_str, force_unicode, smart_unicode
|
||||
from django.core.urlresolvers import reverse, NoReverseMatch
|
||||
|
||||
from elementtree.ElementTree import Element, SubElement
|
||||
|
||||
#from common.utils import urlquote
|
||||
|
||||
from .utils import (resolve_to_name, resolve_arguments,
|
||||
resolve_template_variable, get_navigation_objects)
|
||||
from . import main_menu
|
||||
|
||||
object_navigation = {}
|
||||
multi_object_navigation = {}
|
||||
@@ -26,6 +30,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
class ResolvedLink(object):
|
||||
active = False
|
||||
|
||||
|
||||
class Link(object):
|
||||
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):
|
||||
@@ -43,6 +48,11 @@ class Link(object):
|
||||
self.children_view_regex = children_view_regex
|
||||
self.klass = klass
|
||||
self.keep_query = keep_query
|
||||
|
||||
#
|
||||
self.conditional_highlight = None
|
||||
self.children_views = []
|
||||
self.children_classes = []
|
||||
|
||||
def resolve(self, context):
|
||||
request = Variable('request').resolve(context)
|
||||
@@ -63,6 +73,8 @@ class Link(object):
|
||||
if condition_result:
|
||||
#new_link = {}#copy.copy(link)
|
||||
resolved_link = ResolvedLink()
|
||||
resolved_link.text = self.text
|
||||
resolved_link.sprite = self.sprite
|
||||
try:
|
||||
#args, kwargs = resolve_arguments(context, self.get('args', {}))
|
||||
args, kwargs = resolve_arguments(context, self.args)
|
||||
@@ -171,37 +183,36 @@ def register_multi_item_links(src, links, menu_name=None):
|
||||
multi_object_navigation[menu_name][src]['links'].extend(links)
|
||||
|
||||
|
||||
def register_top_menu(name, link, children_views=None,
|
||||
children_path_regex=None, children_view_regex=None,
|
||||
position=None):
|
||||
def register_top_menu(name, link, children_views=None, children_path_regex=None, children_view_regex=None, position=None):
|
||||
"""
|
||||
Register a new menu entry for the main menu displayed at the top
|
||||
of the page
|
||||
"""
|
||||
menu = SubElement(main_menu, name, link=link)
|
||||
#entry = {'link': link, 'name': name}
|
||||
#if children_views:
|
||||
# entry['children_views'] = children_views
|
||||
#if children_path_regex:
|
||||
# entry['children_path_regex'] = children_path_regex
|
||||
#if children_view_regex:
|
||||
# entry['children_view_regex'] = children_view_regex
|
||||
#if position is not None:
|
||||
# entry['position'] = position
|
||||
# top_menu_entries.insert(position, entry)
|
||||
#else:
|
||||
# length = len(top_menu_entries)
|
||||
# entry['position'] = length
|
||||
# top_menu_entries.append(entry)
|
||||
|
||||
entry = {'link': link, 'name': name}
|
||||
if children_views:
|
||||
entry['children_views'] = children_views
|
||||
if children_path_regex:
|
||||
entry['children_path_regex'] = children_path_regex
|
||||
if children_view_regex:
|
||||
entry['children_view_regex'] = children_view_regex
|
||||
if position is not None:
|
||||
entry['position'] = position
|
||||
top_menu_entries.insert(position, entry)
|
||||
else:
|
||||
length = len(top_menu_entries)
|
||||
entry['position'] = length
|
||||
top_menu_entries.append(entry)
|
||||
|
||||
sort_menu_entries()
|
||||
|
||||
return entry
|
||||
#sort_menu_entries()
|
||||
#
|
||||
#return entry
|
||||
return menu
|
||||
|
||||
|
||||
def sort_menu_entries():
|
||||
global top_menu_entries
|
||||
top_menu_entries = sorted(top_menu_entries, key=lambda k: (k['position'] < 0, k['position']))
|
||||
#def sort_menu_entries():
|
||||
# global top_menu_entries
|
||||
# top_menu_entries = sorted(top_menu_entries, key=lambda k: (k['position'] < 0, k['position']))
|
||||
|
||||
|
||||
def register_model_list_columns(model, columns):
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
{% get_main_setting "DISABLE_ICONS" as disable_icons %}
|
||||
|
||||
{% if link.disabled %}
|
||||
<a class="{{ link.class }}" style="cursor: default;" href="#">{% if link.famfam and not disable_icons %}<span class="famfam inactive famfam-{{ link.famfam|default:'link' }}"></span>{% endif %}{{ link.text|capfirst }}{% if link.error %} - {{ link.error }}{% endif %}{% if link.active and not hide_active_anchor %}<span class="famfam active famfam-resultset_previous"></span>{% endif %}</a>{% if horizontal %}{% if not forloop.last %} | {% endif %}{% endif %}
|
||||
<a class="{{ link.class }}" style="cursor: default;" href="#">{% if link.sprite and not disable_icons %}<span class="famfam inactive famfam-{{ link.sprite|default:'link' }}"></span>{% endif %}{{ link.text|capfirst }}{% if link.error %} - {{ link.error }}{% endif %}{% if link.active and not hide_active_anchor %}<span class="famfam active famfam-resultset_previous"></span>{% endif %}</a>{% if horizontal %}{% if not forloop.last %} | {% endif %}{% endif %}
|
||||
{% else %}
|
||||
<a class="{{ link.class }}" href="{{ link.url }}">{% if link.famfam and not disable_icons %}<span class="famfam active famfam-{{ link.famfam|default:'link' }}"></span>{% endif %}{{ link.text|capfirst }}{% if link.error %} - {{ link.error }}{% endif %}{% if link.active and not hide_active_anchor %}<span class="famfam active famfam-resultset_previous"></span>{% endif %}</a>{% if horizontal %}{% if not forloop.last %} | {% endif %}{% endif %}
|
||||
<a class="{{ link.class }}" href="{{ link.url }}">{% if link.sprite and not disable_icons %}<span class="famfam active famfam-{{ link.sprite|default:'link' }}"></span>{% endif %}{{ link.text|capfirst }}{% if link.error %} - {{ link.error }}{% endif %}{% if link.active and not hide_active_anchor %}<span class="famfam active famfam-resultset_previous"></span>{% endif %}</a>{% if horizontal %}{% if not forloop.last %} | {% endif %}{% endif %}
|
||||
{% endif %}
|
||||
|
||||
@@ -19,6 +19,7 @@ from ..api import (object_navigation, multi_object_navigation,
|
||||
from ..forms import MultiItemForm
|
||||
from ..utils import (resolve_to_name, resolve_arguments, resolve_template_variable,
|
||||
get_navigation_objects)
|
||||
from .. import main_menu
|
||||
|
||||
register = Library()
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -26,27 +27,25 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
class TopMenuNavigationNode(Node):
|
||||
def render(self, context):
|
||||
request = Variable('request').resolve(context)
|
||||
current_path = request.META['PATH_INFO']
|
||||
current_view = resolve_to_name(current_path)
|
||||
|
||||
#request = Variable('request').resolve(context)
|
||||
#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)
|
||||
|
||||
for index, link in enumerate(top_menu_entries):
|
||||
#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 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
|
||||
#for index, link in enumerate(top_menu_entries):
|
||||
# #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 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.get('link').resolve(context) for menu in main_menu.getchildren()]
|
||||
return ''
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import logging
|
||||
|
||||
from django.core.urlresolvers import RegexURLResolver, RegexURLPattern, Resolver404, get_resolver
|
||||
|
||||
from django.template import (TemplateSyntaxError, Library,
|
||||
@@ -7,6 +9,8 @@ from django.template import (TemplateSyntaxError, Library,
|
||||
from django.utils.text import unescape_string_literal
|
||||
|
||||
#__all__ = ('resolve_to_name',)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_navigation_objects(context):
|
||||
object_list = []
|
||||
|
||||
@@ -17,9 +17,9 @@ from .utils import resolve_to_name
|
||||
|
||||
|
||||
def button_navigation_widget(request, link):
|
||||
if 'permissions' in link:
|
||||
if link.permissions:
|
||||
try:
|
||||
Permission.objects.check_permissions(request.user, link['permissions'])
|
||||
Permission.objects.check_permissions(request.user, link.permissions)
|
||||
return render_widget(request, link)
|
||||
except PermissionDenied:
|
||||
return u''
|
||||
|
||||
Reference in New Issue
Block a user