From c1eaf288128b17da92d31779a08310273555a4a2 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 29 Nov 2018 14:51:57 -0600 Subject: [PATCH] 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 --- tailbone/menus.py | 72 ++++++++++++---------- tailbone/subscribers.py | 4 ++ tailbone/templates/menu.mako | 34 ++++++---- tailbone/templates/themes/bobcat/base.mako | 38 +++++++----- 4 files changed, 91 insertions(+), 57 deletions(-) diff --git a/tailbone/menus.py b/tailbone/menus.py index ea7a86b3..f28574bf 100644 --- a/tailbone/menus.py +++ b/tailbone/menus.py @@ -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 diff --git a/tailbone/subscribers.py b/tailbone/subscribers.py index 6af90cbc..1a610ef2 100644 --- a/tailbone/subscribers.py +++ b/tailbone/subscribers.py @@ -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): """ diff --git a/tailbone/templates/menu.mako b/tailbone/templates/menu.mako index 29fdd229..7549e763 100644 --- a/tailbone/templates/menu.mako +++ b/tailbone/templates/menu.mako @@ -4,30 +4,40 @@ % for topitem in menus:
  • - ${topitem.title} - + % if topitem.is_link: + ${h.link_to(topitem.title, topitem.url, target=topitem.target)} + % else: + ${topitem.title} + + % endif
  • % endfor ## User Menu % if request.user:
  • - ${request.user}${" ({})".format(inbox_count) if inbox_count else ''} + % if messaging_enabled: + ${request.user}${" ({})".format(inbox_count) if inbox_count else ''} + % else: + ${request.user} + % endif
      % if request.is_root:
    • ${h.link_to("Stop being root", url('stop_root'))}
    • % elif request.is_admin:
    • ${h.link_to("Become root", url('become_root'))}
    • % endif -
    • ${h.link_to("Messages{}".format(" ({})".format(inbox_count) if inbox_count else ''), url('messages.inbox'))}
    • + % if messaging_enabled: +
    • ${h.link_to("Messages{}".format(" ({})".format(inbox_count) if inbox_count else ''), url('messages.inbox'))}
    • + % endif
    • ${h.link_to("Change Password", url('change_password'))}
    • ${h.link_to("Logout", url('logout'))}
    diff --git a/tailbone/templates/themes/bobcat/base.mako b/tailbone/templates/themes/bobcat/base.mako index 1e11aebb..dd1217b8 100644 --- a/tailbone/templates/themes/bobcat/base.mako +++ b/tailbone/templates/themes/bobcat/base.mako @@ -27,18 +27,22 @@ @@ -47,14 +51,20 @@ ## User Menu % if request.user: