Просмотр исходного кода

Restrict A terminations by parent object

jeremystretch 3 лет назад
Родитель
Сommit
f0fc8bf2cf

+ 1 - 1
netbox/dcim/models/device_components.py

@@ -159,7 +159,7 @@ class LinkTermination(models.Model):
 
     @property
     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
     def link(self):

+ 7 - 4
netbox/dcim/views.py

@@ -2846,17 +2846,20 @@ class CableCreateView(generic.ObjectEditView):
         #     initial_data['termination_b_rack'] = getattr(obj.termination_a.parent_object, 'rack', None)
         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
         form.fields['a_terminations'].queryset = kwargs['termination_a_type'].objects.all()
-
-        # TODO Find a better way to infer the near-end parent object
-        termination_a = kwargs['termination_a_type'].objects.filter(pk__in=initial_data['a_terminations']).first()
+        form.fields['a_terminations'].widget.add_query_params({
+            'device_id': termination_a.device_id,
+        })
 
         return render(request, self.template_name, {
             'obj': obj,
             'obj_type': Cable._meta.verbose_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,
             'form': form,
             'return_url': self.get_return_url(request, obj),

+ 9 - 15
netbox/templates/dcim/cable_connect.html

@@ -28,22 +28,16 @@
                   <div class="card-body">
                       {% if termination_a_type == 'circuit' %}
                           {# 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">
                               <label class="col-sm-3 col-form-label text-lg-end">Provider</label>
                               <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 class="row mb-3">
                               <label class="col-sm-3 col-form-label text-lg-end">Circuit</label>
                               <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>
                       {% else %}
@@ -51,43 +45,43 @@
                           <div class="row mb-3">
                               <label class="col-sm-3 col-form-label text-lg-end">Region</label>
                               <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 class="row mb-3">
                               <label class="col-sm-3 col-form-label text-lg-end">Site Group</label>
                               <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 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.device.site }}" disabled />
+                                  <input class="form-control" value="{{ termination_a_parent.site }}" disabled />
                               </div>
                           </div>
                           <div class="row mb-3">
                               <label class="col-sm-3 col-form-label text-lg-end">Location</label>
                               <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 class="row mb-3">
                               <label class="col-sm-3 col-form-label text-lg-end">Rack</label>
                               <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 class="row mb-3">
                               <label class="col-sm-3 col-form-label text-lg-end">Device</label>
                               <div class="col">
-                                  <input class="form-control" value="{{ termination_a.device }}" disabled />
+                                  <input class="form-control" value="{{ termination_a_parent }}" disabled />
                               </div>
                           </div>
                           <div class="row mb-3">
                               <label class="col-sm-3 col-form-label text-lg-end">Type</label>
                               <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>
                       {% endif %}