8356 htmx

This commit is contained in:
Arthur 2023-11-09 09:48:16 -08:00
parent 4f3b086f3f
commit a679740626
2 changed files with 37 additions and 37 deletions

View File

@ -14,12 +14,12 @@ from extras.utils import FeatureQuery
from netbox.config import get_config, PARAMS from netbox.config import get_config, PARAMS
from netbox.forms import NetBoxModelForm from netbox.forms import NetBoxModelForm
from tenancy.models import Tenant, TenantGroup from tenancy.models import Tenant, TenantGroup
from utilities.forms import BootstrapMixin, add_blank_choice from utilities.forms import BootstrapMixin, add_blank_choice, get_field_value
from utilities.forms.fields import ( from utilities.forms.fields import (
CommentField, ContentTypeChoiceField, ContentTypeMultipleChoiceField, DynamicModelChoiceField, CommentField, ContentTypeChoiceField, ContentTypeMultipleChoiceField, DynamicModelChoiceField,
DynamicModelMultipleChoiceField, JSONField, SlugField, DynamicModelMultipleChoiceField, JSONField, SlugField,
) )
from utilities.forms.widgets import ChoicesWidget from utilities.forms.widgets import ChoicesWidget, HTMXSelect
from virtualization.models import Cluster, ClusterGroup, ClusterType from virtualization.models import Cluster, ClusterGroup, ClusterType
@ -245,28 +245,16 @@ class EventRuleForm(NetBoxModelForm):
queryset=ContentType.objects.all(), queryset=ContentType.objects.all(),
limit_choices_to=FeatureQuery('webhooks') limit_choices_to=FeatureQuery('webhooks')
) )
action_choice = forms.ChoiceField(
# Webhook form fields label=_('Action choice'),
# choices=[]
payload_url = Webhook._meta.get_field('payload_url').formfield() )
http_method = Webhook._meta.get_field('http_method').formfield()
http_content_type = Webhook._meta.get_field('http_content_type').formfield()
additional_headers = Webhook._meta.get_field('additional_headers').formfield()
body_template = Webhook._meta.get_field('body_template').formfield()
secret = Webhook._meta.get_field('secret').formfield()
ssl_verification = Webhook._meta.get_field('ssl_verification').formfield()
ca_file_path = Webhook._meta.get_field('ca_file_path').formfield()
fieldsets = ( fieldsets = (
(_('EventRule'), ('name', 'content_types', 'enabled', 'tags')), (_('EventRule'), ('name', 'content_types', 'enabled', 'tags')),
(_('Events'), ('type_create', 'type_update', 'type_delete', 'type_job_start', 'type_job_end')), (_('Events'), ('type_create', 'type_update', 'type_delete', 'type_job_start', 'type_job_end')),
(_('Conditions'), ('conditions',)), (_('Conditions'), ('conditions',)),
(_('Action'), ('action_type',)), (_('Action'), ('action_type', 'action_choice', 'parameters')),
(_('HTTP Request'), (
'payload_url', 'http_method', 'http_content_type', 'additional_headers', 'body_template', 'secret',
)),
(_('SSL'), ('ssl_verification', 'ca_file_path')),
) )
class Meta: class Meta:
@ -281,28 +269,40 @@ class EventRuleForm(NetBoxModelForm):
} }
widgets = { widgets = {
'conditions': forms.Textarea(attrs={'class': 'font-monospace'}), 'conditions': forms.Textarea(attrs={'class': 'font-monospace'}),
'action_type': HTMXSelect(),
} }
def get_script_choices(self):
choices = []
idx = 0
for module in ScriptModule.objects.all():
scripts = []
for script_name in module.scripts.keys():
name = f"{str(module).lower()}:{script_name.lower()}"
scripts.append((name, script_name))
if scripts:
choices.append((str(module), scripts))
self.fields['action_choice'].choices = choices
def get_webhook_choices(self):
self.fields['action_choice'] = DynamicModelChoiceField(
label=_('Webhook'),
queryset=Webhook.objects.all(),
required=True
)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
SCRIPT_CHOICES = [ # Determine the action type
( action_type = get_field_value(self, 'action_type')
"Audio",
( if action_type == EventRuleActionChoices.WEBHOOK:
("vinyl", "Vinyl"), self.get_webhook_choices()
("cd", "CD"), elif action_type == EventRuleActionChoices.SCRIPT:
), self.get_script_choices()
),
(
"Video",
(
("vhs", "VHS Tape"),
("dvd", "DVD"),
),
),
("unknown", "Unknown"),
]
class TagForm(BootstrapMixin, forms.ModelForm): class TagForm(BootstrapMixin, forms.ModelForm):

View File

@ -35,7 +35,7 @@ def move_webhooks(apps, schema_editor):
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('contenttypes', '0002_remove_content_type_name'), ('contenttypes', '0002_remove_content_type_name'),
('extras', '0098_webhook_custom_field_data_webhook_tags'), ('extras', '0099_cachedvalue_ordering'),
] ]
operations = [ operations = [