14132 change action_parameters to jsonfield

This commit is contained in:
Arthur 2023-11-21 09:19:24 -08:00
parent 7eb5371600
commit 568a5b8054
4 changed files with 20 additions and 9 deletions

View File

@ -296,7 +296,11 @@ class EventRuleForm(NetBoxModelForm):
choices.append((str(module), scripts))
self.fields['action_choice'].choices = choices
self.fields['action_choice'].initial = get_field_value(self, 'action_parameters')
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
def init_webhook_choice(self):
initial = None
@ -331,12 +335,11 @@ class EventRuleForm(NetBoxModelForm):
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_parameters'] = ''
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_parameters'] = action_choice
self.cleaned_data['action_parameters'] = {'script_choice': action_choice}
return self.cleaned_data

View File

@ -60,7 +60,7 @@ class Migration(migrations.Migration):
('conditions', models.JSONField(blank=True, null=True)),
('action_type', models.CharField(default='webhook', max_length=30)),
('action_object_id', models.PositiveBigIntegerField(blank=True, null=True)),
('action_parameters', models.CharField(blank=True, max_length=80)),
('action_parameters', models.JSONField(blank=True, null=True)),
('action_data', models.JSONField(blank=True, null=True)),
('comments', models.TextField(blank=True)),
],

View File

@ -118,9 +118,9 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged
fk_field='action_object_id'
)
# internal (not show in UI) - used by scripts to store function name
action_parameters = models.CharField(
max_length=80,
blank=True
action_parameters = models.JSONField(
blank=True,
null=True,
)
action_data = models.JSONField(
verbose_name=_('parameters'),

View File

@ -19,8 +19,16 @@ def process_script(event_rule, data, username, **kwargs):
if not event_rule.eval_conditions(data):
return
module_id = event_rule.action_parameters.split(":")[0]
script_name = event_rule.action_parameters.split(":")[1]
script_choice = None
if event_rule.action_parameters and 'script_choice' in event_rule_action_parameters:
script_choice = event_rule.action_parameters['script_choice']
if script_choice:
module_id = script_choice.split(":")[0]
script_name = script_choice.split(":")[1]
else:
logger.warning(f"event run script - event_rule: {event_rule.id} no script_choice selected")
return
try:
module = ScriptModule.objects.get(pk=module_id)