How to use Custom Event Types #19715
Replies: 2 comments
-
Same question. Default events seems to handled via signals, but how to actually trigger a custom event in the plugin code? |
Beta Was this translation helpful? Give feedback.
-
Hi, I figured this out, sadly it is not documented anywhere. You need to enqueue your event in the Netbox Event Queue, I looked through the Netbox source code and implemented it like it's done in the handle_changed_object receiver. Basically I created a file In the same file I added a receiver function which enqueues the event in Netbox' event queue from django.dispatch import Signal, receiver
from netbox.context import events_queue, current_request
expirystatus = Signal()
@receiver((expirystatus,))
def handle_expirystatus(sender, instance, **kwargs):
from extras.events import enqueue_event
queue = events_queue.get()
request = current_request.get()
enqueue_event(queue, instance, request.user, request.id, 'expirystatus')
events_queue.set(queue) After that you can call your signal from anywhere you want by using from yourplugin import signals
signals.expirystatus.send(sender=self, instance=YourModelInstance) This will allow Netbox to handle your event. From there on you can navigate to 'Event Rules' in the Web UI and do what ever you want with your new event. In my case I added a button to a view which runs a custom script. I do this through a POST request, which also allows me to easily hand the request.user and request.id over to the event queue by using |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi All,
I'm wondering if anyone can help me find documentation on how to implement Custom Event types in a plugin.
I was able to create a new Event type, and it is visible in netbox using the events.py file.
The only info i can find is this: https://netboxlabs.com/docs/netbox/plugins/development/event-types/
`
events.py
from django.utils.translation import gettext as _
from netbox.events import EventType, EVENT_TYPE_KIND_WARNING
EventType(
name='expirystatus',
text=_('ExpiryStatus'),
kind=EVENT_TYPE_KIND_WARNING,
).register()
`
But how can i define when this Event Type needs to be triggered?
I would like to trigger this event every time a field "expiry status" changes.
Beta Was this translation helpful? Give feedback.
All reactions