Skip to content

Commit

Permalink
netbox-community#49: Allow selection of devices at other sites when c…
Browse files Browse the repository at this point in the history
…onnecting an interface
  • Loading branch information
jeremystretch committed Dec 9, 2016
1 parent fdf489c commit 900ba14
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
17 changes: 13 additions & 4 deletions netbox/dcim/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,11 @@ class Meta:

class InterfaceConnectionForm(forms.ModelForm, BootstrapMixin):
interface_a = forms.ChoiceField(choices=[], widget=SelectWithDisabled, label='Interface')
site_b = forms.ModelChoiceField(queryset=Site.objects.all(), label='Site', required=False,
widget=forms.Select(attrs={'filter-for': 'rack_b'}))
rack_b = forms.ModelChoiceField(queryset=Rack.objects.all(), label='Rack', required=False,
widget=forms.Select(attrs={'filter-for': 'device_b'}))
widget=APISelect(api_url='/api/dcim/racks/?site_id={{site_b}}',
attrs={'filter-for': 'device_b'}))
device_b = forms.ModelChoiceField(queryset=Device.objects.all(), label='Device', required=False,
widget=APISelect(api_url='/api/dcim/devices/?rack_id={{rack_b}}',
display_field='display_name',
Expand All @@ -1060,21 +1063,27 @@ class InterfaceConnectionForm(forms.ModelForm, BootstrapMixin):

class Meta:
model = InterfaceConnection
fields = ['interface_a', 'rack_b', 'device_b', 'interface_b', 'livesearch', 'connection_status']
fields = ['interface_a', 'site_b', 'rack_b', 'device_b', 'interface_b', 'livesearch', 'connection_status']

def __init__(self, device_a, *args, **kwargs):

super(InterfaceConnectionForm, self).__init__(*args, **kwargs)

self.fields['rack_b'].queryset = Rack.objects.filter(site=device_a.rack.site)

# Initialize interface A choices
device_a_interfaces = Interface.objects.filter(device=device_a).exclude(form_factor=IFACE_FF_VIRTUAL) \
.select_related('circuit', 'connected_as_a', 'connected_as_b')
self.fields['interface_a'].choices = [
(iface.id, {'label': iface.name, 'disabled': iface.is_connected}) for iface in device_a_interfaces
]

# Initialize rack_b choices if site_b is set
if self.is_bound and self.data.get('site_b'):
self.fields['rack_b'].queryset = Rack.objects.filter(site__pk=self.data['site_b'])
elif self.initial.get('site_b'):
self.fields['rack_b'].queryset = Rack.objects.filter(site=self.initial['site_b'])
else:
self.fields['rack_b'].choices = []

# Initialize device_b choices if rack_b is set
if self.is_bound and self.data.get('rack_b'):
self.fields['device_b'].queryset = Device.objects.filter(rack__pk=self.data['rack_b'])
Expand Down
4 changes: 3 additions & 1 deletion netbox/dcim/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1467,9 +1467,11 @@ def interfaceconnection_add(request, pk):

else:
form = forms.InterfaceConnectionForm(device, initial={
'interface_a': request.GET.get('interface', None),
'interface_a': request.GET.get('interface_a', None),
'site_b': request.GET.get('site_b', device.rack.site),
'rack_b': request.GET.get('rack_b', None),
'device_b': request.GET.get('device_b', None),
'interface_b': request.GET.get('interface_b', None),
})

return render(request, 'dcim/interfaceconnection_edit.html', {
Expand Down
2 changes: 1 addition & 1 deletion netbox/templates/dcim/inc/_interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<i class="glyphicon glyphicon-remove" aria-hidden="true"></i>
</a>
{% else %}
<a href="{% url 'dcim:interfaceconnection_add' pk=device.pk %}?interface={{ iface.pk }}" class="btn btn-success btn-xs" title="Connect">
<a href="{% url 'dcim:interfaceconnection_add' pk=device.pk %}?interface_a={{ iface.pk }}" class="btn btn-success btn-xs" title="Connect">
<i class="glyphicon glyphicon-plus" aria-hidden="true"></i>
</a>
{% endif %}
Expand Down
11 changes: 9 additions & 2 deletions netbox/templates/dcim/interfaceconnection_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ <h1>Connect Interfaces</h1>
<strong>A Side</strong>
</div>
<div class="panel-body">
<div class="form-group">
<label class="col-md-3 control-label required">Site</label>
<div class="col-md-9">
<p class="form-control-static">{{ device.rack.site }}</p>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label required">Rack</label>
<div class="col-md-9">
Expand Down Expand Up @@ -61,6 +67,7 @@ <h1>Connect Interfaces</h1>
{% render_field form.livesearch %}
</div>
<div class="tab-pane" id="select">
{% render_field form.site_b %}
{% render_field form.rack_b %}
{% render_field form.device_b %}
</div>
Expand All @@ -77,8 +84,8 @@ <h1>Connect Interfaces</h1>
</div>
<div class="text-center">
<div class="form-group">
<button type="submit" name="_create" class="btn btn-primary">Create</button>
<button type="submit" name="_addanother" class="btn btn-primary">Create and Connect Another</button>
<button type="submit" name="_create" class="btn btn-primary">Connect</button>
<button type="submit" name="_addanother" class="btn btn-primary">Connect and Add Another</button>
<a href="{{ cancel_url }}" class="btn btn-default">Cancel</a>
</div>
</div>
Expand Down

0 comments on commit 900ba14

Please sign in to comment.