From b5083d32db95ded589d6db81a390f65ee0a05405 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 29 Nov 2018 01:39:20 -0600 Subject: [PATCH] Add basic support for "simple menus" for sake of keeping those in common across various themes --- tailbone/menus.py | 73 +++++++++++++++++++++++++++++++++++++++++ tailbone/subscribers.py | 7 ++++ 2 files changed, 80 insertions(+) create mode 100644 tailbone/menus.py diff --git a/tailbone/menus.py b/tailbone/menus.py new file mode 100644 index 00000000..b3bb09f1 --- /dev/null +++ b/tailbone/menus.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8; -*- +################################################################################ +# +# Rattail -- Retail Software Framework +# Copyright © 2010-2018 Lance Edgar +# +# This file is part of Rattail. +# +# Rattail is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. +# +# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# Rattail. If not, see . +# +################################################################################ +""" +App Menus +""" + +from __future__ import unicode_literals, absolute_import + +from rattail.core import Object +from rattail.util import import_module_path + + +class MenuGroup(Object): + title = None + items = None + + +class MenuItem(Object): + title = None + url = None + perm = None + + +def make_simple_menus(request): + """ + Build the main menu list for the app. + """ + menus_module = import_module_path( + request.rattail_config.require('tailbone', 'menus')) + + if not hasattr(menus_module, 'simple_menus') or not callable(menus_module.simple_menus): + raise RuntimeError("module does not have a simple_menus() callable: {}".format(menus_module)) + + # collect "simple" menus definition, but must refine that somewhat to + # produce our final menus + raw_menus = menus_module.simple_menus(request) + final_menus = [] + 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'])] + 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)) + + return final_menus diff --git a/tailbone/subscribers.py b/tailbone/subscribers.py index 2203487c..6af90cbc 100644 --- a/tailbone/subscribers.py +++ b/tailbone/subscribers.py @@ -40,6 +40,7 @@ from webhelpers2.html import tags import tailbone from tailbone import helpers from tailbone.db import Session +from tailbone.menus import make_simple_menus def new_request(event): @@ -113,6 +114,12 @@ def before_render(event): options = [tags.Option(theme) for theme in available] renderer_globals['theme_picker_options'] = options + # heck while we're assuming the classic web app here... + # (we don't want this to happen for the API either!) + # TODO: just..awful *shrug* + if request.rattail_config.getbool('tailbone', 'menus.simple', default=False): + renderer_globals['menus'] = make_simple_menus(request) + def add_inbox_count(event): """