Skip to content

Commit

Permalink
Closes #704: Allow filtering VLANs by group when editing prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Jun 9, 2017
1 parent 9e1d03b commit 05a796f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
23 changes: 22 additions & 1 deletion netbox/ipam/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,31 @@ class PrefixForm(BootstrapMixin, TenancyForm, CustomFieldForm):
required=False,
label='Site',
widget=forms.Select(
attrs={'filter-for': 'vlan_group', 'nullable': 'true'}
)
)
vlan_group = ChainedModelChoiceField(
queryset=VLANGroup.objects.all(),
chains=(
('site', 'site'),
),
required=False,
label='VLAN group',
widget=APISelect(
api_url='/api/ipam/vlan-groups/?site_id={{site}}',
attrs={'filter-for': 'vlan', 'nullable': 'true'}
)
)
vlan = ChainedModelChoiceField(
queryset=VLAN.objects.all(),
chains=(
('site', 'site'),
('group', 'vlan_group'),
),
required=False,
label='VLAN',
widget=APISelect(
api_url='/api/ipam/vlans/?site_id={{site}}', display_field='display_name'
api_url='/api/ipam/vlans/?site_id={{site}}&group_id={{vlan_group}}', display_field='display_name'
)
)

Expand All @@ -200,6 +213,14 @@ class Meta:
fields = ['prefix', 'vrf', 'site', 'vlan', 'status', 'role', 'is_pool', 'description', 'tenant_group', 'tenant']

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

# Initialize helper selectors
instance = kwargs.get('instance')
initial = kwargs.get('initial', {})
if instance and instance.vlan is not None:
initial['vlan_group'] = instance.vlan.group
kwargs['initial'] = initial

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

self.fields['vrf'].empty_label = 'Global'
Expand Down
10 changes: 8 additions & 2 deletions netbox/templates/ipam/prefix_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
{% render_field form.prefix %}
{% render_field form.status %}
{% render_field form.vrf %}
{% render_field form.site %}
{% render_field form.vlan %}
{% render_field form.role %}
{% render_field form.description %}
{% render_field form.is_pool %}
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading"><strong>Site/VLAN Assignment</strong></div>
<div class="panel-body">
{% render_field form.site %}
{% render_field form.vlan_group %}
{% render_field form.vlan %}
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading"><strong>Tenancy</strong></div>
<div class="panel-body">
Expand Down
4 changes: 2 additions & 2 deletions netbox/utilities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,8 @@ def __init__(self, *args, **kwargs):

filters_dict = {}
for (db_field, parent_field) in field.chains:
if self.is_bound and self.data.get(parent_field):
filters_dict[db_field] = self.data[parent_field]
if self.is_bound and parent_field in self.data:
filters_dict[db_field] = self.data[parent_field] or None
elif self.initial.get(parent_field):
filters_dict[db_field] = self.initial[parent_field]
elif self.fields[parent_field].widget.attrs.get('nullable'):
Expand Down

0 comments on commit 05a796f

Please sign in to comment.