From 69a31e100728d00040506cb637d48299e988d78a Mon Sep 17 00:00:00 2001 From: Dillon Henschen Date: Mon, 22 May 2023 01:07:57 -0400 Subject: [PATCH] Leverage HTMX to semplify rendering of Circuit Termination Form Associated with issue #11891 --- netbox/circuits/choices.py | 10 ++++ netbox/circuits/forms/model_forms.py | 24 ++++++-- ...043_circuittermination_termination_type.py | 18 ++++++ netbox/circuits/models/circuits.py | 5 ++ netbox/circuits/views.py | 1 - .../circuits/circuittermination_edit.html | 57 ------------------- 6 files changed, 53 insertions(+), 62 deletions(-) create mode 100644 netbox/circuits/migrations/0043_circuittermination_termination_type.py delete mode 100644 netbox/templates/circuits/circuittermination_edit.html diff --git a/netbox/circuits/choices.py b/netbox/circuits/choices.py index 518baea9f01..f541ae52294 100644 --- a/netbox/circuits/choices.py +++ b/netbox/circuits/choices.py @@ -70,3 +70,13 @@ class CircuitTerminationPortSpeedChoices(ChoiceSet): (1544, 'T1 (1.544 Mbps)'), (2048, 'E1 (2.048 Mbps)'), ] + + +class CircuitTerminationTypeChoices(ChoiceSet): + SITE = 'site' + PROVIDER_NETWORK = 'provider_network' + + CHOICES = [ + (SITE, 'Site'), + (PROVIDER_NETWORK, 'Provider Network'), + ] diff --git a/netbox/circuits/forms/model_forms.py b/netbox/circuits/forms/model_forms.py index d3929c08a5f..60e607c7f81 100644 --- a/netbox/circuits/forms/model_forms.py +++ b/netbox/circuits/forms/model_forms.py @@ -1,13 +1,15 @@ +from django import forms from django.utils.translation import gettext as _ -from circuits.choices import CircuitCommitRateChoices, CircuitTerminationPortSpeedChoices +from circuits.choices import CircuitCommitRateChoices, CircuitTerminationPortSpeedChoices, CircuitTerminationTypeChoices from circuits.models import * from dcim.models import Site from ipam.models import ASN from netbox.forms import NetBoxModelForm from tenancy.forms import TenancyForm from utilities.forms.fields import CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, SlugField -from utilities.forms.widgets import DatePicker, NumberWithOptions +from utilities.forms.utils import get_field_value +from utilities.forms.widgets import DatePicker, HTMXSelect, NumberWithOptions __all__ = ( 'CircuitForm', @@ -139,11 +141,21 @@ class CircuitTerminationForm(NetBoxModelForm): selector=True ) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + termination_type = get_field_value(self, 'termination_type') + + if termination_type != CircuitTerminationTypeChoices.SITE: + del self.fields['site'] + if termination_type != CircuitTerminationTypeChoices.PROVIDER_NETWORK: + del self.fields['provider_network'] + class Meta: model = CircuitTermination fields = [ - 'circuit', 'term_side', 'site', 'provider_network', 'mark_connected', 'port_speed', 'upstream_speed', - 'xconnect_id', 'pp_info', 'description', 'tags', + 'circuit', 'term_side', 'termination_type', 'site', 'provider_network', 'mark_connected', 'port_speed', + 'upstream_speed', 'xconnect_id', 'pp_info', 'description', 'tags', ] widgets = { 'port_speed': NumberWithOptions( @@ -152,4 +164,8 @@ class Meta: 'upstream_speed': NumberWithOptions( options=CircuitTerminationPortSpeedChoices ), + 'termination_type': HTMXSelect(), + } + labels = { + 'termination_type': 'Termination Type', } diff --git a/netbox/circuits/migrations/0043_circuittermination_termination_type.py b/netbox/circuits/migrations/0043_circuittermination_termination_type.py new file mode 100644 index 00000000000..131cd0b9e9b --- /dev/null +++ b/netbox/circuits/migrations/0043_circuittermination_termination_type.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.9 on 2023-05-22 04:54 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('circuits', '0042_provideraccount'), + ] + + operations = [ + migrations.AddField( + model_name='circuittermination', + name='termination_type', + field=models.CharField(default='site', max_length=50), + ), + ] diff --git a/netbox/circuits/models/circuits.py b/netbox/circuits/models/circuits.py index f629c0b3008..73759d961a1 100644 --- a/netbox/circuits/models/circuits.py +++ b/netbox/circuits/models/circuits.py @@ -164,6 +164,11 @@ class CircuitTermination( choices=CircuitTerminationSideChoices, verbose_name='Termination' ) + termination_type = models.CharField( + max_length=50, + choices=CircuitTerminationTypeChoices, + default=CircuitTerminationTypeChoices.SITE + ) site = models.ForeignKey( to='dcim.Site', on_delete=models.PROTECT, diff --git a/netbox/circuits/views.py b/netbox/circuits/views.py index f1cfdd1d5d3..f023e5a4bed 100644 --- a/netbox/circuits/views.py +++ b/netbox/circuits/views.py @@ -412,7 +412,6 @@ class CircuitContactsView(ObjectContactsView): class CircuitTerminationEditView(generic.ObjectEditView): queryset = CircuitTermination.objects.all() form = forms.CircuitTerminationForm - template_name = 'circuits/circuittermination_edit.html' @register_model_view(CircuitTermination, 'delete') diff --git a/netbox/templates/circuits/circuittermination_edit.html b/netbox/templates/circuits/circuittermination_edit.html deleted file mode 100644 index 63da79a36c8..00000000000 --- a/netbox/templates/circuits/circuittermination_edit.html +++ /dev/null @@ -1,57 +0,0 @@ -{% extends 'generic/object_edit.html' %} -{% load static %} -{% load form_helpers %} - -{% block form %} -
-
-
Circuit Termination
-
- {% render_field form.circuit %} - {% render_field form.term_side %} - {% render_field form.tags %} - {% render_field form.mark_connected %} - {% with providernetwork_tab_active=form.initial.provider_network %} -
-
- -
-
-
-
- {% render_field form.site %} -
-
- {% render_field form.provider_network %} -
-
- {% endwith %} -
- -
-
-
Termination Details
-
- {% render_field form.port_speed %} - {% render_field form.upstream_speed %} - {% render_field form.xconnect_id %} - {% render_field form.pp_info %} - {% render_field form.description %} -
- - {% if form.custom_fields %} -
-
-
Custom Fields
-
- {% render_custom_fields form %} -
- {% endif %} -{% endblock %}