mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 20:45:44 +00:00
Let plugins decide if events should be processed or not (#5618)
* add wants_process_event to EventMixin in register_event this function get called and the task is just offload if the plugin returns true * add tests for wants_process_event * fix comments for register_event after code changes * add documentation for EventMixin.wants_process_event * avoid pre-formatting log messages --------- Co-authored-by: Oliver Lippert <oliver@lipperts-web.de>
This commit is contained in:
32
InvenTree/plugin/samples/event/filtered_event_sample.py
Normal file
32
InvenTree/plugin/samples/event/filtered_event_sample.py
Normal file
@ -0,0 +1,32 @@
|
||||
"""Sample plugin which responds to events."""
|
||||
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from plugin import InvenTreePlugin
|
||||
from plugin.mixins import EventMixin
|
||||
|
||||
logger = logging.getLogger('inventree')
|
||||
|
||||
|
||||
class FilteredEventPluginSample(EventMixin, InvenTreePlugin):
|
||||
"""A sample plugin which provides supports for triggered events."""
|
||||
|
||||
NAME = "FilteredEventPlugin"
|
||||
SLUG = "filteredsampleevent"
|
||||
TITLE = "Triggered by test.event only"
|
||||
|
||||
def wants_process_event(self, event):
|
||||
"""Return whether given event should be processed or not."""
|
||||
return event == "test.event"
|
||||
|
||||
def process_event(self, event, *args, **kwargs):
|
||||
"""Custom event processing."""
|
||||
print(f"Processing triggered event: '{event}'")
|
||||
print("args:", str(args))
|
||||
print("kwargs:", str(kwargs))
|
||||
|
||||
# Issue warning that we can test for
|
||||
if settings.PLUGIN_TESTING:
|
||||
logger.debug('Event `%s` triggered in sample plugin', event)
|
52
InvenTree/plugin/samples/event/test_filtered_event_sample.py
Normal file
52
InvenTree/plugin/samples/event/test_filtered_event_sample.py
Normal file
@ -0,0 +1,52 @@
|
||||
"""Unit tests for event_sample sample plugins."""
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
from common.models import InvenTreeSetting
|
||||
from plugin import registry
|
||||
from plugin.base.event.events import trigger_event
|
||||
|
||||
from .filtered_event_sample import logger
|
||||
|
||||
|
||||
class FilteredEventPluginSampleTests(TestCase):
|
||||
"""Tests for EventPluginSample."""
|
||||
|
||||
def test_run_event(self):
|
||||
"""Check if the event is issued."""
|
||||
# Activate plugin
|
||||
config = registry.get_plugin('filteredsampleevent').plugin_config()
|
||||
config.active = True
|
||||
config.save()
|
||||
|
||||
InvenTreeSetting.set_setting('ENABLE_PLUGINS_EVENTS', True, change_user=None)
|
||||
|
||||
# Enable event testing
|
||||
settings.PLUGIN_TESTING_EVENTS = True
|
||||
# Check that an event is issued
|
||||
with self.assertLogs(logger=logger, level="DEBUG") as cm:
|
||||
trigger_event('test.event')
|
||||
self.assertIn('DEBUG:inventree:Event `test.event` triggered in sample plugin', cm[1])
|
||||
|
||||
# Disable again
|
||||
settings.PLUGIN_TESTING_EVENTS = False
|
||||
|
||||
def test_ignore_event(self):
|
||||
"""Check if the event is issued."""
|
||||
# Activate plugin
|
||||
config = registry.get_plugin('filteredsampleevent').plugin_config()
|
||||
config.active = True
|
||||
config.save()
|
||||
|
||||
InvenTreeSetting.set_setting('ENABLE_PLUGINS_EVENTS', True, change_user=None)
|
||||
|
||||
# Enable event testing
|
||||
settings.PLUGIN_TESTING_EVENTS = True
|
||||
# Check that an event is issued
|
||||
with self.assertLogs(logger=logger, level="DEBUG") as cm:
|
||||
trigger_event('test.some.other.event')
|
||||
self.assertNotIn('DEBUG:inventree:Event `test.some.other.event` triggered in sample plugin', cm[1])
|
||||
|
||||
# Disable again
|
||||
settings.PLUGIN_TESTING_EVENTS = False
|
Reference in New Issue
Block a user