Add support for top-level links for simple menus

also add 'messaging_enabled' to global template context, so can include (or
not) that stuff in the user menu
This commit is contained in:
Lance Edgar 2018-11-29 14:51:57 -06:00
parent 0eb394fb86
commit c1eaf28812
4 changed files with 91 additions and 57 deletions

View file

@ -33,12 +33,14 @@ from rattail.util import import_module_path
class MenuGroup(Object):
title = None
items = None
is_link = False
class MenuItem(Object):
title = None
url = None
target = None
is_link = True
is_sep = False
@ -62,44 +64,52 @@ def make_simple_menus(request):
final_menus = []
for topitem in raw_menus:
# figure out which ones the user has permission to access
allowed = []
for item in topitem['items']:
if topitem.get('type') == 'link':
final_menus.append(
MenuItem(title=topitem['title'],
url=topitem['url'],
target=topitem.get('target')))
if item.get('type') == 'sep':
allowed.append(item)
else: # assuming 'menu' type
if item.get('perm'):
if request.has_perm(item['perm']):
allowed.append(item)
else:
allowed.append(item)
# figure out which ones the user has permission to access
allowed = []
for item in topitem['items']:
if allowed:
# 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())
allowed.append(item)
# menu item
if item.get('perm'):
if request.has_perm(item['perm']):
allowed.append(item)
else:
menu_items.append(
MenuItem(title=item['title'],
url=item['url'],
target=item.get('target')))
allowed.append(item)
# remove final separator if present
if menu_items and menu_items[-1].is_sep:
menu_items.pop()
if allowed:
# only add if we wound up with something
if menu_items:
final_menus.append(
MenuGroup(title=topitem['title'], items=menu_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

@ -120,6 +120,10 @@ def before_render(event):
if request.rattail_config.getbool('tailbone', 'menus.simple', default=False):
renderer_globals['menus'] = make_simple_menus(request)
# TODO: ugh, same deal here
renderer_globals['messaging_enabled'] = request.rattail_config.getbool(
'tailbone', 'messaging.enabled', default=True)
def add_inbox_count(event):
"""

View file

@ -4,30 +4,40 @@
% 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>
% if topitem.is_link:
${h.link_to(topitem.title, topitem.url, target=topitem.target)}
% else:
<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>
% endif
</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>
% if messaging_enabled:
<a${' class="root-user"' if request.is_root else ''|n}>${request.user}${" ({})".format(inbox_count) if inbox_count else ''}</a>
% else:
<a${' class="root-user"' if request.is_root else ''|n}>${request.user}</a>
% endif
<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>
% if messaging_enabled:
<li>${h.link_to("Messages{}".format(" ({})".format(inbox_count) if inbox_count else ''), url('messages.inbox'))}</li>
% endif
<li>${h.link_to("Change Password", url('change_password'))}</li>
<li>${h.link_to("Logout", url('logout'))}</li>
</ul>

View file

@ -27,18 +27,22 @@
<div class="navbar-start">
% for topitem in menus:
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">${topitem.title}</a>
<div class="navbar-dropdown">
% for subitem in topitem.items:
% 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>
% if topitem.is_link:
${h.link_to(topitem.title, topitem.url, target=topitem.target, class_='navbar-item')}
% else:
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">${topitem.title}</a>
<div class="navbar-dropdown">
% for subitem in topitem.items:
% 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>
% endif
% endfor
</div><!-- navbar-start -->
@ -47,14 +51,20 @@
## User Menu
% if request.user:
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link ${'root-user' if request.is_root else ''}">${request.user}${" ({})".format(inbox_count) if inbox_count else ''}</a>
% if messaging_enabled:
<a class="navbar-link ${'root-user' if request.is_root else ''}">${request.user}${" ({})".format(inbox_count) if inbox_count else ''}</a>
% else:
<a class="navbar-link ${'root-user' if request.is_root else ''}">${request.user}</a>
% endif
<div class="navbar-dropdown">
% if request.is_root:
${h.link_to("Stop being root", url('stop_root'), class_='navbar-item root-user')}
% elif request.is_admin:
${h.link_to("Become root", url('become_root'), class_='navbar-item root-user')}
% endif
${h.link_to("Messages{}".format(" ({})".format(inbox_count) if inbox_count else ''), url('messages.inbox'), class_='navbar-item')}
% if messaging_enabled:
${h.link_to("Messages{}".format(" ({})".format(inbox_count) if inbox_count else ''), url('messages.inbox'), class_='navbar-item')}
% endif
${h.link_to("Change Password", url('change_password'), class_='navbar-item')}
${h.link_to("Logout", url('logout'), class_='navbar-item')}
</div>