Define dynamic choices for Notification.event_name

This commit is contained in:
Jeremy Stretch 2024-07-04 15:38:08 -04:00
parent 71860ba985
commit c22b7052ff
2 changed files with 15 additions and 2 deletions

View File

@ -18,7 +18,7 @@ from utilities.api import get_serializer_for_model
from utilities.rqworker import get_rq_retry
from utilities.serialization import serialize_object
from .choices import EventRuleActionChoices
from .constants import EVENT_CREATE, EVENT_DELETE, EVENT_UPDATE
from .constants import EVENT_CREATE, EVENT_DELETE, EVENT_JOB_END, EVENT_JOB_START, EVENT_UPDATE
from .models import EventRule
logger = logging.getLogger('netbox.events_processor')
@ -27,6 +27,8 @@ logger = logging.getLogger('netbox.events_processor')
Event(name=EVENT_CREATE, text=_('Object created')).register()
Event(name=EVENT_UPDATE, text=_('Object updated')).register()
Event(name=EVENT_DELETE, text=_('Object deleted')).register()
Event(name=EVENT_JOB_START, text=_('Job started')).register()
Event(name=EVENT_JOB_END, text=_('Job ended')).register()
def serialize_for_event(instance):

View File

@ -21,6 +21,16 @@ __all__ = (
)
def get_event_type_choices():
"""
Compile a list of choices from all registered event types
"""
return [
(name, event.text)
for name, event in registry['events'].items()
]
class Notification(models.Model):
"""
A notification message for a User relating to a specific object in NetBox.
@ -49,7 +59,8 @@ class Notification(models.Model):
)
event_name = models.CharField(
verbose_name=_('event'),
max_length=50
max_length=50,
choices=get_event_type_choices
)
objects = NotificationQuerySet.as_manager()