diff --git a/docs/plugins/development/events.md b/docs/plugins/development/events.md index f41ed7ff5fa..bbf9783e236 100644 --- a/docs/plugins/development/events.md +++ b/docs/plugins/development/events.md @@ -4,12 +4,12 @@ Plugins can register their own custom event types for use with NetBox [event rul ```python from django.utils.translation import gettext_lazy as _ -from netbox.events import Event, EVENT_TYPE_SUCCESS +from netbox.events import EventType, EVENT_TYPE_KIND_SUCCESS -Event( +EventType( name='ticket_opened', text=_('Ticket opened'), - type=EVENT_TYPE_SUCCESS + type=EVENT_TYPE_KIND_SUCCESS ).register() ``` diff --git a/netbox/core/events.py b/netbox/core/events.py index 4855e5b2325..98a1ea18fb3 100644 --- a/netbox/core/events.py +++ b/netbox/core/events.py @@ -1,6 +1,6 @@ from django.utils.translation import gettext as _ -from netbox.events import Event, EVENT_TYPE_DANGER, EVENT_TYPE_SUCCESS, EVENT_TYPE_WARNING +from netbox.events import EventType, EVENT_TYPE_KIND_DANGER, EVENT_TYPE_KIND_SUCCESS, EVENT_TYPE_KIND_WARNING __all__ = ( 'JOB_COMPLETED', @@ -24,10 +24,10 @@ JOB_ERRORED = 'job_errored' # Register core events -Event(OBJECT_CREATED, _('Object created')).register() -Event(OBJECT_UPDATED, _('Object updated')).register() -Event(OBJECT_DELETED, _('Object deleted')).register() -Event(JOB_STARTED, _('Job started')).register() -Event(JOB_COMPLETED, _('Job completed'), type=EVENT_TYPE_SUCCESS).register() -Event(JOB_FAILED, _('Job failed'), type=EVENT_TYPE_WARNING).register() -Event(JOB_ERRORED, _('Job errored'), type=EVENT_TYPE_DANGER).register() +EventType(OBJECT_CREATED, _('Object created')).register() +EventType(OBJECT_UPDATED, _('Object updated')).register() +EventType(OBJECT_DELETED, _('Object deleted')).register() +EventType(JOB_STARTED, _('Job started')).register() +EventType(JOB_COMPLETED, _('Job completed'), kind=EVENT_TYPE_KIND_SUCCESS).register() +EventType(JOB_FAILED, _('Job failed'), kind=EVENT_TYPE_KIND_WARNING).register() +EventType(JOB_ERRORED, _('Job errored'), kind=EVENT_TYPE_KIND_DANGER).register() diff --git a/netbox/extras/models/notifications.py b/netbox/extras/models/notifications.py index dba059ea757..a15dfe9aab9 100644 --- a/netbox/extras/models/notifications.py +++ b/netbox/extras/models/notifications.py @@ -27,7 +27,7 @@ def get_event_type_choices(): """ return [ (name, event.text) - for name, event in registry['events'].items() + for name, event in registry['event_types'].items() ] @@ -102,7 +102,7 @@ def event(self): """ Returns the registered Event which triggered this Notification. """ - return registry['events'].get(self.event_type) + return registry['event_types'].get(self.event_type) class NotificationGroup(ChangeLoggedModel): diff --git a/netbox/extras/views.py b/netbox/extras/views.py index 54f00265afe..2a5b6ef0538 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -554,7 +554,7 @@ class EventRuleView(generic.ObjectView): def get_extra_context(self, request, instance): return { 'event_types': [ - event for name, event in registry['events'].items() + event for name, event in registry['event_types'].items() if name in instance.event_types ] } diff --git a/netbox/netbox/events.py b/netbox/netbox/events.py index ee2561720d8..2fa4b6e7855 100644 --- a/netbox/netbox/events.py +++ b/netbox/netbox/events.py @@ -2,41 +2,41 @@ from netbox.registry import registry -EVENT_TYPE_INFO = 'info' -EVENT_TYPE_SUCCESS = 'success' -EVENT_TYPE_WARNING = 'warning' -EVENT_TYPE_DANGER = 'danger' +EVENT_TYPE_KIND_INFO = 'info' +EVENT_TYPE_KIND_SUCCESS = 'success' +EVENT_TYPE_KIND_WARNING = 'warning' +EVENT_TYPE_KIND_DANGER = 'danger' __all__ = ( - 'EVENT_TYPE_DANGER', - 'EVENT_TYPE_INFO', - 'EVENT_TYPE_SUCCESS', - 'EVENT_TYPE_WARNING', - 'Event', - 'get_event', + 'EVENT_TYPE_KIND_DANGER', + 'EVENT_TYPE_KIND_INFO', + 'EVENT_TYPE_KIND_SUCCESS', + 'EVENT_TYPE_KIND_WARNING', + 'EventType', + 'get_event_type', 'get_event_type_choices', 'get_event_text', ) -def get_event(name): - return registry['events'].get(name) +def get_event_type(name): + return registry['event_types'].get(name) def get_event_text(name): - if event := registry['events'].get(name): + if event := registry['event_types'].get(name): return event.text return '' def get_event_type_choices(): return [ - (event.name, event.text) for event in registry['events'].values() + (event.name, event.text) for event in registry['event_types'].values() ] @dataclass -class Event: +class EventType: """ A type of event which can occur in NetBox. Event rules can be defined to automatically perform some action in response to an event. @@ -44,32 +44,32 @@ class Event: Args: name: The unique name under which the event is registered. text: The human-friendly event name. This should support translation. - type: The event's classification (info, success, warning, or danger). The default type is info. + kind: The event's classification (info, success, warning, or danger). The default type is info. """ name: str text: str - type: str = EVENT_TYPE_INFO + kind: str = EVENT_TYPE_KIND_INFO def __str__(self): return self.text def register(self): - if self.name in registry['events']: - raise Exception(f"An event named {self.name} has already been registered!") - registry['events'][self.name] = self + if self.name in registry['event_types']: + raise Exception(f"An event type named {self.name} has already been registered!") + registry['event_types'][self.name] = self def color(self): return { - EVENT_TYPE_INFO: 'blue', - EVENT_TYPE_SUCCESS: 'green', - EVENT_TYPE_WARNING: 'orange', - EVENT_TYPE_DANGER: 'red', - }.get(self.type) + EVENT_TYPE_KIND_INFO: 'blue', + EVENT_TYPE_KIND_SUCCESS: 'green', + EVENT_TYPE_KIND_WARNING: 'orange', + EVENT_TYPE_KIND_DANGER: 'red', + }.get(self.kind) def icon(self): return { - EVENT_TYPE_INFO: 'mdi mdi-information', - EVENT_TYPE_SUCCESS: 'mdi mdi-check-circle', - EVENT_TYPE_WARNING: 'mdi mdi-alert-box', - EVENT_TYPE_DANGER: 'mdi mdi-alert-octagon', - }.get(self.type) + EVENT_TYPE_KIND_INFO: 'mdi mdi-information', + EVENT_TYPE_KIND_SUCCESS: 'mdi mdi-check-circle', + EVENT_TYPE_KIND_WARNING: 'mdi mdi-alert-box', + EVENT_TYPE_KIND_DANGER: 'mdi mdi-alert-octagon', + }.get(self.kind) diff --git a/netbox/netbox/registry.py b/netbox/netbox/registry.py index 44cdfb92be3..0920cbccf96 100644 --- a/netbox/netbox/registry.py +++ b/netbox/netbox/registry.py @@ -25,7 +25,7 @@ def __delitem__(self, key): 'counter_fields': collections.defaultdict(dict), 'data_backends': dict(), 'denormalized_fields': collections.defaultdict(list), - 'events': dict(), + 'event_types': dict(), 'model_features': dict(), 'models': collections.defaultdict(set), 'plugins': dict(),