mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-08 16:48:16 -06:00
Move event registration to core
This commit is contained in:
parent
c22b7052ff
commit
f1297382b9
@ -18,7 +18,7 @@ class CoreConfig(AppConfig):
|
|||||||
def ready(self):
|
def ready(self):
|
||||||
from core.api import schema # noqa
|
from core.api import schema # noqa
|
||||||
from netbox.models.features import register_models
|
from netbox.models.features import register_models
|
||||||
from . import data_backends, search
|
from . import data_backends, events, search
|
||||||
|
|
||||||
# Register models
|
# Register models
|
||||||
register_models(*self.get_models())
|
register_models(*self.get_models())
|
||||||
|
16
netbox/core/events.py
Normal file
16
netbox/core/events.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
|
from netbox.events import Event
|
||||||
|
|
||||||
|
OBJECT_CREATED = 'object_created'
|
||||||
|
OBJECT_UPDATED = 'object_updated'
|
||||||
|
OBJECT_DELETED = 'object_deleted'
|
||||||
|
JOB_STARTED = 'job_started'
|
||||||
|
JOB_ENDED = 'job_ended'
|
||||||
|
|
||||||
|
# Register core events
|
||||||
|
Event(name=OBJECT_CREATED, text=_('Object created')).register()
|
||||||
|
Event(name=OBJECT_UPDATED, text=_('Object updated')).register()
|
||||||
|
Event(name=OBJECT_DELETED, text=_('Object deleted')).register()
|
||||||
|
Event(name=JOB_STARTED, text=_('Job started')).register()
|
||||||
|
Event(name=JOB_ENDED, text=_('Job ended')).register()
|
@ -9,27 +9,20 @@ from django.utils.translation import gettext as _
|
|||||||
from django_rq import get_queue
|
from django_rq import get_queue
|
||||||
|
|
||||||
from core.choices import ObjectChangeActionChoices
|
from core.choices import ObjectChangeActionChoices
|
||||||
|
from core.events import JOB_ENDED, JOB_STARTED, OBJECT_CREATED, OBJECT_DELETED, OBJECT_UPDATED
|
||||||
from core.models import Job
|
from core.models import Job
|
||||||
|
from extras.constants import EVENT_CREATE, EVENT_DELETE, EVENT_JOB_END, EVENT_JOB_START, EVENT_UPDATE
|
||||||
from netbox.config import get_config
|
from netbox.config import get_config
|
||||||
from netbox.constants import RQ_QUEUE_DEFAULT
|
from netbox.constants import RQ_QUEUE_DEFAULT
|
||||||
from netbox.events import Event
|
|
||||||
from netbox.registry import registry
|
from netbox.registry import registry
|
||||||
from utilities.api import get_serializer_for_model
|
from utilities.api import get_serializer_for_model
|
||||||
from utilities.rqworker import get_rq_retry
|
from utilities.rqworker import get_rq_retry
|
||||||
from utilities.serialization import serialize_object
|
from utilities.serialization import serialize_object
|
||||||
from .choices import EventRuleActionChoices
|
from .choices import EventRuleActionChoices
|
||||||
from .constants import EVENT_CREATE, EVENT_DELETE, EVENT_JOB_END, EVENT_JOB_START, EVENT_UPDATE
|
|
||||||
from .models import EventRule
|
from .models import EventRule
|
||||||
|
|
||||||
logger = logging.getLogger('netbox.events_processor')
|
logger = logging.getLogger('netbox.events_processor')
|
||||||
|
|
||||||
# Register event types
|
|
||||||
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):
|
def serialize_for_event(instance):
|
||||||
"""
|
"""
|
||||||
@ -145,10 +138,18 @@ def process_event_rules(event_rules, object_type, event, data, username=None, sn
|
|||||||
# Notification groups
|
# Notification groups
|
||||||
elif event_rule.action_type == EventRuleActionChoices.NOTIFICATION:
|
elif event_rule.action_type == EventRuleActionChoices.NOTIFICATION:
|
||||||
# Bulk-create notifications for all members of the notification group
|
# Bulk-create notifications for all members of the notification group
|
||||||
|
# TODO: Support dynamic events upstream
|
||||||
|
event_name = {
|
||||||
|
EVENT_CREATE: OBJECT_CREATED,
|
||||||
|
EVENT_UPDATE: OBJECT_UPDATED,
|
||||||
|
EVENT_DELETE: OBJECT_DELETED,
|
||||||
|
EVENT_JOB_START: JOB_STARTED,
|
||||||
|
EVENT_JOB_END: JOB_ENDED,
|
||||||
|
}[event]
|
||||||
event_rule.action_object.notify(
|
event_rule.action_object.notify(
|
||||||
object_type=object_type,
|
object_type=object_type,
|
||||||
object_id=data['id'],
|
object_id=data['id'],
|
||||||
event_name=event
|
event_name=event_name
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -10,9 +10,10 @@ from django.utils.translation import gettext_lazy as _
|
|||||||
from django_prometheus.models import model_deletes, model_inserts, model_updates
|
from django_prometheus.models import model_deletes, model_inserts, model_updates
|
||||||
|
|
||||||
from core.choices import ObjectChangeActionChoices
|
from core.choices import ObjectChangeActionChoices
|
||||||
|
from core.events import OBJECT_UPDATED
|
||||||
from core.models import ObjectChange, ObjectType
|
from core.models import ObjectChange, ObjectType
|
||||||
from core.signals import job_end, job_start
|
from core.signals import job_end, job_start
|
||||||
from extras.constants import EVENT_JOB_END, EVENT_JOB_START, EVENT_UPDATE
|
from extras.constants import EVENT_JOB_END, EVENT_JOB_START
|
||||||
from extras.events import process_event_rules
|
from extras.events import process_event_rules
|
||||||
from extras.models import EventRule, Notification, Subscription
|
from extras.models import EventRule, Notification, Subscription
|
||||||
from netbox.config import get_config
|
from netbox.config import get_config
|
||||||
@ -309,7 +310,7 @@ def notify_object_changed(sender, instance, created, raw, **kwargs):
|
|||||||
Notification(
|
Notification(
|
||||||
user_id=sub['user'],
|
user_id=sub['user'],
|
||||||
object=instance,
|
object=instance,
|
||||||
event_name=EVENT_UPDATE
|
event_name=OBJECT_UPDATED
|
||||||
)
|
)
|
||||||
for sub in subscriptions
|
for sub in subscriptions
|
||||||
]
|
]
|
||||||
|
@ -2,15 +2,6 @@ from dataclasses import dataclass
|
|||||||
|
|
||||||
from netbox.registry import registry
|
from netbox.registry import registry
|
||||||
|
|
||||||
__all__ = (
|
|
||||||
'EVENT_TYPE_DANGER',
|
|
||||||
'EVENT_TYPE_INFO',
|
|
||||||
'EVENT_TYPE_SUCCESS',
|
|
||||||
'EVENT_TYPE_WARNING',
|
|
||||||
'Event',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
EVENT_TYPE_INFO = 'info'
|
EVENT_TYPE_INFO = 'info'
|
||||||
EVENT_TYPE_SUCCESS = 'success'
|
EVENT_TYPE_SUCCESS = 'success'
|
||||||
EVENT_TYPE_WARNING = 'warning'
|
EVENT_TYPE_WARNING = 'warning'
|
||||||
|
Loading…
Reference in New Issue
Block a user