mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-13 02:58:17 -06:00
Infer A/B termination types from POST data
This commit is contained in:
parent
fc3e52d37b
commit
297ab4a9db
@ -109,7 +109,7 @@ def get_cable_form(a_type, b_type):
|
|||||||
super().clean()
|
super().clean()
|
||||||
|
|
||||||
# Set the A/B terminations on the Cable instance
|
# Set the A/B terminations on the Cable instance
|
||||||
self.instance.a_terminations = self.cleaned_data['a_terminations']
|
self.instance.a_terminations = self.cleaned_data.get('a_terminations', [])
|
||||||
self.instance.b_terminations = self.cleaned_data['b_terminations']
|
self.instance.b_terminations = self.cleaned_data.get('b_terminations', [])
|
||||||
|
|
||||||
return _CableForm
|
return _CableForm
|
||||||
|
@ -13,8 +13,7 @@ from netbox.forms import NetBoxModelForm
|
|||||||
from tenancy.forms import TenancyForm
|
from tenancy.forms import TenancyForm
|
||||||
from utilities.forms import BootstrapMixin, add_blank_choice
|
from utilities.forms import BootstrapMixin, add_blank_choice
|
||||||
from utilities.forms.fields import (
|
from utilities.forms.fields import (
|
||||||
CommentField, ContentTypeChoiceField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, JSONField,
|
CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, JSONField, NumericArrayField, SlugField,
|
||||||
NumericArrayField, SlugField,
|
|
||||||
)
|
)
|
||||||
from utilities.forms.widgets import APISelect, ClearableFileInput, HTMXSelect, NumberWithOptions, SelectWithPK
|
from utilities.forms.widgets import APISelect, ClearableFileInput, HTMXSelect, NumberWithOptions, SelectWithPK
|
||||||
from virtualization.models import Cluster
|
from virtualization.models import Cluster
|
||||||
@ -617,13 +616,10 @@ class ModuleForm(ModuleCommonForm, NetBoxModelForm):
|
|||||||
|
|
||||||
|
|
||||||
def get_termination_type_choices():
|
def get_termination_type_choices():
|
||||||
return [
|
return add_blank_choice([
|
||||||
(None, '---------'),
|
(f'{ct.app_label}.{ct.model}', ct.model_class()._meta.verbose_name.title())
|
||||||
*[
|
for ct in ContentType.objects.filter(CABLE_TERMINATION_MODELS)
|
||||||
(f'{ct.app_label}.{ct.model}', ct.model_class()._meta.verbose_name.title())
|
])
|
||||||
for ct in ContentType.objects.filter(CABLE_TERMINATION_MODELS)
|
|
||||||
]
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class CableForm(TenancyForm, NetBoxModelForm):
|
class CableForm(TenancyForm, NetBoxModelForm):
|
||||||
|
@ -3201,18 +3201,17 @@ class CableEditView(generic.ObjectEditView):
|
|||||||
Hack into get_object() to set the form class when editing an existing Cable, since ObjectEditView
|
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.
|
doesn't currently provide a hook for dynamic class resolution.
|
||||||
"""
|
"""
|
||||||
a_terminations_type = request.GET.get('a_terminations_type')
|
a_terminations_type = request.POST.get('a_terminations_type') or request.GET.get('a_terminations_type')
|
||||||
b_terminations_type = request.GET.get('b_terminations_type')
|
b_terminations_type = request.POST.get('b_terminations_type') or request.GET.get('b_terminations_type')
|
||||||
if obj.pk:
|
if obj.pk:
|
||||||
# TODO: Optimize this logic
|
if not a_terminations_type and (termination_a := obj.terminations.filter(cable_end='A').first()):
|
||||||
termination_a = obj.terminations.filter(cable_end='A').first()
|
a_type = termination_a.termination._meta.model
|
||||||
a_type = termination_a.termination._meta.model if termination_a else (
|
else:
|
||||||
CABLE_TERMINATION_TYPES.get(a_terminations_type)
|
a_type = CABLE_TERMINATION_TYPES.get(a_terminations_type)
|
||||||
)
|
if not b_terminations_type and (termination_b := obj.terminations.filter(cable_end='B').first()):
|
||||||
termination_b = obj.terminations.filter(cable_end='B').first()
|
b_type = termination_b.termination._meta.model
|
||||||
b_type = termination_b.termination._meta.model if termination_b else (
|
else:
|
||||||
CABLE_TERMINATION_TYPES.get(b_terminations_type)
|
b_type = CABLE_TERMINATION_TYPES.get(b_terminations_type)
|
||||||
)
|
|
||||||
|
|
||||||
self.form = forms.get_cable_form(a_type, b_type)
|
self.form = forms.get_cable_form(a_type, b_type)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user