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

View file

@ -120,6 +120,10 @@ def before_render(event):
if request.rattail_config.getbool('tailbone', 'menus.simple', default=False): if request.rattail_config.getbool('tailbone', 'menus.simple', default=False):
renderer_globals['menus'] = make_simple_menus(request) 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): def add_inbox_count(event):
""" """

View file

@ -4,30 +4,40 @@
% for topitem in menus: % for topitem in menus:
<li> <li>
<a>${topitem.title}</a> % if topitem.is_link:
<ul> ${h.link_to(topitem.title, topitem.url, target=topitem.target)}
% for subitem in topitem.items: % else:
% if subitem.is_sep: <a>${topitem.title}</a>
<li>-</li> <ul>
% else: % for subitem in topitem.items:
<li>${h.link_to(subitem.title, subitem.url, target=subitem.target)}</li> % if subitem.is_sep:
% endif <li>-</li>
% endfor % else:
</ul> <li>${h.link_to(subitem.title, subitem.url, target=subitem.target)}</li>
% endif
% endfor
</ul>
% endif
</li> </li>
% endfor % endfor
## User Menu ## User Menu
% if request.user: % if request.user:
<li> <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> <ul>
% if request.is_root: % if request.is_root:
<li class="root-user">${h.link_to("Stop being root", url('stop_root'))}</li> <li class="root-user">${h.link_to("Stop being root", url('stop_root'))}</li>
% elif request.is_admin: % elif request.is_admin:
<li class="root-user">${h.link_to("Become root", url('become_root'))}</li> <li class="root-user">${h.link_to("Become root", url('become_root'))}</li>
% endif % 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("Change Password", url('change_password'))}</li>
<li>${h.link_to("Logout", url('logout'))}</li> <li>${h.link_to("Logout", url('logout'))}</li>
</ul> </ul>

View file

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