mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-17 13:08:16 -06:00
14438 update EventRule code / form
This commit is contained in:
parent
1a6099fce9
commit
8c2fbceb76
@ -82,9 +82,9 @@ class EventRuleSerializer(NetBoxModelSerializer):
|
|||||||
context = {'request': self.context['request']}
|
context = {'request': self.context['request']}
|
||||||
# We need to manually instantiate the serializer for scripts
|
# We need to manually instantiate the serializer for scripts
|
||||||
if instance.action_type == EventRuleActionChoices.SCRIPT:
|
if instance.action_type == EventRuleActionChoices.SCRIPT:
|
||||||
script_name = instance.action_parameters['script_name']
|
script = instance.action_object
|
||||||
script = instance.action_object.scripts[script_name]()
|
script_name = script.name
|
||||||
return NestedScriptSerializer(script, context=context).data
|
return NestedScriptSerializer(script.python_class(), context=context).data
|
||||||
else:
|
else:
|
||||||
serializer = get_serializer_for_model(
|
serializer = get_serializer_for_model(
|
||||||
model=instance.action_object_type.model_class(),
|
model=instance.action_object_type.model_class(),
|
||||||
|
@ -115,15 +115,13 @@ def process_event_rules(event_rules, model_name, event, data, username=None, sna
|
|||||||
# Scripts
|
# Scripts
|
||||||
elif event_rule.action_type == EventRuleActionChoices.SCRIPT:
|
elif event_rule.action_type == EventRuleActionChoices.SCRIPT:
|
||||||
# Resolve the script from action parameters
|
# Resolve the script from action parameters
|
||||||
script_module = event_rule.action_object
|
script = event_rule.action_object.python_class()
|
||||||
script_name = event_rule.action_parameters['script_name']
|
|
||||||
script = script_module.scripts[script_name]()
|
|
||||||
|
|
||||||
# Enqueue a Job to record the script's execution
|
# Enqueue a Job to record the script's execution
|
||||||
Job.enqueue(
|
Job.enqueue(
|
||||||
"extras.scripts.run_script",
|
"extras.scripts.run_script",
|
||||||
instance=script_module,
|
instance=script.module,
|
||||||
name=script.class_name,
|
name=script.name,
|
||||||
user=user,
|
user=user,
|
||||||
data=data
|
data=data
|
||||||
)
|
)
|
||||||
|
@ -212,11 +212,8 @@ class EventRuleImportForm(NetBoxModelImportForm):
|
|||||||
module, script = get_module_and_script(module_name, script_name)
|
module, script = get_module_and_script(module_name, script_name)
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
raise forms.ValidationError(f"Script {action_object} not found")
|
raise forms.ValidationError(f"Script {action_object} not found")
|
||||||
self.instance.action_object = module
|
self.instance.action_object = script
|
||||||
self.instance.action_object_type = ContentType.objects.get_for_model(module, for_concrete_model=False)
|
self.instance.action_object_type = ContentType.objects.get_for_model(script, for_concrete_model=False)
|
||||||
self.instance.action_parameters = {
|
|
||||||
'script_name': script_name,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class TagImportForm(CSVModelForm):
|
class TagImportForm(CSVModelForm):
|
||||||
|
@ -299,18 +299,17 @@ class EventRuleForm(NetBoxModelForm):
|
|||||||
def init_script_choice(self):
|
def init_script_choice(self):
|
||||||
choices = []
|
choices = []
|
||||||
for module in ScriptModule.objects.all():
|
for module in ScriptModule.objects.all():
|
||||||
scripts = []
|
script_list = []
|
||||||
for script_name in module.scripts.keys():
|
for script in module.scripts.all():
|
||||||
name = f"{str(module.pk)}:{script_name}"
|
name = f"{str(script.pk)}:{script.name}"
|
||||||
scripts.append((name, script_name))
|
script_list.append((name, script.name))
|
||||||
if scripts:
|
if script_list:
|
||||||
choices.append((str(module), scripts))
|
choices.append((str(module), script_list))
|
||||||
self.fields['action_choice'].choices = choices
|
self.fields['action_choice'].choices = choices
|
||||||
|
|
||||||
if self.instance.action_type == EventRuleActionChoices.SCRIPT and self.instance.action_parameters:
|
if self.instance.action_type == EventRuleActionChoices.SCRIPT:
|
||||||
scriptmodule_id = self.instance.action_object_id
|
script = self.instance.action_object
|
||||||
script_name = self.instance.action_parameters.get('script_name')
|
self.fields['action_choice'].initial = f'{script.id}:{script.name}'
|
||||||
self.fields['action_choice'].initial = f'{scriptmodule_id}:{script_name}'
|
|
||||||
|
|
||||||
def init_webhook_choice(self):
|
def init_webhook_choice(self):
|
||||||
initial = None
|
initial = None
|
||||||
@ -348,26 +347,14 @@ class EventRuleForm(NetBoxModelForm):
|
|||||||
# Script
|
# Script
|
||||||
elif self.cleaned_data.get('action_type') == EventRuleActionChoices.SCRIPT:
|
elif self.cleaned_data.get('action_type') == EventRuleActionChoices.SCRIPT:
|
||||||
self.cleaned_data['action_object_type'] = ContentType.objects.get_for_model(
|
self.cleaned_data['action_object_type'] = ContentType.objects.get_for_model(
|
||||||
ScriptModule,
|
Script,
|
||||||
for_concrete_model=False
|
for_concrete_model=False
|
||||||
)
|
)
|
||||||
module_id, script_name = action_choice.split(":", maxsplit=1)
|
script_id, script_name = action_choice.split(":", maxsplit=1)
|
||||||
self.cleaned_data['action_object_id'] = module_id
|
self.cleaned_data['action_object_id'] = script_id
|
||||||
|
|
||||||
return self.cleaned_data
|
return self.cleaned_data
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
|
||||||
# Set action_parameters on the instance
|
|
||||||
if self.cleaned_data['action_type'] == EventRuleActionChoices.SCRIPT:
|
|
||||||
module_id, script_name = self.cleaned_data.get('action_choice').split(":", maxsplit=1)
|
|
||||||
self.instance.action_parameters = {
|
|
||||||
'script_name': script_name,
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
self.instance.action_parameters = None
|
|
||||||
|
|
||||||
return super().save(*args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
class TagForm(forms.ModelForm):
|
class TagForm(forms.ModelForm):
|
||||||
slug = SlugField()
|
slug = SlugField()
|
||||||
|
@ -83,13 +83,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th scope="row">{% trans "Object" %}</th>
|
<th scope="row">{% trans "Object" %}</th>
|
||||||
<td>
|
<td>
|
||||||
{% if object.action_type == 'script' %}
|
|
||||||
<a href="{% url 'extras:script' module=object.action_object.python_name name=object.action_parameters.script_name %}">
|
|
||||||
{{ object.action_object }} / {{ object.action_parameters.script_name }}
|
|
||||||
</a>
|
|
||||||
{% else %}
|
|
||||||
{{ object.action_object|linkify }}
|
{{ object.action_object|linkify }}
|
||||||
{% endif %}
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
Loading…
Reference in New Issue
Block a user