Add support for "simple" menus in base template

also flesh out the simple menu logic a bit, add separators etc.
This commit is contained in:
Lance Edgar 2018-11-29 14:00:11 -06:00
parent 291128b96f
commit 0eb394fb86
3 changed files with 86 additions and 11 deletions

View file

@ -38,7 +38,12 @@ class MenuGroup(Object):
class MenuItem(Object):
title = None
url = None
perm = None
target = None
is_sep = False
class MenuSeparator(object):
is_sep = True
def make_simple_menus(request):
@ -58,16 +63,43 @@ def make_simple_menus(request):
for topitem in raw_menus:
# figure out which ones the user has permission to access
allowed = [item for item in topitem['items']
if request.has_perm(item['perm'])]
allowed = []
for item in topitem['items']:
if item.get('type') == 'sep':
allowed.append(item)
if item.get('perm'):
if request.has_perm(item['perm']):
allowed.append(item)
else:
allowed.append(item)
if allowed:
# okay, user must have access to something; add the menu
items = [MenuItem(title=item['title'],
url=item['url'],
perm=item['perm'])
for item in allowed]
final_menus.append(
MenuGroup(title=topitem['title'], items=items))
# user must have access to something; construct items for the menu
menu_items = []
for item in allowed:
# separator
if item.get('type') == 'sep':
if menu_items and not menu_items[-1].is_sep:
menu_items.append(MenuSeparator())
# menu item
else:
menu_items.append(
MenuItem(title=item['title'],
url=item['url'],
target=item.get('target')))
# remove final separator if present
if menu_items and menu_items[-1].is_sep:
menu_items.pop()
# only add if we wound up with something
if menu_items:
final_menus.append(
MenuGroup(title=topitem['title'], items=menu_items))
return final_menus

View file

@ -0,0 +1,39 @@
## -*- coding: utf-8; -*-
<%def name="main_menu_items()">
% for topitem in menus:
<li>
<a>${topitem.title}</a>
<ul>
% for subitem in topitem.items:
% if subitem.is_sep:
<li>-</li>
% else:
<li>${h.link_to(subitem.title, subitem.url, target=subitem.target)}</li>
% endif
% endfor
</ul>
</li>
% endfor
## User Menu
% if request.user:
<li>
<a${' class="root-user"' if request.is_root else ''|n}>${request.user}${" ({})".format(inbox_count) if inbox_count else ''}</a>
<ul>
% if request.is_root:
<li class="root-user">${h.link_to("Stop being root", url('stop_root'))}</li>
% elif request.is_admin:
<li class="root-user">${h.link_to("Become root", url('become_root'))}</li>
% endif
<li>${h.link_to("Messages{}".format(" ({})".format(inbox_count) if inbox_count else ''), url('messages.inbox'))}</li>
<li>${h.link_to("Change Password", url('change_password'))}</li>
<li>${h.link_to("Logout", url('logout'))}</li>
</ul>
</li>
% else:
<li>${h.link_to("Login", url('login'))}</li>
% endif
</%def>

View file

@ -31,7 +31,11 @@
<a class="navbar-link">${topitem.title}</a>
<div class="navbar-dropdown">
% for subitem in topitem.items:
${h.link_to(subitem.title, subitem.url, class_='navbar-item')}
% if subitem.is_sep:
<hr class="navbar-divider">
% else:
${h.link_to(subitem.title, subitem.url, class_='navbar-item', target=subitem.target)}
% endif
% endfor
</div>
</div>