From 8514970da5370556b4baa6255a5ed7604adfb056 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 19 Sep 2024 17:23:09 +1000 Subject: [PATCH] [PUI] Panels plugin fix (#8147) * Rename "get_custom_panels" method - So that PUI plugin does not conflict with CUI plugin * Update docstrings * Rename "get_ui_panels" -> "ui_panels" * Docs fix * rename "ui_panels" -> "get_ui_panels" --- docs/docs/extend/plugins/ui.md | 4 ++-- src/backend/InvenTree/plugin/api.py | 24 ++++++++++++------- .../base/integration/UserInterfaceMixin.py | 8 +++++-- .../integration/user_interface_sample.py | 2 +- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/docs/docs/extend/plugins/ui.md b/docs/docs/extend/plugins/ui.md index 33b58faabeb6..160368784265 100644 --- a/docs/docs/extend/plugins/ui.md +++ b/docs/docs/extend/plugins/ui.md @@ -18,9 +18,9 @@ When rendering certain content in the user interface, the rendering functions ar ## Custom Panels -Many of the pages in the InvenTree web interface are built using a series of "panels" which are displayed on the page. Custom panels can be added to these pages, by implementing the `get_custom_panels` method: +Many of the pages in the InvenTree web interface are built using a series of "panels" which are displayed on the page. Custom panels can be added to these pages, by implementing the `get_ui_panels` method: -::: plugin.base.integration.UserInterfaceMixin.UserInterfaceMixin.get_custom_panels +::: plugin.base.integration.UserInterfaceMixin.UserInterfaceMixin.get_ui_panels options: show_bases: False show_root_heading: False diff --git a/src/backend/InvenTree/plugin/api.py b/src/backend/InvenTree/plugin/api.py index a8a9cb1c48de..4b2172929b8c 100644 --- a/src/backend/InvenTree/plugin/api.py +++ b/src/backend/InvenTree/plugin/api.py @@ -19,6 +19,7 @@ from common.api import GlobalSettingsPermissions from common.settings import get_global_setting from InvenTree.api import MetadataView +from InvenTree.exceptions import log_error from InvenTree.filters import SEARCH_ORDER_FILTER from InvenTree.mixins import ( CreateAPI, @@ -433,17 +434,22 @@ def get(self, request): if get_global_setting('ENABLE_PLUGINS_INTERFACE'): # Extract all plugins from the registry which provide custom panels for _plugin in registry.with_mixin('ui', active=True): - # Allow plugins to fill this data out - plugin_panels = _plugin.get_custom_panels( - target_model, target_id, request - ) + try: + # Allow plugins to fill this data out + plugin_panels = _plugin.get_ui_panels( + target_model, target_id, request + ) - if plugin_panels and type(plugin_panels) is list: - for panel in plugin_panels: - panel['plugin'] = _plugin.slug + if plugin_panels and type(plugin_panels) is list: + for panel in plugin_panels: + panel['plugin'] = _plugin.slug - # TODO: Validate each panel before inserting - panels.append(panel) + # TODO: Validate each panel before inserting + panels.append(panel) + except Exception: + # Custom panels could not load + # Log the error and continue + log_error(f'{_plugin.slug}.get_ui_panels') return Response(PluginSerializers.PluginPanelSerializer(panels, many=True).data) diff --git a/src/backend/InvenTree/plugin/base/integration/UserInterfaceMixin.py b/src/backend/InvenTree/plugin/base/integration/UserInterfaceMixin.py index 252c18705bcd..7c70dd2c4e53 100644 --- a/src/backend/InvenTree/plugin/base/integration/UserInterfaceMixin.py +++ b/src/backend/InvenTree/plugin/base/integration/UserInterfaceMixin.py @@ -34,6 +34,10 @@ class UserInterfaceMixin: - All content is accessed via the API, as requested by the user interface. - This means that content can be dynamically generated, based on the current state of the system. + + The following custom UI methods are available: + - get_ui_panels: Return a list of custom panels to be injected into the UI + """ class MixinMeta: @@ -46,8 +50,8 @@ def __init__(self): super().__init__() self.add_mixin('ui', True, __class__) - def get_custom_panels( - self, instance_type: str, instance_id: int, request: Request + def get_ui_panels( + self, instance_type: str, instance_id: int, request: Request, **kwargs ) -> list[CustomPanel]: """Return a list of custom panels to be injected into the UI. diff --git a/src/backend/InvenTree/plugin/samples/integration/user_interface_sample.py b/src/backend/InvenTree/plugin/samples/integration/user_interface_sample.py index 5b0fbf2f77b7..deaa655e2456 100644 --- a/src/backend/InvenTree/plugin/samples/integration/user_interface_sample.py +++ b/src/backend/InvenTree/plugin/samples/integration/user_interface_sample.py @@ -44,7 +44,7 @@ class SampleUserInterfacePlugin(SettingsMixin, UserInterfaceMixin, InvenTreePlug }, } - def get_custom_panels(self, instance_type: str, instance_id: int, request): + def get_ui_panels(self, instance_type: str, instance_id: int, request, **kwargs): """Return a list of custom panels to be injected into the UI.""" panels = []