This commit is contained in:
Daniel Sheppard 2024-04-23 15:17:05 -05:00 committed by GitHub
commit be1fdf2c1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 131 additions and 93 deletions

View File

@ -3183,6 +3183,7 @@ class CableView(generic.ObjectView):
class CableEditView(generic.ObjectEditView):
queryset = Cable.objects.all()
template_name = 'dcim/cable_edit.html'
htmx_template_name = 'dcim/htmx/cable_edit.html'
def dispatch(self, request, *args, **kwargs):
@ -3195,22 +3196,27 @@ class CableEditView(generic.ObjectEditView):
return super().dispatch(request, *args, **kwargs)
def get_object(self, **kwargs):
def alter_object(self, obj, request, url_args, url_kwargs):
"""
Hack into get_object() to set the form class when editing an existing Cable, since ObjectEditView
doesn't currently provide a hook for dynamic class resolution.
"""
obj = super().get_object(**kwargs)
a_terminations_type = request.GET.get('a_terminations_type')
b_terminations_type = request.GET.get('b_terminations_type')
if obj.pk:
# TODO: Optimize this logic
termination_a = obj.terminations.filter(cable_end='A').first()
a_type = termination_a.termination._meta.model if termination_a else None
a_type = termination_a.termination._meta.model if termination_a else (
CABLE_TERMINATION_TYPES.get(a_terminations_type)
)
termination_b = obj.terminations.filter(cable_end='B').first()
b_type = termination_b.termination._meta.model if termination_b else None
b_type = termination_b.termination._meta.model if termination_b else (
CABLE_TERMINATION_TYPES.get(b_terminations_type)
)
self.form = forms.get_cable_form(a_type, b_type)
return obj
return super().alter_object(obj, request, url_args, url_kwargs)
def get_extra_addanother_params(self, request):

View File

@ -167,6 +167,7 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView):
"""
template_name = 'generic/object_edit.html'
form = None
htmx_template_name = 'htmx/form.html'
def dispatch(self, request, *args, **kwargs):
# Determine required permission based on whether we are editing an existing object
@ -228,7 +229,7 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView):
# If this is an HTMX request, return only the rendered form HTML
if is_htmx(request):
return render(request, 'htmx/form.html', {
return render(request, self.htmx_template_name, {
'form': form,
})

View File

@ -1,90 +1,5 @@
{% extends 'generic/object_edit.html' %}
{% load static %}
{% load helpers %}
{% load form_helpers %}
{% load i18n %}
{% block form %}
{# A side termination #}
<div class="field-group mb-5">
<div class="row mb-2">
<h5 class="offset-sm-3">{% trans "A Side" %}</h5>
</div>
{% if 'termination_a_device' in form.fields %}
{% render_field form.termination_a_device %}
{% endif %}
{% if 'termination_a_powerpanel' in form.fields %}
{% render_field form.termination_a_powerpanel %}
{% endif %}
{% if 'termination_a_circuit' in form.fields %}
{% render_field form.termination_a_circuit %}
{% endif %}
{% render_field form.a_terminations %}
</div>
{# B side termination #}
<div class="field-group mb-5">
<div class="row mb-2">
<h5 class="offset-sm-3">{% trans "B Side" %}</h5>
</div>
{% if 'termination_b_device' in form.fields %}
{% render_field form.termination_b_device %}
{% endif %}
{% if 'termination_b_powerpanel' in form.fields %}
{% render_field form.termination_b_powerpanel %}
{% endif %}
{% if 'termination_b_circuit' in form.fields %}
{% render_field form.termination_b_circuit %}
{% endif %}
{% render_field form.b_terminations %}
</div>
{# Cable attributes #}
<div class="field-group mb-5">
<div class="row mb-2">
<h5 class="offset-sm-3">{% trans "Cable" %}</h5>
</div>
{% render_field form.status %}
{% render_field form.type %}
{% render_field form.label %}
{% render_field form.description %}
{% render_field form.color %}
<div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">{{ form.length.label }}</label>
<div class="col-md-5">
{{ form.length }}
</div>
<div class="col-md-4">
{{ form.length_unit }}
</div>
<div class="invalid-feedback"></div>
</div>
{% render_field form.tags %}
</div>
<div class="field-group mb-5">
<div class="row mb-2">
<h5 class="offset-sm-3">{% trans "Tenancy" %}</h5>
</div>
{% render_field form.tenant_group %}
{% render_field form.tenant %}
</div>
{% if form.custom_fields %}
<div class="field-group mb-5">
<div class="row mb-2">
<h5 class="offset-sm-3">{% trans "Custom Fields" %}</h5>
</div>
{% render_custom_fields form %}
</div>
{% endif %}
{% if form.comments %}
<div class="field-group mb-5">
<h5 class="text-center">{% trans "Comments" %}</h5>
{% render_field form.comments %}
</div>
{% endif %}
{% include 'dcim/htmx/cable_edit.html' %}
{% endblock %}

View File

@ -0,0 +1,116 @@
{% load static %}
{% load helpers %}
{% load form_helpers %}
{% load i18n %}
{# A side termination #}
<div id="a_termination_block" class="field-group mb-5">
<div class="row mb-2">
<h5 class="offset-sm-3">{% trans "A Side" %}</h5>
</div>
{% if 'termination_a_device' in form.fields %}
{% render_field form.termination_a_device %}
{% endif %}
{% if 'termination_a_powerpanel' in form.fields %}
{% render_field form.termination_a_powerpanel %}
{% endif %}
{% if 'termination_a_circuit' in form.fields %}
{% render_field form.termination_a_circuit %}
{% endif %}
{% if 'a_terminations' in form.fields %}
{% render_field form.a_terminations %}
{% else %}
<div class="row mb-3">
<label for="a_termination_type" class="col-sm-3 col-form-label text-lg-end">{% trans "Termination Type" %}</label>
<div class="col" hx-select="#a_termination_block" hx-target="#a_termination_block" hx-trigger="changed">
<select class="netbox-static-select" hx-get name="a_terminations_type">
<option selected>----</option>
<option value="dcim.interface">{% trans "Interface" %}</option>
<option value="dcim.frontport">{% trans "Front Port" %}</option>
<option value="dcim.rearport">{% trans "Rear Port" %}</option>
<option value="circuits.circuittermination">{% trans "Circuit Termination" %}</option>
</select>
</div>
</div>
{% endif %}
</div>
{# B side termination #}
<div id="b_termination_block" class="field-group mb-5">
<div class="row mb-2">
<h5 class="offset-sm-3">{% trans "B Side" %}</h5>
</div>
{% if 'termination_b_device' in form.fields %}
{% render_field form.termination_b_device %}
{% endif %}
{% if 'termination_b_powerpanel' in form.fields %}
{% render_field form.termination_b_powerpanel %}
{% endif %}
{% if 'termination_b_circuit' in form.fields %}
{% render_field form.termination_b_circuit %}
{% endif %}
{% if 'b_terminations' in form.fields %}
{% render_field form.b_terminations %}
{% else %}
<div class="row mb-3">
<label for="b_termination_type" class="col-sm-3 col-form-label text-lg-end">{% trans "Termination Type" %}</label>
<div class="col" hx-select="#b_termination_block" hx-target="#b_termination_block" hx-trigger="changed">
<select class="netbox-static-select" hx-get name="b_terminations_type">
<option selected>----</option>
<option value="dcim.interface">{% trans "Interface" %}</option>
<option value="dcim.frontport">{% trans "Front Port" %}</option>
<option value="dcim.rearport">{% trans "Rear Port" %}</option>
<option value="circuits.circuittermination">{% trans "Circuit Termination" %}</option>
</select>
</div>
</div>
{% endif %}
</div>
{# Cable attributes #}
<div class="field-group mb-5">
<div class="row mb-2">
<h5 class="offset-sm-3">{% trans "Cable" %}</h5>
</div>
{% render_field form.status %}
{% render_field form.type %}
{% render_field form.label %}
{% render_field form.description %}
{% render_field form.color %}
<div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">{{ form.length.label }}</label>
<div class="col-md-5">
{{ form.length }}
</div>
<div class="col-md-4">
{{ form.length_unit }}
</div>
<div class="invalid-feedback"></div>
</div>
{% render_field form.tags %}
</div>
<div class="field-group mb-5">
<div class="row mb-2">
<h5 class="offset-sm-3">{% trans "Tenancy" %}</h5>
</div>
{% render_field form.tenant_group %}
{% render_field form.tenant %}
</div>
{% if form.custom_fields %}
<div class="field-group mb-5">
<div class="row mb-2">
<h5 class="offset-sm-3">{% trans "Custom Fields" %}</h5>
</div>
{% render_custom_fields form %}
</div>
{% endif %}
{% if form.comments %}
<div class="field-group mb-5">
<h5 class="text-center">{% trans "Comments" %}</h5>
{% render_field form.comments %}
</div>
{% endif %}