From 28e16616e5d8b574e8f24714bcac29a6c5bb2c5c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 6 May 2022 21:32:11 +1000 Subject: [PATCH] Adds a PanelMixin plugin mixin class Intended to allow rendering of custom panels on pages --- .../plugin/builtin/integration/mixins.py | 44 +++++++++++++++++++ InvenTree/plugin/mixins/__init__.py | 4 +- .../integration/custom_panel_sample.py | 22 ++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 InvenTree/plugin/samples/integration/custom_panel_sample.py diff --git a/InvenTree/plugin/builtin/integration/mixins.py b/InvenTree/plugin/builtin/integration/mixins.py index ebe3ebf553..dce48304ec 100644 --- a/InvenTree/plugin/builtin/integration/mixins.py +++ b/InvenTree/plugin/builtin/integration/mixins.py @@ -542,3 +542,47 @@ class APICallMixin: if simple_response: return response.json() return response + + +class PanelMixin: + """ + Mixin which allows integration of custom 'panels' into a particular page. + + The mixin provides a number of key functionalities: + + - Adds an (initially hidden) panel to the page + - Allows rendering of custom templated content to the panel + - Adds a menu item to the 'navbar' on the left side of the screen + - Allows custom javascript to be run when the panel is initially loaded + + The PanelMixin class allows multiple panels to be returned for any page, + and also allows the plugin to return panels for many different pages. + + Any class implementing this mixin must provide the 'get_custom_panels' method, + which dynamically returns the custom panels for a particular page. + + This method is provided with: + + - page: The name of the page e.g. 'part-detail' + - instance: The model instance specific to the page + - request: The request object responsible for the page load + + It must return a list of CustomPanel class instances (see below). + + Note that as this is called dynamically (per request), + then the actual panels returned can vary depending on the particular request or page + + """ + + class CustomPanel: + ... + + class MixinMeta: + MIXIN_NAME = 'Panel' + + def __init__(self): + super().__init__() + self.add_mixin('panel', True, __class__) + + def get_custom_panels(self, page, instance, request): + raise NotImplementedError(f"{__class__} is missing the 'get_custom_panels' method") diff --git a/InvenTree/plugin/mixins/__init__.py b/InvenTree/plugin/mixins/__init__.py index 900289ae37..fdbe863e19 100644 --- a/InvenTree/plugin/mixins/__init__.py +++ b/InvenTree/plugin/mixins/__init__.py @@ -2,7 +2,8 @@ Utility class to enable simpler imports """ -from ..builtin.integration.mixins import APICallMixin, AppMixin, LabelPrintingMixin, SettingsMixin, EventMixin, ScheduleMixin, UrlsMixin, NavigationMixin +from ..builtin.integration.mixins import APICallMixin, AppMixin, LabelPrintingMixin, SettingsMixin, EventMixin, ScheduleMixin, UrlsMixin, NavigationMixin, PanelMixin + from common.notifications import SingleNotificationMethod, BulkNotificationMethod from ..builtin.action.mixins import ActionMixin @@ -17,6 +18,7 @@ __all__ = [ 'ScheduleMixin', 'SettingsMixin', 'UrlsMixin', + 'PanelMixin', 'ActionMixin', 'BarcodeMixin', 'SingleNotificationMethod', diff --git a/InvenTree/plugin/samples/integration/custom_panel_sample.py b/InvenTree/plugin/samples/integration/custom_panel_sample.py new file mode 100644 index 0000000000..c2bae15548 --- /dev/null +++ b/InvenTree/plugin/samples/integration/custom_panel_sample.py @@ -0,0 +1,22 @@ +""" +Sample plugin which renders custom panels on certain pages +""" + +from plugin import IntegrationPluginBase +from plugin.mixins import PanelMixin + + +class CustomPanelSample(PanelMixin, IntegrationPluginBase): + """ + A sample plugin which renders some custom panels. + """ + + PLUGIN_NAME = "CustomPanelExample" + PLUGIN_SLUG = "panel" + PLUGIN_TITLE = "Custom Panel Example" + + def get_custom_panels(self, page, instance, request): + + print("get_custom_panels:") + + return [] \ No newline at end of file