From 12b4c484a1d9982d46a460d7ecb1580e5d0d55cf Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 29 Nov 2023 09:04:28 -0800 Subject: [PATCH] 14132 fix bulk import --- netbox/extras/forms/bulk_import.py | 41 +++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/netbox/extras/forms/bulk_import.py b/netbox/extras/forms/bulk_import.py index efd331b5c..ded12260f 100644 --- a/netbox/extras/forms/bulk_import.py +++ b/netbox/extras/forms/bulk_import.py @@ -1,5 +1,6 @@ from django import forms from django.contrib.postgres.forms import SimpleArrayField +from django.core.exceptions import ObjectDoesNotExist from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ @@ -159,14 +160,52 @@ class EventRuleImportForm(NetBoxModelImportForm): queryset=ContentType.objects.with_feature('event_rules'), help_text=_("One or more assigned object types") ) + action_object = forms.CharField( + label=_('Action object'), + required=True, + help_text=_('Webhook name or script as dotted path module.Class') + ) class Meta: model = EventRule fields = ( 'name', 'description', 'enabled', 'conditions', 'content_types', 'type_create', 'type_update', - 'type_delete', 'type_job_start', 'type_job_end', 'comments', 'tags' + 'type_delete', 'type_job_start', 'type_job_end', 'action_type', 'action_object', 'comments', 'tags' ) + def clean(self): + super().clean() + + action_object = self.cleaned_data.get('action_object') + action_type = self.cleaned_data.get('action_type') + if action_object and action_type: + if action_type == EventRuleActionChoices.WEBHOOK: + webhook = Webhook.objects.filter(name=action_object) + if not webhook: + raise forms.ValidationError(f"Webhook {action_object} not found") + elif action_type == EventRuleActionChoices.SCRIPT: + from extras.scripts import get_module_and_script + module_name, script_name = action_object.split('.', 1) + try: + module, script = get_module_and_script(module_name, script_name) + except ObjectDoesNotExist: + raise forms.ValidationError(f"Script {action_object} not found") + + def save(self, *args, **kwargs): + action_object = self.cleaned_data.get('action_object') + action_type = self.cleaned_data.get('action_type') + + if action_type == EventRuleActionChoices.WEBHOOK: + self.instance.action_object = Webhook.objects.get(name=action_object) + elif action_type == EventRuleActionChoices.SCRIPT: + from extras.scripts import get_module_and_script + module_name, script_name = action_object.split('.', 1) + module, script = get_module_and_script(module_name, script_name) + self.instance.action_object = module + self.instance.action_parameters = {'script_choice': f"{str(module.pk)}:{script_name}"} + + return super().save(*args, **kwargs) + class TagImportForm(CSVModelForm): slug = SlugField()