Skip to content

Commit

Permalink
#16886: Rename Event to EventType
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Jul 31, 2024
1 parent 5cfa2bb commit d1e1601
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 45 deletions.
6 changes: 3 additions & 3 deletions docs/plugins/development/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
```

Expand Down
16 changes: 8 additions & 8 deletions netbox/core/events.py
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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()
4 changes: 2 additions & 2 deletions netbox/extras/models/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
]


Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion netbox/extras/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
}
Expand Down
60 changes: 30 additions & 30 deletions netbox/netbox/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,74 @@

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.
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)
2 changes: 1 addition & 1 deletion netbox/netbox/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit d1e1601

Please sign in to comment.