Skip to content

Commit

Permalink
Leverage HTMX to semplify rendering of Circuit Termination Form
Browse files Browse the repository at this point in the history
Associated with issue netbox-community#11891
  • Loading branch information
dhenschen committed May 22, 2023
1 parent c8d9a3b commit 69a31e1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 62 deletions.
10 changes: 10 additions & 0 deletions netbox/circuits/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]
24 changes: 20 additions & 4 deletions netbox/circuits/forms/model_forms.py
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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(
Expand All @@ -152,4 +164,8 @@ class Meta:
'upstream_speed': NumberWithOptions(
options=CircuitTerminationPortSpeedChoices
),
'termination_type': HTMXSelect(),
}
labels = {
'termination_type': 'Termination Type',
}
Original file line number Diff line number Diff line change
@@ -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),
),
]
5 changes: 5 additions & 0 deletions netbox/circuits/models/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion netbox/circuits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
57 changes: 0 additions & 57 deletions netbox/templates/circuits/circuittermination_edit.html

This file was deleted.

0 comments on commit 69a31e1

Please sign in to comment.