Skip to content

Commit 00807d1

Browse files
Fixes #14550: Fix changing event rule action type from webhook to script (#14571)
* Fixes #14550: Fix changing event rule action type from webhook to script * Remove action_parameters from form; set on instance under save()
1 parent 58f925c commit 00807d1

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

netbox/extras/forms/model_forms.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ class EventRuleForm(NetBoxModelForm):
269269
(_('Events'), ('type_create', 'type_update', 'type_delete', 'type_job_start', 'type_job_end')),
270270
(_('Conditions'), ('conditions',)),
271271
(_('Action'), (
272-
'action_type', 'action_choice', 'action_parameters', 'action_object_type', 'action_object_id',
273-
'action_data',
272+
'action_type', 'action_choice', 'action_object_type', 'action_object_id', 'action_data',
274273
)),
275274
)
276275

@@ -279,7 +278,7 @@ class Meta:
279278
fields = (
280279
'content_types', 'name', 'description', 'type_create', 'type_update', 'type_delete', 'type_job_start',
281280
'type_job_end', 'enabled', 'conditions', 'action_type', 'action_object_type', 'action_object_id',
282-
'action_parameters', 'action_data', 'comments', 'tags'
281+
'action_data', 'comments', 'tags'
283282
)
284283
labels = {
285284
'type_create': _('Creations'),
@@ -293,7 +292,6 @@ class Meta:
293292
'action_type': HTMXSelect(),
294293
'action_object_type': forms.HiddenInput,
295294
'action_object_id': forms.HiddenInput,
296-
'action_parameters': forms.HiddenInput,
297295
}
298296

299297
def init_script_choice(self):
@@ -307,16 +305,16 @@ def init_script_choice(self):
307305
choices.append((str(module), scripts))
308306
self.fields['action_choice'].choices = choices
309307

310-
if self.instance.pk:
308+
if self.instance.action_type == EventRuleActionChoices.SCRIPT and self.instance.action_parameters:
311309
scriptmodule_id = self.instance.action_object_id
312310
script_name = self.instance.action_parameters.get('script_name')
313311
self.fields['action_choice'].initial = f'{scriptmodule_id}:{script_name}'
314-
print(self.fields['action_choice'].initial)
315312

316313
def init_webhook_choice(self):
317314
initial = None
318-
if self.fields['action_object_type'] and get_field_value(self, 'action_object_id'):
319-
initial = Webhook.objects.get(pk=get_field_value(self, 'action_object_id'))
315+
if self.instance.action_type == EventRuleActionChoices.WEBHOOK:
316+
webhook_id = get_field_value(self, 'action_object_id')
317+
initial = Webhook.objects.get(pk=webhook_id) if webhook_id else None
320318
self.fields['action_choice'] = DynamicModelChoiceField(
321319
label=_('Webhook'),
322320
queryset=Webhook.objects.all(),
@@ -353,11 +351,20 @@ def clean(self):
353351
)
354352
module_id, script_name = action_choice.split(":", maxsplit=1)
355353
self.cleaned_data['action_object_id'] = module_id
356-
self.cleaned_data['action_parameters'] = {
354+
355+
return self.cleaned_data
356+
357+
def save(self, *args, **kwargs):
358+
# Set action_parameters on the instance
359+
if self.cleaned_data['action_type'] == EventRuleActionChoices.SCRIPT:
360+
module_id, script_name = self.cleaned_data.get('action_choice').split(":", maxsplit=1)
361+
self.instance.action_parameters = {
357362
'script_name': script_name,
358363
}
364+
else:
365+
self.instance.action_parameters = None
359366

360-
return self.cleaned_data
367+
return super().save(*args, **kwargs)
361368

362369

363370
class TagForm(BootstrapMixin, forms.ModelForm):

0 commit comments

Comments
 (0)