|
|
@@ -1,5 +1,4 @@
|
|
|
from django import forms
|
|
|
-from django.core.exceptions import MultipleObjectsReturned
|
|
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
|
|
from taggit.forms import TagField
|
|
|
|
|
|
@@ -11,16 +10,15 @@ from tenancy.forms import TenancyFilterForm, TenancyForm
|
|
|
from tenancy.models import Tenant
|
|
|
from utilities.forms import (
|
|
|
add_blank_choice, APISelect, APISelectMultiple, BootstrapMixin, BulkEditNullBooleanSelect, CSVChoiceField,
|
|
|
- DatePicker, DynamicModelChoiceField, DynamicModelMultipleChoiceField, ExpandableIPAddressField,
|
|
|
- FlexibleModelChoiceField, ReturnURLForm, SlugField, StaticSelect2, StaticSelect2Multiple, TagFilterField,
|
|
|
+ CSVModelChoiceField, CSVModelForm, DatePicker, DynamicModelChoiceField, DynamicModelMultipleChoiceField,
|
|
|
+ ExpandableIPAddressField, ReturnURLForm, SlugField, StaticSelect2, StaticSelect2Multiple, TagFilterField,
|
|
|
BOOLEAN_WITH_BLANK_CHOICES,
|
|
|
)
|
|
|
from virtualization.models import VirtualMachine
|
|
|
-from .constants import *
|
|
|
from .choices import *
|
|
|
+from .constants import *
|
|
|
from .models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF
|
|
|
|
|
|
-
|
|
|
PREFIX_MASK_LENGTH_CHOICES = add_blank_choice([
|
|
|
(i, i) for i in range(PREFIX_LENGTH_MIN, PREFIX_LENGTH_MAX + 1)
|
|
|
])
|
|
|
@@ -53,22 +51,16 @@ class VRFForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
|
|
|
|
|
|
|
|
class VRFCSVForm(CustomFieldModelCSVForm):
|
|
|
- tenant = forms.ModelChoiceField(
|
|
|
+ tenant = CSVModelChoiceField(
|
|
|
queryset=Tenant.objects.all(),
|
|
|
required=False,
|
|
|
to_field_name='name',
|
|
|
- help_text='Name of assigned tenant',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Tenant not found.',
|
|
|
- }
|
|
|
+ help_text='Assigned tenant'
|
|
|
)
|
|
|
|
|
|
class Meta:
|
|
|
model = VRF
|
|
|
fields = VRF.csv_headers
|
|
|
- help_texts = {
|
|
|
- 'name': 'VRF name',
|
|
|
- }
|
|
|
|
|
|
|
|
|
class VRFBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
|
|
|
@@ -120,7 +112,7 @@ class RIRForm(BootstrapMixin, forms.ModelForm):
|
|
|
]
|
|
|
|
|
|
|
|
|
-class RIRCSVForm(forms.ModelForm):
|
|
|
+class RIRCSVForm(CSVModelForm):
|
|
|
slug = SlugField()
|
|
|
|
|
|
class Meta:
|
|
|
@@ -168,13 +160,10 @@ class AggregateForm(BootstrapMixin, CustomFieldModelForm):
|
|
|
|
|
|
|
|
|
class AggregateCSVForm(CustomFieldModelCSVForm):
|
|
|
- rir = forms.ModelChoiceField(
|
|
|
+ rir = CSVModelChoiceField(
|
|
|
queryset=RIR.objects.all(),
|
|
|
to_field_name='name',
|
|
|
- help_text='Name of parent RIR',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'RIR not found.',
|
|
|
- }
|
|
|
+ help_text='Assigned RIR'
|
|
|
)
|
|
|
|
|
|
class Meta:
|
|
|
@@ -247,15 +236,12 @@ class RoleForm(BootstrapMixin, forms.ModelForm):
|
|
|
]
|
|
|
|
|
|
|
|
|
-class RoleCSVForm(forms.ModelForm):
|
|
|
+class RoleCSVForm(CSVModelForm):
|
|
|
slug = SlugField()
|
|
|
|
|
|
class Meta:
|
|
|
model = Role
|
|
|
fields = Role.csv_headers
|
|
|
- help_texts = {
|
|
|
- 'name': 'Role name',
|
|
|
- }
|
|
|
|
|
|
|
|
|
#
|
|
|
@@ -333,92 +319,62 @@ class PrefixForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
|
|
|
|
|
|
|
|
class PrefixCSVForm(CustomFieldModelCSVForm):
|
|
|
- vrf = FlexibleModelChoiceField(
|
|
|
+ vrf = CSVModelChoiceField(
|
|
|
queryset=VRF.objects.all(),
|
|
|
to_field_name='name',
|
|
|
required=False,
|
|
|
- help_text='Name of parent VRF (or {ID})',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'VRF not found.',
|
|
|
- }
|
|
|
+ help_text='Assigned VRF'
|
|
|
)
|
|
|
- tenant = forms.ModelChoiceField(
|
|
|
+ tenant = CSVModelChoiceField(
|
|
|
queryset=Tenant.objects.all(),
|
|
|
required=False,
|
|
|
to_field_name='name',
|
|
|
- help_text='Name of assigned tenant',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Tenant not found.',
|
|
|
- }
|
|
|
+ help_text='Assigned tenant'
|
|
|
)
|
|
|
- site = forms.ModelChoiceField(
|
|
|
+ site = CSVModelChoiceField(
|
|
|
queryset=Site.objects.all(),
|
|
|
required=False,
|
|
|
to_field_name='name',
|
|
|
- help_text='Name of parent site',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Site not found.',
|
|
|
- }
|
|
|
+ help_text='Assigned site'
|
|
|
)
|
|
|
- vlan_group = forms.CharField(
|
|
|
- help_text='Group name of assigned VLAN',
|
|
|
- required=False
|
|
|
+ vlan_group = CSVModelChoiceField(
|
|
|
+ queryset=VLANGroup.objects.all(),
|
|
|
+ required=False,
|
|
|
+ to_field_name='name',
|
|
|
+ help_text="VLAN's group (if any)"
|
|
|
)
|
|
|
- vlan_vid = forms.IntegerField(
|
|
|
- help_text='Numeric ID of assigned VLAN',
|
|
|
- required=False
|
|
|
+ vlan = CSVModelChoiceField(
|
|
|
+ queryset=VLAN.objects.all(),
|
|
|
+ required=False,
|
|
|
+ to_field_name='vid',
|
|
|
+ help_text="Assigned VLAN"
|
|
|
)
|
|
|
status = CSVChoiceField(
|
|
|
choices=PrefixStatusChoices,
|
|
|
help_text='Operational status'
|
|
|
)
|
|
|
- role = forms.ModelChoiceField(
|
|
|
+ role = CSVModelChoiceField(
|
|
|
queryset=Role.objects.all(),
|
|
|
required=False,
|
|
|
to_field_name='name',
|
|
|
- help_text='Functional role',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Invalid role.',
|
|
|
- }
|
|
|
+ help_text='Functional role'
|
|
|
)
|
|
|
|
|
|
class Meta:
|
|
|
model = Prefix
|
|
|
fields = Prefix.csv_headers
|
|
|
|
|
|
- def clean(self):
|
|
|
+ def __init__(self, data=None, *args, **kwargs):
|
|
|
+ super().__init__(data, *args, **kwargs)
|
|
|
|
|
|
- super().clean()
|
|
|
+ if data:
|
|
|
|
|
|
- site = self.cleaned_data.get('site')
|
|
|
- vlan_group = self.cleaned_data.get('vlan_group')
|
|
|
- vlan_vid = self.cleaned_data.get('vlan_vid')
|
|
|
-
|
|
|
- # Validate VLAN
|
|
|
- if vlan_group and vlan_vid:
|
|
|
- try:
|
|
|
- self.instance.vlan = VLAN.objects.get(site=site, group__name=vlan_group, vid=vlan_vid)
|
|
|
- except VLAN.DoesNotExist:
|
|
|
- if site:
|
|
|
- raise forms.ValidationError("VLAN {} not found in site {} group {}".format(
|
|
|
- vlan_vid, site, vlan_group
|
|
|
- ))
|
|
|
- else:
|
|
|
- raise forms.ValidationError("Global VLAN {} not found in group {}".format(vlan_vid, vlan_group))
|
|
|
- except MultipleObjectsReturned:
|
|
|
- raise forms.ValidationError(
|
|
|
- "Multiple VLANs with VID {} found in group {}".format(vlan_vid, vlan_group)
|
|
|
- )
|
|
|
- elif vlan_vid:
|
|
|
- try:
|
|
|
- self.instance.vlan = VLAN.objects.get(site=site, group__isnull=True, vid=vlan_vid)
|
|
|
- except VLAN.DoesNotExist:
|
|
|
- if site:
|
|
|
- raise forms.ValidationError("VLAN {} not found in site {}".format(vlan_vid, site))
|
|
|
- else:
|
|
|
- raise forms.ValidationError("Global VLAN {} not found".format(vlan_vid))
|
|
|
- except MultipleObjectsReturned:
|
|
|
- raise forms.ValidationError("Multiple VLANs with VID {} found".format(vlan_vid))
|
|
|
+ # Limit vlan queryset by assigned site and group
|
|
|
+ params = {
|
|
|
+ f"site__{self.fields['site'].to_field_name}": data.get('site'),
|
|
|
+ f"group__{self.fields['vlan_group'].to_field_name}": data.get('vlan_group'),
|
|
|
+ }
|
|
|
+ self.fields['vlan'].queryset = self.fields['vlan'].queryset.filter(**params)
|
|
|
|
|
|
|
|
|
class PrefixBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
|
|
|
@@ -737,23 +693,17 @@ class IPAddressBulkAddForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
|
|
|
|
|
|
|
|
class IPAddressCSVForm(CustomFieldModelCSVForm):
|
|
|
- vrf = FlexibleModelChoiceField(
|
|
|
+ vrf = CSVModelChoiceField(
|
|
|
queryset=VRF.objects.all(),
|
|
|
to_field_name='name',
|
|
|
required=False,
|
|
|
- help_text='Name of parent VRF (or {ID})',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'VRF not found.',
|
|
|
- }
|
|
|
+ help_text='Assigned VRF'
|
|
|
)
|
|
|
- tenant = forms.ModelChoiceField(
|
|
|
+ tenant = CSVModelChoiceField(
|
|
|
queryset=Tenant.objects.all(),
|
|
|
to_field_name='name',
|
|
|
required=False,
|
|
|
- help_text='Name of the assigned tenant',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Tenant not found.',
|
|
|
- }
|
|
|
+ help_text='Assigned tenant'
|
|
|
)
|
|
|
status = CSVChoiceField(
|
|
|
choices=IPAddressStatusChoices,
|
|
|
@@ -764,27 +714,23 @@ class IPAddressCSVForm(CustomFieldModelCSVForm):
|
|
|
required=False,
|
|
|
help_text='Functional role'
|
|
|
)
|
|
|
- device = FlexibleModelChoiceField(
|
|
|
+ device = CSVModelChoiceField(
|
|
|
queryset=Device.objects.all(),
|
|
|
required=False,
|
|
|
to_field_name='name',
|
|
|
- help_text='Name or ID of assigned device',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Device not found.',
|
|
|
- }
|
|
|
+ help_text='Parent device of assigned interface (if any)'
|
|
|
)
|
|
|
- virtual_machine = forms.ModelChoiceField(
|
|
|
+ virtual_machine = CSVModelChoiceField(
|
|
|
queryset=VirtualMachine.objects.all(),
|
|
|
required=False,
|
|
|
to_field_name='name',
|
|
|
- help_text='Name of assigned virtual machine',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Virtual machine not found.',
|
|
|
- }
|
|
|
+ help_text='Parent VM of assigned interface (if any)'
|
|
|
)
|
|
|
- interface_name = forms.CharField(
|
|
|
- help_text='Name of assigned interface',
|
|
|
- required=False
|
|
|
+ interface = CSVModelChoiceField(
|
|
|
+ queryset=Interface.objects.all(),
|
|
|
+ required=False,
|
|
|
+ to_field_name='name',
|
|
|
+ help_text='Assigned interface'
|
|
|
)
|
|
|
is_primary = forms.BooleanField(
|
|
|
help_text='Make this the primary IP for the assigned device',
|
|
|
@@ -795,38 +741,34 @@ class IPAddressCSVForm(CustomFieldModelCSVForm):
|
|
|
model = IPAddress
|
|
|
fields = IPAddress.csv_headers
|
|
|
|
|
|
+ def __init__(self, data=None, *args, **kwargs):
|
|
|
+ super().__init__(data, *args, **kwargs)
|
|
|
+
|
|
|
+ if data:
|
|
|
+
|
|
|
+ # Limit interface queryset by assigned device or virtual machine
|
|
|
+ if data.get('device'):
|
|
|
+ params = {
|
|
|
+ f"device__{self.fields['device'].to_field_name}": data.get('device')
|
|
|
+ }
|
|
|
+ elif data.get('virtual_machine'):
|
|
|
+ params = {
|
|
|
+ f"virtual_machine__{self.fields['virtual_machine'].to_field_name}": data.get('virtual_machine')
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ params = {
|
|
|
+ 'device': None,
|
|
|
+ 'virtual_machine': None,
|
|
|
+ }
|
|
|
+ self.fields['interface'].queryset = self.fields['interface'].queryset.filter(**params)
|
|
|
+
|
|
|
def clean(self):
|
|
|
super().clean()
|
|
|
|
|
|
device = self.cleaned_data.get('device')
|
|
|
virtual_machine = self.cleaned_data.get('virtual_machine')
|
|
|
- interface_name = self.cleaned_data.get('interface_name')
|
|
|
is_primary = self.cleaned_data.get('is_primary')
|
|
|
|
|
|
- # Validate interface
|
|
|
- if interface_name and device:
|
|
|
- try:
|
|
|
- self.instance.interface = Interface.objects.get(device=device, name=interface_name)
|
|
|
- except Interface.DoesNotExist:
|
|
|
- raise forms.ValidationError("Invalid interface {} for device {}".format(
|
|
|
- interface_name, device
|
|
|
- ))
|
|
|
- elif interface_name and virtual_machine:
|
|
|
- try:
|
|
|
- self.instance.interface = Interface.objects.get(virtual_machine=virtual_machine, name=interface_name)
|
|
|
- except Interface.DoesNotExist:
|
|
|
- raise forms.ValidationError("Invalid interface {} for virtual machine {}".format(
|
|
|
- interface_name, virtual_machine
|
|
|
- ))
|
|
|
- elif interface_name:
|
|
|
- raise forms.ValidationError("Interface given ({}) but parent device/virtual machine not specified".format(
|
|
|
- interface_name
|
|
|
- ))
|
|
|
- elif device:
|
|
|
- raise forms.ValidationError("Device specified ({}) but interface missing".format(device))
|
|
|
- elif virtual_machine:
|
|
|
- raise forms.ValidationError("Virtual machine specified ({}) but interface missing".format(virtual_machine))
|
|
|
-
|
|
|
# Validate is_primary
|
|
|
if is_primary and not device and not virtual_machine:
|
|
|
raise forms.ValidationError("No device or virtual machine specified; cannot set as primary IP")
|
|
|
@@ -993,24 +935,18 @@ class VLANGroupForm(BootstrapMixin, forms.ModelForm):
|
|
|
]
|
|
|
|
|
|
|
|
|
-class VLANGroupCSVForm(forms.ModelForm):
|
|
|
- site = forms.ModelChoiceField(
|
|
|
+class VLANGroupCSVForm(CSVModelForm):
|
|
|
+ site = CSVModelChoiceField(
|
|
|
queryset=Site.objects.all(),
|
|
|
required=False,
|
|
|
to_field_name='name',
|
|
|
- help_text='Name of parent site',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Site not found.',
|
|
|
- }
|
|
|
+ help_text='Assigned site'
|
|
|
)
|
|
|
slug = SlugField()
|
|
|
|
|
|
class Meta:
|
|
|
model = VLANGroup
|
|
|
fields = VLANGroup.csv_headers
|
|
|
- help_texts = {
|
|
|
- 'name': 'Name of VLAN group',
|
|
|
- }
|
|
|
|
|
|
|
|
|
class VLANGroupFilterForm(BootstrapMixin, forms.Form):
|
|
|
@@ -1082,40 +1018,33 @@ class VLANForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
|
|
|
|
|
|
|
|
class VLANCSVForm(CustomFieldModelCSVForm):
|
|
|
- site = forms.ModelChoiceField(
|
|
|
+ site = CSVModelChoiceField(
|
|
|
queryset=Site.objects.all(),
|
|
|
required=False,
|
|
|
to_field_name='name',
|
|
|
- help_text='Name of parent site',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Site not found.',
|
|
|
- }
|
|
|
+ help_text='Assigned site'
|
|
|
)
|
|
|
- group_name = forms.CharField(
|
|
|
- help_text='Name of VLAN group',
|
|
|
- required=False
|
|
|
+ group = CSVModelChoiceField(
|
|
|
+ queryset=VLANGroup.objects.all(),
|
|
|
+ required=False,
|
|
|
+ to_field_name='name',
|
|
|
+ help_text='Assigned VLAN group'
|
|
|
)
|
|
|
- tenant = forms.ModelChoiceField(
|
|
|
+ tenant = CSVModelChoiceField(
|
|
|
queryset=Tenant.objects.all(),
|
|
|
to_field_name='name',
|
|
|
required=False,
|
|
|
- help_text='Name of assigned tenant',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Tenant not found.',
|
|
|
- }
|
|
|
+ help_text='Assigned tenant'
|
|
|
)
|
|
|
status = CSVChoiceField(
|
|
|
choices=VLANStatusChoices,
|
|
|
help_text='Operational status'
|
|
|
)
|
|
|
- role = forms.ModelChoiceField(
|
|
|
+ role = CSVModelChoiceField(
|
|
|
queryset=Role.objects.all(),
|
|
|
required=False,
|
|
|
to_field_name='name',
|
|
|
- help_text='Functional role',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Invalid role.',
|
|
|
- }
|
|
|
+ help_text='Functional role'
|
|
|
)
|
|
|
|
|
|
class Meta:
|
|
|
@@ -1126,25 +1055,14 @@ class VLANCSVForm(CustomFieldModelCSVForm):
|
|
|
'name': 'VLAN name',
|
|
|
}
|
|
|
|
|
|
- def clean(self):
|
|
|
- super().clean()
|
|
|
+ def __init__(self, data=None, *args, **kwargs):
|
|
|
+ super().__init__(data, *args, **kwargs)
|
|
|
|
|
|
- site = self.cleaned_data.get('site')
|
|
|
- group_name = self.cleaned_data.get('group_name')
|
|
|
+ if data:
|
|
|
|
|
|
- # Validate VLAN group
|
|
|
- if group_name:
|
|
|
- try:
|
|
|
- self.instance.group = VLANGroup.objects.get(site=site, name=group_name)
|
|
|
- except VLANGroup.DoesNotExist:
|
|
|
- if site:
|
|
|
- raise forms.ValidationError(
|
|
|
- "VLAN group {} not found for site {}".format(group_name, site)
|
|
|
- )
|
|
|
- else:
|
|
|
- raise forms.ValidationError(
|
|
|
- "Global VLAN group {} not found".format(group_name)
|
|
|
- )
|
|
|
+ # Limit vlan queryset by assigned group
|
|
|
+ params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
|
|
|
+ self.fields['group'].queryset = self.fields['group'].queryset.filter(**params)
|
|
|
|
|
|
|
|
|
class VLANBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
|
|
|
@@ -1299,23 +1217,17 @@ class ServiceFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|
|
|
|
|
|
|
|
class ServiceCSVForm(CustomFieldModelCSVForm):
|
|
|
- device = FlexibleModelChoiceField(
|
|
|
+ device = CSVModelChoiceField(
|
|
|
queryset=Device.objects.all(),
|
|
|
required=False,
|
|
|
to_field_name='name',
|
|
|
- help_text='Name or ID of device',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Device not found.',
|
|
|
- }
|
|
|
+ help_text='Required if not assigned to a VM'
|
|
|
)
|
|
|
- virtual_machine = FlexibleModelChoiceField(
|
|
|
+ virtual_machine = CSVModelChoiceField(
|
|
|
queryset=VirtualMachine.objects.all(),
|
|
|
required=False,
|
|
|
to_field_name='name',
|
|
|
- help_text='Name or ID of virtual machine',
|
|
|
- error_messages={
|
|
|
- 'invalid_choice': 'Virtual machine not found.',
|
|
|
- }
|
|
|
+ help_text='Required if not assigned to a device'
|
|
|
)
|
|
|
protocol = CSVChoiceField(
|
|
|
choices=ServiceProtocolChoices,
|
|
|
@@ -1325,8 +1237,6 @@ class ServiceCSVForm(CustomFieldModelCSVForm):
|
|
|
class Meta:
|
|
|
model = Service
|
|
|
fields = Service.csv_headers
|
|
|
- help_texts = {
|
|
|
- }
|
|
|
|
|
|
|
|
|
class ServiceBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
|