From 970503f424a2144ea24077d24a4fa56b276d04a6 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 11 May 2022 13:28:59 +0200 Subject: [PATCH] refactor events --- InvenTree/build/models.py | 2 +- InvenTree/order/models.py | 2 +- InvenTree/plugin/__init__.py | 6 +++- InvenTree/plugin/{ => base/event}/events.py | 0 InvenTree/plugin/base/event/mixins.py | 29 +++++++++++++++++++ InvenTree/plugin/base/integration/mixins.py | 26 ----------------- InvenTree/plugin/mixins/__init__.py | 3 +- InvenTree/plugin/samples/event/__init__.py | 0 .../{integration => event}/event_sample.py | 0 InvenTree/stock/models.py | 2 +- 10 files changed, 39 insertions(+), 31 deletions(-) rename InvenTree/plugin/{ => base/event}/events.py (100%) create mode 100644 InvenTree/plugin/base/event/mixins.py create mode 100644 InvenTree/plugin/samples/event/__init__.py rename InvenTree/plugin/samples/{integration => event}/event_sample.py (100%) diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index a1517d73dd..8b4ee5e0b2 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -36,7 +36,7 @@ import InvenTree.fields import InvenTree.helpers import InvenTree.tasks -from plugin.events import trigger_event +from plugin import trigger_event from part import models as PartModels from stock import models as StockModels diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index 6ca5b7a293..0bf14cffc8 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -30,7 +30,7 @@ from users import models as UserModels from part import models as PartModels from stock import models as stock_models from company.models import Company, SupplierPart -from plugin.events import trigger_event +from plugin import trigger_event import InvenTree.helpers from InvenTree.fields import InvenTreeModelMoneyField, RoundingDecimalField diff --git a/InvenTree/plugin/__init__.py b/InvenTree/plugin/__init__.py index 35a6687628..3e2aa48b47 100644 --- a/InvenTree/plugin/__init__.py +++ b/InvenTree/plugin/__init__.py @@ -3,16 +3,20 @@ Utility file to enable simper imports """ from .registry import registry +from .base.event.events import trigger_event + from .plugin import InvenTreePluginBase, IntegrationPluginBase from .base.action.action import ActionPlugin from .helpers import MixinNotImplementedError, MixinImplementationError __all__ = [ + 'registry', + 'trigger_event', + 'ActionPlugin', 'IntegrationPluginBase', 'InvenTreePluginBase', - 'registry', 'MixinNotImplementedError', 'MixinImplementationError', ] diff --git a/InvenTree/plugin/events.py b/InvenTree/plugin/base/event/events.py similarity index 100% rename from InvenTree/plugin/events.py rename to InvenTree/plugin/base/event/events.py diff --git a/InvenTree/plugin/base/event/mixins.py b/InvenTree/plugin/base/event/mixins.py new file mode 100644 index 0000000000..8df9bfd164 --- /dev/null +++ b/InvenTree/plugin/base/event/mixins.py @@ -0,0 +1,29 @@ +"""Plugin mixin class for events""" + +from plugin.helpers import MixinNotImplementedError + + +class EventMixin: + """ + Mixin that provides support for responding to triggered events. + + Implementing classes must provide a "process_event" function: + """ + + def process_event(self, event, *args, **kwargs): + """ + Function to handle events + Must be overridden by plugin + """ + # Default implementation does not do anything + raise MixinNotImplementedError + + class MixinMeta: + """ + Meta options for this mixin + """ + MIXIN_NAME = 'Events' + + def __init__(self): + super().__init__() + self.add_mixin('events', True, __class__) diff --git a/InvenTree/plugin/base/integration/mixins.py b/InvenTree/plugin/base/integration/mixins.py index b22efc9415..5bae993e61 100644 --- a/InvenTree/plugin/base/integration/mixins.py +++ b/InvenTree/plugin/base/integration/mixins.py @@ -238,32 +238,6 @@ class ScheduleMixin: logger.warning("unregister_tasks failed, database not ready") -class EventMixin: - """ - Mixin that provides support for responding to triggered events. - - Implementing classes must provide a "process_event" function: - """ - - def process_event(self, event, *args, **kwargs): - """ - Function to handle events - Must be overridden by plugin - """ - # Default implementation does not do anything - raise MixinNotImplementedError - - class MixinMeta: - """ - Meta options for this mixin - """ - MIXIN_NAME = 'Events' - - def __init__(self): - super().__init__() - self.add_mixin('events', True, __class__) - - class UrlsMixin: """ Mixin that enables custom URLs for the plugin diff --git a/InvenTree/plugin/mixins/__init__.py b/InvenTree/plugin/mixins/__init__.py index af20a05491..8683b5c45f 100644 --- a/InvenTree/plugin/mixins/__init__.py +++ b/InvenTree/plugin/mixins/__init__.py @@ -2,12 +2,13 @@ Utility class to enable simpler imports """ -from ..base.integration.mixins import APICallMixin, AppMixin, LabelPrintingMixin, SettingsMixin, EventMixin, ScheduleMixin, UrlsMixin, NavigationMixin, PanelMixin +from ..base.integration.mixins import APICallMixin, AppMixin, LabelPrintingMixin, SettingsMixin, ScheduleMixin, UrlsMixin, NavigationMixin, PanelMixin from common.notifications import SingleNotificationMethod, BulkNotificationMethod from ..base.action.mixins import ActionMixin from ..base.barcodes.mixins import BarcodeMixin +from ..base.events.mixins import EventMixin __all__ = [ 'APICallMixin', diff --git a/InvenTree/plugin/samples/event/__init__.py b/InvenTree/plugin/samples/event/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/InvenTree/plugin/samples/integration/event_sample.py b/InvenTree/plugin/samples/event/event_sample.py similarity index 100% rename from InvenTree/plugin/samples/integration/event_sample.py rename to InvenTree/plugin/samples/event/event_sample.py diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 53e1321e1a..cf240fc5dc 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -38,7 +38,7 @@ import common.models import report.models import label.models -from plugin.events import trigger_event +from plugin import trigger_event from InvenTree.status_codes import StockStatus, StockHistoryCode from InvenTree.models import InvenTreeTree, InvenTreeAttachment