mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
#14132: Simplify form logic for script EventRules
This commit is contained in:
netbox/extras
@ -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:
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
}
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
@ -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'),
|
||||
|
Reference in New Issue
Block a user