Restrict A terminations by parent object

This commit is contained in:
jeremystretch 2022-05-03 09:53:01 -04:00
parent c3742f63fd
commit f0fc8bf2cf
3 changed files with 17 additions and 20 deletions

View File

@ -159,7 +159,7 @@ class LinkTermination(models.Model):
@property @property
def parent_object(self): def parent_object(self):
raise NotImplementedError("CableTermination models must implement parent_object()") raise NotImplementedError(f"{self.__class__.__name__} models must declare a parent_object property")
@property @property
def link(self): def link(self):

View File

@ -2846,17 +2846,20 @@ class CableCreateView(generic.ObjectEditView):
# initial_data['termination_b_rack'] = getattr(obj.termination_a.parent_object, 'rack', None) # initial_data['termination_b_rack'] = getattr(obj.termination_a.parent_object, 'rack', None)
form = self.form(instance=obj, initial=initial_data) form = self.form(instance=obj, initial=initial_data)
# TODO Find a better way to infer the near-end parent object
termination_a = kwargs['termination_a_type'].objects.filter(pk=int(initial_data['a_terminations'])).first()
# Set the queryset of termination A # Set the queryset of termination A
form.fields['a_terminations'].queryset = kwargs['termination_a_type'].objects.all() form.fields['a_terminations'].queryset = kwargs['termination_a_type'].objects.all()
form.fields['a_terminations'].widget.add_query_params({
# TODO Find a better way to infer the near-end parent object 'device_id': termination_a.device_id,
termination_a = kwargs['termination_a_type'].objects.filter(pk__in=initial_data['a_terminations']).first() })
return render(request, self.template_name, { return render(request, self.template_name, {
'obj': obj, 'obj': obj,
'obj_type': Cable._meta.verbose_name, 'obj_type': Cable._meta.verbose_name,
'termination_a_type': kwargs['termination_a_type']._meta.model_name, 'termination_a_type': kwargs['termination_a_type']._meta.model_name,
'termination_a': termination_a, 'termination_a_parent': termination_a.parent_object,
'termination_b_type': termination_b_type.name, 'termination_b_type': termination_b_type.name,
'form': form, 'form': form,
'return_url': self.get_return_url(request, obj), 'return_url': self.get_return_url(request, obj),

View File

@ -28,22 +28,16 @@
<div class="card-body"> <div class="card-body">
{% if termination_a_type == 'circuit' %} {% if termination_a_type == 'circuit' %}
{# Circuit termination #} {# Circuit termination #}
<div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">Site</label>
<div class="col">
<input class="form-control" value="{{ termination_a.site }}" disabled />
</div>
</div>
<div class="row mb-3"> <div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">Provider</label> <label class="col-sm-3 col-form-label text-lg-end">Provider</label>
<div class="col"> <div class="col">
<input class="form-control" value="{{ termination_a.circuit.provider }}" disabled /> <input class="form-control" value="{{ termination_a_parent.provider }}" disabled />
</div> </div>
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">Circuit</label> <label class="col-sm-3 col-form-label text-lg-end">Circuit</label>
<div class="col"> <div class="col">
<input class="form-control" value="{{ termination_a.circuit.cid }}" disabled /> <input class="form-control" value="{{ termination_a_parent.cid }}" disabled />
</div> </div>
</div> </div>
{% else %} {% else %}
@ -51,43 +45,43 @@
<div class="row mb-3"> <div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">Region</label> <label class="col-sm-3 col-form-label text-lg-end">Region</label>
<div class="col"> <div class="col">
<input class="form-control" value="{{ termination_a.device.site.region }}" disabled /> <input class="form-control" value="{{ termination_a_parent.site.region }}" disabled />
</div> </div>
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">Site Group</label> <label class="col-sm-3 col-form-label text-lg-end">Site Group</label>
<div class="col"> <div class="col">
<input class="form-control" value="{{ termination_a.device.site.group }}" disabled /> <input class="form-control" value="{{ termination_a_parent.site.group }}" disabled />
</div> </div>
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">Site</label> <label class="col-sm-3 col-form-label text-lg-end">Site</label>
<div class="col"> <div class="col">
<input class="form-control" value="{{ termination_a.device.site }}" disabled /> <input class="form-control" value="{{ termination_a_parent.site }}" disabled />
</div> </div>
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">Location</label> <label class="col-sm-3 col-form-label text-lg-end">Location</label>
<div class="col"> <div class="col">
<input class="form-control" value="{{ termination_a.device.location|default:"None" }}" disabled /> <input class="form-control" value="{{ termination_a_parent.location|default:"None" }}" disabled />
</div> </div>
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">Rack</label> <label class="col-sm-3 col-form-label text-lg-end">Rack</label>
<div class="col"> <div class="col">
<input class="form-control" value="{{ termination_a.device.rack|default:"None" }}" disabled /> <input class="form-control" value="{{ termination_a_parent.rack|default:"None" }}" disabled />
</div> </div>
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">Device</label> <label class="col-sm-3 col-form-label text-lg-end">Device</label>
<div class="col"> <div class="col">
<input class="form-control" value="{{ termination_a.device }}" disabled /> <input class="form-control" value="{{ termination_a_parent }}" disabled />
</div> </div>
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end">Type</label> <label class="col-sm-3 col-form-label text-lg-end">Type</label>
<div class="col"> <div class="col">
<input class="form-control" value="{{ termination_a|meta:"verbose_name"|capfirst }}" disabled /> <input class="form-control" value="{{ termination_a_type|capfirst }}" disabled />
</div> </div>
</div> </div>
{% endif %} {% endif %}