mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-22 15:36:44 -06:00
8356 fix form, init fields
This commit is contained in:
parent
73047c979b
commit
1453419779
@ -2,6 +2,7 @@ import json
|
|||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
from django.utils.translation import gettext_lazy as _
|
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')
|
self.fields['action_choice'].initial = get_field_value(self, 'action_object_identifier')
|
||||||
|
|
||||||
def get_webhook_choices(self):
|
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(
|
self.fields['action_choice'] = DynamicModelChoiceField(
|
||||||
label=_('Webhook'),
|
label=_('Webhook'),
|
||||||
queryset=Webhook.objects.all(),
|
queryset=Webhook.objects.all(),
|
||||||
required=True
|
required=True,
|
||||||
|
initial=initial
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
self.fields['action_object_type'].required = False
|
||||||
|
self.fields['action_object_id'].required = False
|
||||||
|
|
||||||
# Determine the action type
|
# Determine the action type
|
||||||
action_type = get_field_value(self, 'action_type')
|
action_type = get_field_value(self, 'action_type')
|
||||||
@ -308,6 +315,22 @@ class EventRuleForm(NetBoxModelForm):
|
|||||||
elif action_type == EventRuleActionChoices.SCRIPT:
|
elif action_type == EventRuleActionChoices.SCRIPT:
|
||||||
self.get_script_choices()
|
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):
|
class TagForm(BootstrapMixin, forms.ModelForm):
|
||||||
slug = SlugField()
|
slug = SlugField()
|
||||||
|
Loading…
Reference in New Issue
Block a user