Skip to content

Commit

Permalink
14852 delete event-rule when delete script
Browse files Browse the repository at this point in the history
  • Loading branch information
arthanson authored and jeremystretch committed Apr 29, 2024
1 parent 79b9dc2 commit 4b21cf6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions netbox/extras/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ def get_action_object(self, instance):
# We need to manually instantiate the serializer for scripts
if instance.action_type == EventRuleActionChoices.SCRIPT:
script_name = instance.action_parameters['script_name']
script = instance.action_object.scripts[script_name]()
return NestedScriptSerializer(script, context=context).data
if script_name in instance.action_object.scripts:
script = instance.action_object.scripts[script_name]()
return NestedScriptSerializer(script, context=context).data
else:
return None
else:
serializer = get_serializer_for_model(
model=instance.action_object_type.model_class(),
Expand Down
8 changes: 8 additions & 0 deletions netbox/extras/models/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from functools import cached_property

from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -41,6 +42,13 @@ class ScriptModule(PythonModuleMixin, JobsMixin, ManagedFile):
"""
objects = ScriptModuleManager()

event_rules = GenericRelation(
to='extras.EventRule',
content_type_field='action_object_type',
object_id_field='action_object_id',
for_concrete_model=False
)

class Meta:
proxy = True
verbose_name = _('script module')
Expand Down

0 comments on commit 4b21cf6

Please sign in to comment.