Add basic support for "simple menus"

for sake of keeping those in common across various themes
This commit is contained in:
Lance Edgar 2018-11-29 01:39:20 -06:00
parent a76a7dd54c
commit b5083d32db
2 changed files with 80 additions and 0 deletions

73
tailbone/menus.py Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
#
################################################################################
"""
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

View file

@ -40,6 +40,7 @@ from webhelpers2.html import tags
import tailbone import tailbone
from tailbone import helpers from tailbone import helpers
from tailbone.db import Session from tailbone.db import Session
from tailbone.menus import make_simple_menus
def new_request(event): def new_request(event):
@ -113,6 +114,12 @@ def before_render(event):
options = [tags.Option(theme) for theme in available] options = [tags.Option(theme) for theme in available]
renderer_globals['theme_picker_options'] = options 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): def add_inbox_count(event):
""" """