From 2b3d69a3794f941e4c360304e4c5f84f2d97d841 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 13 Jan 2025 12:55:34 -0600 Subject: [PATCH] fix: expose setting to choose menu handler, in appinfo/configure --- pyproject.toml | 3 + src/wuttaweb/handler.py | 82 +++++++++++++++++-- src/wuttaweb/templates/appinfo/configure.mako | 17 ++++ src/wuttaweb/views/settings.py | 16 +++- tests/test_handler.py | 75 ++++++++++++++++- 5 files changed, 181 insertions(+), 12 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5a11bc1..a7a19ff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -70,6 +70,9 @@ wuttaweb = "wuttaweb.conf:WuttaWebConfigExtension" [project.entry-points."wutta.typer_imports"] wuttaweb = "wuttaweb.cli" +[project.entry-points."wutta.web.menus"] +wuttaweb = "wuttaweb.menus:MenuHandler" + [project.urls] Homepage = "https://wuttaproject.org/" diff --git a/src/wuttaweb/handler.py b/src/wuttaweb/handler.py index 1ac0b78..9ca6c5a 100644 --- a/src/wuttaweb/handler.py +++ b/src/wuttaweb/handler.py @@ -24,7 +24,10 @@ Web Handler """ +import warnings + from wuttjamaican.app import GenericHandler +from wuttjamaican.util import load_entry_points from wuttaweb import static, forms, grids @@ -106,22 +109,87 @@ class WebHandler(GenericHandler): def get_menu_handler(self, **kwargs): """ - Get the configured "menu" handler for the web app. + Get the configured :term:`menu handler` for the web app. Specify a custom handler in your config file like this: .. code-block:: ini [wutta.web] - menus.handler_spec = poser.web.menus:PoserMenuHandler + menus.handler.spec = poser.web.menus:PoserMenuHandler :returns: Instance of :class:`~wuttaweb.menus.MenuHandler`. """ - if not hasattr(self, 'menu_handler'): - spec = self.config.get(f'{self.appname}.web.menus.handler_spec', - default='wuttaweb.menus:MenuHandler') - self.menu_handler = self.app.load_object(spec)(self.config) - return self.menu_handler + spec = self.config.get(f'{self.appname}.web.menus.handler.spec') + if not spec: + spec = self.config.get(f'{self.appname}.web.menus.handler_spec') + if spec: + warnings.warn(f"setting '{self.appname}.web.menus.handler_spec' is deprecated; " + f"please use '{self.appname}.web.menus.handler_spec' instead", + DeprecationWarning) + else: + spec = self.config.get(f'{self.appname}.web.menus.handler.default_spec', + default='wuttaweb.menus:MenuHandler') + factory = self.app.load_object(spec) + return factory(self.config) + + def get_menu_handler_specs(self, default=None): + """ + Get the :term:`spec` strings for all available :term:`menu + handlers `. See also + :meth:`get_menu_handler()`. + + :param default: Default spec string(s) to include, even if not + registered. Can be a string or list of strings. + + :returns: List of menu handler spec strings. + + This will gather available spec strings from the following: + + First, the ``default`` as provided by caller. + + Second, the default spec from config, if set; for example: + + .. code-block:: ini + + [wutta.web] + menus.handler.default_spec = poser.web.menus:PoserMenuHandler + + Third, each spec registered via entry points. For instance in + ``pyproject.toml``: + + .. code-block:: toml + + [project.entry-points."wutta.web.menus"] + poser = "poser.web.menus:PoserMenuHandler" + + The final list will be "sorted" according to the above, with + the latter registered handlers being sorted alphabetically. + """ + handlers = [] + + # defaults from caller + if isinstance(default, str): + handlers.append(default) + elif default: + handlers.extend(default) + + # configured default, if applicable + default = self.config.get(f'{self.config.appname}.web.menus.handler.default_spec') + if default and default not in handlers: + handlers.append(default) + + # registered via entry points + registered = [] + for Handler in load_entry_points(f'{self.appname}.web.menus').values(): + spec = Handler.get_spec() + if spec not in handlers: + registered.append(spec) + if registered: + registered.sort() + handlers.extend(registered) + + return handlers def make_form(self, request, **kwargs): """ diff --git a/src/wuttaweb/templates/appinfo/configure.mako b/src/wuttaweb/templates/appinfo/configure.mako index 03f1551..f761ade 100644 --- a/src/wuttaweb/templates/appinfo/configure.mako +++ b/src/wuttaweb/templates/appinfo/configure.mako @@ -41,6 +41,21 @@ + + + + + + + +

User/Auth

@@ -241,6 +256,8 @@ ${parent.modify_vue_vars()}