From 5d57e9863dcb524bc0fdaea0287d118c18325611 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 4 Dec 2023 10:57:29 -0500 Subject: [PATCH] #14132: Simplify form logic for script EventRules --- netbox/extras/api/serializers.py | 2 +- netbox/extras/events.py | 2 +- netbox/extras/forms/bulk_import.py | 8 ++++---- netbox/extras/forms/model_forms.py | 28 ++++++++++++++-------------- netbox/extras/models/models.py | 7 +++---- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/netbox/extras/api/serializers.py b/netbox/extras/api/serializers.py index ffd0df9ab..60a30aed2 100644 --- a/netbox/extras/api/serializers.py +++ b/netbox/extras/api/serializers.py @@ -86,7 +86,7 @@ class EventRuleSerializer(NetBoxModelSerializer): context = {'request': self.context['request']} # We need to manually instantiate the serializer for scripts if instance.action_type == EventRuleActionChoices.SCRIPT: - module_id, script_name = instance.action_parameters['script_choice'].split(":", maxsplit=1) + script_name = instance.action_parameters['script_name'] script = instance.action_object.scripts[script_name]() return NestedScriptSerializer(script, context=context).data else: diff --git a/netbox/extras/events.py b/netbox/extras/events.py index 1d7a7ed64..6d0654929 100644 --- a/netbox/extras/events.py +++ b/netbox/extras/events.py @@ -116,7 +116,7 @@ def process_event_rules(event_rules, model_name, event, data, username, snapshot elif event_rule.action_type == EventRuleActionChoices.SCRIPT: # Resolve the script from action parameters script_module = event_rule.action_object - _, script_name = event_rule.action_parameters['script_choice'].split(":", maxsplit=1) + script_name = event_rule.action_parameters['script_name'] script = script_module.scripts[script_name]() # Enqueue a Job to record the script's execution diff --git a/netbox/extras/forms/bulk_import.py b/netbox/extras/forms/bulk_import.py index e08a6528d..243d8fa4c 100644 --- a/netbox/extras/forms/bulk_import.py +++ b/netbox/extras/forms/bulk_import.py @@ -179,12 +179,14 @@ class EventRuleImportForm(NetBoxModelImportForm): action_object = self.cleaned_data.get('action_object') action_type = self.cleaned_data.get('action_type') if action_object and action_type: + # Webhook if action_type == EventRuleActionChoices.WEBHOOK: try: webhook = Webhook.objects.get(name=action_object) - except Webhook.ObjectDoesNotExist: + except Webhook.DoesNotExist: raise forms.ValidationError(f"Webhook {action_object} not found") self.instance.action_object = webhook + # Script elif action_type == EventRuleActionChoices.SCRIPT: from extras.scripts import get_module_and_script module_name, script_name = action_object.split('.', 1) @@ -195,9 +197,7 @@ class EventRuleImportForm(NetBoxModelImportForm): self.instance.action_object = module self.instance.action_object_type = ContentType.objects.get_for_model(module, for_concrete_model=False) self.instance.action_parameters = { - 'script_choice': f"{str(module.pk)}:{script_name}", - 'script_name': script.name, - 'script_full_name': script.full_name, + 'script_name': script_name, } diff --git a/netbox/extras/forms/model_forms.py b/netbox/extras/forms/model_forms.py index 9403165e9..8a5d319d3 100644 --- a/netbox/extras/forms/model_forms.py +++ b/netbox/extras/forms/model_forms.py @@ -288,16 +288,15 @@ class EventRuleForm(NetBoxModelForm): for script_name in module.scripts.keys(): name = f"{str(module.pk)}:{script_name}" scripts.append((name, script_name)) - if scripts: choices.append((str(module), scripts)) - self.fields['action_choice'].choices = choices - parameters = get_field_value(self, 'action_parameters') - initial = None - if parameters and 'script_choice' in parameters: - initial = parameters['script_choice'] - self.fields['action_choice'].initial = initial + + if self.instance.pk: + scriptmodule_id = self.instance.action_object_id + script_name = self.instance.action_parameters.get('script_name') + self.fields['action_choice'].initial = f'{scriptmodule_id}:{script_name}' + print(self.fields['action_choice'].initial) def init_webhook_choice(self): initial = None @@ -327,19 +326,20 @@ class EventRuleForm(NetBoxModelForm): super().clean() action_choice = self.cleaned_data.get('action_choice') + # Webhook if self.cleaned_data.get('action_type') == EventRuleActionChoices.WEBHOOK: self.cleaned_data['action_object_type'] = ContentType.objects.get_for_model(action_choice) self.cleaned_data['action_object_id'] = action_choice.id + # Script elif self.cleaned_data.get('action_type') == EventRuleActionChoices.SCRIPT: + self.cleaned_data['action_object_type'] = ContentType.objects.get_for_model( + ScriptModule, + for_concrete_model=False + ) module_id, script_name = action_choice.split(":", maxsplit=1) - script_module = ScriptModule.objects.get(pk=module_id) - self.cleaned_data['action_object_type'] = ContentType.objects.get_for_model(script_module, for_concrete_model=False) - self.cleaned_data['action_object_id'] = script_module.id - script = script_module.scripts[script_name]() + self.cleaned_data['action_object_id'] = module_id self.cleaned_data['action_parameters'] = { - 'script_choice': action_choice, - 'script_name': script.name, - 'script_full_name': script.full_name, + 'script_name': script_name, } return self.cleaned_data diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index f996b50b5..21319400c 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -115,16 +115,15 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged ct_field='action_object_type', fk_field='action_object_id' ) - # internal (not show in UI) - used by scripts to store function name action_parameters = models.JSONField( blank=True, - null=True, + null=True ) action_data = models.JSONField( - verbose_name=_('parameters'), + verbose_name=_('data'), blank=True, null=True, - help_text=_("Parameters to pass to the action.") + help_text=_("Additional data to pass to the action object") ) comments = models.TextField( verbose_name=_('comments'),