From 1453419779da34cf51b5e1f10c9139276dbb9d58 Mon Sep 17 00:00:00 2001 From: Arthur Date: Thu, 9 Nov 2023 13:01:10 -0800 Subject: [PATCH] 8356 fix form, init fields --- netbox/extras/forms/model_forms.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/netbox/extras/forms/model_forms.py b/netbox/extras/forms/model_forms.py index b6f18bae4..9fc51d98b 100644 --- a/netbox/extras/forms/model_forms.py +++ b/netbox/extras/forms/model_forms.py @@ -2,6 +2,7 @@ import json from django import forms from django.conf import settings +from django.contrib.contenttypes.models import ContentType from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ @@ -291,14 +292,20 @@ class EventRuleForm(NetBoxModelForm): self.fields['action_choice'].initial = get_field_value(self, 'action_object_identifier') def get_webhook_choices(self): + initial = None + if self.fields['action_object_type'] and self.fields['action_object_id']: + initial = Webhook.objects.get(pk=get_field_value(self, 'action_object_id')) self.fields['action_choice'] = DynamicModelChoiceField( label=_('Webhook'), queryset=Webhook.objects.all(), - required=True + required=True, + initial=initial ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.fields['action_object_type'].required = False + self.fields['action_object_id'].required = False # Determine the action type action_type = get_field_value(self, 'action_type') @@ -308,6 +315,22 @@ class EventRuleForm(NetBoxModelForm): elif action_type == EventRuleActionChoices.SCRIPT: self.get_script_choices() + def clean(self): + super().clean() + + action_choice = self.cleaned_data.get('action_choice') + 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 + self.cleaned_data['action_object_identifier'] = '' + elif self.cleaned_data.get('action_type') == EventRuleActionChoices.SCRIPT: + script = ScriptModule.objects.get(pk=action_choice.split(":")[0]) + self.cleaned_data['action_object_type'] = ContentType.objects.get_for_model(script) + self.cleaned_data['action_object_id'] = script.id + self.cleaned_data['action_object_identifier'] = action_choice + + return self.cleaned_data + class TagForm(BootstrapMixin, forms.ModelForm): slug = SlugField()