|
|
@@ -5,7 +5,9 @@ from dcim.models import Site, Device, Interface
|
|
|
from extras.forms import CustomFieldForm, CustomFieldBulkEditForm, CustomFieldFilterForm
|
|
|
from tenancy.forms import bulkedit_tenant_choices
|
|
|
from tenancy.models import Tenant
|
|
|
-from utilities.forms import BootstrapMixin, APISelect, Livesearch, CSVDataField, BulkImportForm, SlugField
|
|
|
+from utilities.forms import (
|
|
|
+ APISelect, BootstrapMixin, CSVDataField, BulkImportForm, FilterChoiceField, Livesearch, SlugField,
|
|
|
+)
|
|
|
|
|
|
from .models import (
|
|
|
Aggregate, IPAddress, Prefix, PREFIX_STATUS_CHOICES, RIR, Role, VLAN, VLANGroup, VLAN_STATUS_CHOICES, VRF,
|
|
|
@@ -69,15 +71,10 @@ class VRFBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
|
|
|
description = forms.CharField(max_length=100, required=False)
|
|
|
|
|
|
|
|
|
-def vrf_tenant_choices():
|
|
|
- tenant_choices = Tenant.objects.annotate(vrf_count=Count('vrfs'))
|
|
|
- return [(t.slug, u'{} ({})'.format(t.name, t.vrf_count)) for t in tenant_choices]
|
|
|
-
|
|
|
-
|
|
|
class VRFFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|
|
model = VRF
|
|
|
- tenant = forms.MultipleChoiceField(required=False, choices=vrf_tenant_choices,
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 8}))
|
|
|
+ tenant = FilterChoiceField(queryset=Tenant.objects.annotate(filter_count=Count('vrfs')), to_field_name='slug',
|
|
|
+ null_option=(0, None))
|
|
|
|
|
|
|
|
|
#
|
|
|
@@ -128,16 +125,11 @@ class AggregateBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
|
|
|
description = forms.CharField(max_length=100, required=False)
|
|
|
|
|
|
|
|
|
-def aggregate_rir_choices():
|
|
|
- rir_choices = RIR.objects.annotate(aggregate_count=Count('aggregates'))
|
|
|
- return [(r.slug, u'{} ({})'.format(r.name, r.aggregate_count)) for r in rir_choices]
|
|
|
-
|
|
|
-
|
|
|
class AggregateFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|
|
model = Aggregate
|
|
|
family = forms.ChoiceField(required=False, choices=IP_FAMILY_CHOICES, label='Address Family')
|
|
|
- rir = forms.MultipleChoiceField(required=False, choices=aggregate_rir_choices, label='RIR',
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 8}))
|
|
|
+ rir = FilterChoiceField(queryset=RIR.objects.annotate(filter_count=Count('aggregates')), to_field_name='slug',
|
|
|
+ label='RIR')
|
|
|
|
|
|
|
|
|
#
|
|
|
@@ -268,21 +260,6 @@ class PrefixBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
|
|
|
description = forms.CharField(max_length=100, required=False)
|
|
|
|
|
|
|
|
|
-def prefix_vrf_choices():
|
|
|
- vrf_choices = VRF.objects.annotate(prefix_count=Count('prefixes'))
|
|
|
- return [(v.pk, u'{} ({})'.format(v.name, v.prefix_count)) for v in vrf_choices]
|
|
|
-
|
|
|
-
|
|
|
-def tenant_choices():
|
|
|
- tenant_choices = Tenant.objects.all()
|
|
|
- return [(t.slug, t.name) for t in tenant_choices]
|
|
|
-
|
|
|
-
|
|
|
-def prefix_site_choices():
|
|
|
- site_choices = Site.objects.annotate(prefix_count=Count('prefixes'))
|
|
|
- return [(s.slug, u'{} ({})'.format(s.name, s.prefix_count)) for s in site_choices]
|
|
|
-
|
|
|
-
|
|
|
def prefix_status_choices():
|
|
|
status_counts = {}
|
|
|
for status in Prefix.objects.values('status').annotate(count=Count('status')).order_by('status'):
|
|
|
@@ -290,27 +267,21 @@ def prefix_status_choices():
|
|
|
return [(s[0], u'{} ({})'.format(s[1], status_counts.get(s[0], 0))) for s in PREFIX_STATUS_CHOICES]
|
|
|
|
|
|
|
|
|
-def prefix_role_choices():
|
|
|
- role_choices = Role.objects.annotate(prefix_count=Count('prefixes'))
|
|
|
- return [(r.slug, u'{} ({})'.format(r.name, r.prefix_count)) for r in role_choices]
|
|
|
-
|
|
|
-
|
|
|
class PrefixFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|
|
model = Prefix
|
|
|
parent = forms.CharField(required=False, label='Search Within', widget=forms.TextInput(attrs={
|
|
|
'placeholder': 'Network',
|
|
|
}))
|
|
|
family = forms.ChoiceField(required=False, choices=IP_FAMILY_CHOICES, label='Address Family')
|
|
|
- vrf = forms.MultipleChoiceField(required=False, choices=prefix_vrf_choices, label='VRF',
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 6}))
|
|
|
- tenant = forms.MultipleChoiceField(required=False, choices=tenant_choices, label='Tenant',
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 6}))
|
|
|
- status = forms.MultipleChoiceField(required=False, choices=prefix_status_choices,
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 6}))
|
|
|
- site = forms.MultipleChoiceField(required=False, choices=prefix_site_choices,
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 6}))
|
|
|
- role = forms.MultipleChoiceField(required=False, choices=prefix_role_choices,
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 6}))
|
|
|
+ vrf = FilterChoiceField(queryset=VRF.objects.annotate(filter_count=Count('prefixes')), to_field_name='rd',
|
|
|
+ label='VRF', null_option=(0, 'Global'))
|
|
|
+ tenant = FilterChoiceField(queryset=Tenant.objects.annotate(filter_count=Count('prefixes')), to_field_name='slug',
|
|
|
+ null_option=(0, 'None'))
|
|
|
+ status = forms.MultipleChoiceField(choices=prefix_status_choices, required=False)
|
|
|
+ site = FilterChoiceField(queryset=Site.objects.annotate(filter_count=Count('prefixes')), to_field_name='slug',
|
|
|
+ null_option=(0, 'None'))
|
|
|
+ role = FilterChoiceField(queryset=Role.objects.annotate(filter_count=Count('prefixes')), to_field_name='slug',
|
|
|
+ null_option=(0, 'None'))
|
|
|
expand = forms.BooleanField(required=False, label='Expand prefix hierarchy')
|
|
|
|
|
|
|
|
|
@@ -441,21 +412,16 @@ class IPAddressBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
|
|
|
description = forms.CharField(max_length=100, required=False)
|
|
|
|
|
|
|
|
|
-def ipaddress_vrf_choices():
|
|
|
- vrf_choices = VRF.objects.annotate(ipaddress_count=Count('ip_addresses'))
|
|
|
- return [(v.pk, u'{} ({})'.format(v.name, v.ipaddress_count)) for v in vrf_choices]
|
|
|
-
|
|
|
-
|
|
|
class IPAddressFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|
|
model = IPAddress
|
|
|
parent = forms.CharField(required=False, label='Search Within', widget=forms.TextInput(attrs={
|
|
|
'placeholder': 'Prefix',
|
|
|
}))
|
|
|
family = forms.ChoiceField(required=False, choices=IP_FAMILY_CHOICES, label='Address Family')
|
|
|
- vrf = forms.MultipleChoiceField(required=False, choices=ipaddress_vrf_choices, label='VRF',
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 6}))
|
|
|
- tenant = forms.MultipleChoiceField(required=False, choices=tenant_choices, label='Tenant',
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 6}))
|
|
|
+ vrf = FilterChoiceField(queryset=VRF.objects.annotate(filter_count=Count('ip_addresses')), to_field_name='rd',
|
|
|
+ label='VRF', null_option=(0, 'Global'))
|
|
|
+ tenant = FilterChoiceField(queryset=Tenant.objects.annotate(filter_count=Count('ip_addresses')),
|
|
|
+ to_field_name='slug', null_option=(0, 'None'))
|
|
|
|
|
|
|
|
|
#
|
|
|
@@ -470,14 +436,8 @@ class VLANGroupForm(forms.ModelForm, BootstrapMixin):
|
|
|
fields = ['site', 'name', 'slug']
|
|
|
|
|
|
|
|
|
-def vlangroup_site_choices():
|
|
|
- site_choices = Site.objects.annotate(vlangroup_count=Count('vlan_groups'))
|
|
|
- return [(s.slug, u'{} ({})'.format(s.name, s.vlangroup_count)) for s in site_choices]
|
|
|
-
|
|
|
-
|
|
|
class VLANGroupFilterForm(forms.Form, BootstrapMixin):
|
|
|
- site = forms.MultipleChoiceField(required=False, choices=vlangroup_site_choices,
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 8}))
|
|
|
+ site = FilterChoiceField(queryset=Site.objects.annotate(filter_count=Count('vlan_groups')), to_field_name='slug')
|
|
|
|
|
|
|
|
|
#
|
|
|
@@ -555,21 +515,6 @@ class VLANBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
|
|
|
description = forms.CharField(max_length=100, required=False)
|
|
|
|
|
|
|
|
|
-def vlan_site_choices():
|
|
|
- site_choices = Site.objects.annotate(vlan_count=Count('vlans'))
|
|
|
- return [(s.slug, u'{} ({})'.format(s.name, s.vlan_count)) for s in site_choices]
|
|
|
-
|
|
|
-
|
|
|
-def vlan_group_choices():
|
|
|
- group_choices = VLANGroup.objects.select_related('site').annotate(vlan_count=Count('vlans'))
|
|
|
- return [(g.pk, u'{} ({})'.format(g, g.vlan_count)) for g in group_choices]
|
|
|
-
|
|
|
-
|
|
|
-def vlan_tenant_choices():
|
|
|
- tenant_choices = Tenant.objects.annotate(vrf_count=Count('vlans'))
|
|
|
- return [(t.slug, u'{} ({})'.format(t.name, t.vrf_count)) for t in tenant_choices]
|
|
|
-
|
|
|
-
|
|
|
def vlan_status_choices():
|
|
|
status_counts = {}
|
|
|
for status in VLAN.objects.values('status').annotate(count=Count('status')).order_by('status'):
|
|
|
@@ -577,19 +522,13 @@ def vlan_status_choices():
|
|
|
return [(s[0], u'{} ({})'.format(s[1], status_counts.get(s[0], 0))) for s in VLAN_STATUS_CHOICES]
|
|
|
|
|
|
|
|
|
-def vlan_role_choices():
|
|
|
- role_choices = Role.objects.annotate(vlan_count=Count('vlans'))
|
|
|
- return [(r.slug, u'{} ({})'.format(r.name, r.vlan_count)) for r in role_choices]
|
|
|
-
|
|
|
-
|
|
|
class VLANFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|
|
model = VLAN
|
|
|
- site = forms.MultipleChoiceField(required=False, choices=vlan_site_choices,
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 8}))
|
|
|
- group_id = forms.MultipleChoiceField(required=False, choices=vlan_group_choices, label='VLAN Group',
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 8}))
|
|
|
- tenant = forms.MultipleChoiceField(required=False, choices=vlan_tenant_choices,
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 8}))
|
|
|
- status = forms.MultipleChoiceField(required=False, choices=vlan_status_choices)
|
|
|
- role = forms.MultipleChoiceField(required=False, choices=vlan_role_choices,
|
|
|
- widget=forms.SelectMultiple(attrs={'size': 8}))
|
|
|
+ site = FilterChoiceField(queryset=Site.objects.annotate(filter_count=Count('vlans')), to_field_name='slug')
|
|
|
+ group_id = FilterChoiceField(queryset=VLANGroup.objects.annotate(filter_count=Count('vlans')), label='VLAN group',
|
|
|
+ null_option=(0, 'None'))
|
|
|
+ tenant = FilterChoiceField(queryset=Tenant.objects.annotate(filter_count=Count('vlans')), to_field_name='slug',
|
|
|
+ null_option=(0, 'None'))
|
|
|
+ status = forms.MultipleChoiceField(choices=vlan_status_choices, required=False)
|
|
|
+ role = FilterChoiceField(queryset=Role.objects.annotate(filter_count=Count('vlans')), to_field_name='slug',
|
|
|
+ null_option=(0, 'None'))
|