2021-09-27 17:00:13 -04:00
|
|
|
from django import forms
|
|
|
|
|
|
|
|
from dcim.models import *
|
2021-12-27 21:04:29 -05:00
|
|
|
from extras.forms import CustomFieldModelForm
|
2021-09-27 17:00:13 -04:00
|
|
|
from extras.models import Tag
|
|
|
|
from utilities.forms import (
|
2021-12-27 21:04:29 -05:00
|
|
|
BootstrapMixin, DynamicModelChoiceField, DynamicModelMultipleChoiceField, ExpandableNameField,
|
2021-09-27 17:00:13 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
__all__ = (
|
2021-12-27 21:04:29 -05:00
|
|
|
'ComponentCreateForm',
|
2021-09-27 17:00:13 -04:00
|
|
|
'VirtualChassisCreateForm',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-27 21:04:29 -05:00
|
|
|
class ComponentCreateForm(BootstrapMixin, forms.Form):
|
2021-09-27 17:00:13 -04:00
|
|
|
"""
|
|
|
|
Subclass this form when facilitating the creation of one or more device component or component templates based on
|
|
|
|
a name pattern.
|
|
|
|
"""
|
|
|
|
name_pattern = ExpandableNameField(
|
|
|
|
label='Name'
|
|
|
|
)
|
|
|
|
label_pattern = ExpandableNameField(
|
|
|
|
label='Label',
|
|
|
|
required=False,
|
|
|
|
help_text='Alphanumeric ranges are supported. (Must match the number of names being created.)'
|
|
|
|
)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
super().clean()
|
|
|
|
|
|
|
|
# Validate that the number of components being created from both the name_pattern and label_pattern are equal
|
|
|
|
if self.cleaned_data['label_pattern']:
|
|
|
|
name_pattern_count = len(self.cleaned_data['name_pattern'])
|
|
|
|
label_pattern_count = len(self.cleaned_data['label_pattern'])
|
|
|
|
if name_pattern_count != label_pattern_count:
|
|
|
|
raise forms.ValidationError({
|
|
|
|
'label_pattern': f'The provided name pattern will create {name_pattern_count} components, however '
|
|
|
|
f'{label_pattern_count} labels will be generated. These counts must match.'
|
|
|
|
}, code='label_pattern_mismatch')
|
|
|
|
|
|
|
|
|
2021-11-18 16:19:25 -05:00
|
|
|
class VirtualChassisCreateForm(CustomFieldModelForm):
|
2021-09-27 17:00:13 -04:00
|
|
|
region = DynamicModelChoiceField(
|
|
|
|
queryset=Region.objects.all(),
|
|
|
|
required=False,
|
|
|
|
initial_params={
|
|
|
|
'sites': '$site'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
site_group = DynamicModelChoiceField(
|
|
|
|
queryset=SiteGroup.objects.all(),
|
|
|
|
required=False,
|
|
|
|
initial_params={
|
|
|
|
'sites': '$site'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
site = DynamicModelChoiceField(
|
|
|
|
queryset=Site.objects.all(),
|
|
|
|
required=False,
|
|
|
|
query_params={
|
|
|
|
'region_id': '$region',
|
|
|
|
'group_id': '$site_group',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
rack = DynamicModelChoiceField(
|
|
|
|
queryset=Rack.objects.all(),
|
|
|
|
required=False,
|
|
|
|
null_option='None',
|
|
|
|
query_params={
|
|
|
|
'site_id': '$site'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
members = DynamicModelMultipleChoiceField(
|
|
|
|
queryset=Device.objects.all(),
|
|
|
|
required=False,
|
|
|
|
query_params={
|
|
|
|
'site_id': '$site',
|
|
|
|
'rack_id': '$rack',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
initial_position = forms.IntegerField(
|
|
|
|
initial=1,
|
|
|
|
required=False,
|
|
|
|
help_text='Position of the first member device. Increases by one for each additional member.'
|
|
|
|
)
|
|
|
|
tags = DynamicModelMultipleChoiceField(
|
|
|
|
queryset=Tag.objects.all(),
|
|
|
|
required=False
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VirtualChassis
|
|
|
|
fields = [
|
|
|
|
'name', 'domain', 'region', 'site_group', 'site', 'rack', 'members', 'initial_position', 'tags',
|
|
|
|
]
|
|
|
|
|
2021-11-11 12:10:47 -05:00
|
|
|
def clean(self):
|
|
|
|
if self.cleaned_data['members'] and self.cleaned_data['initial_position'] is None:
|
|
|
|
raise forms.ValidationError({
|
|
|
|
'initial_position': "A position must be specified for the first VC member."
|
|
|
|
})
|
|
|
|
|
2021-09-27 17:00:13 -04:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
instance = super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
# Assign VC members
|
2021-11-11 12:10:47 -05:00
|
|
|
if instance.pk and self.cleaned_data['members']:
|
|
|
|
initial_position = self.cleaned_data.get('initial_position', 1)
|
2021-09-27 17:00:13 -04:00
|
|
|
for i, member in enumerate(self.cleaned_data['members'], start=initial_position):
|
|
|
|
member.virtual_chassis = instance
|
|
|
|
member.vc_position = i
|
|
|
|
member.save()
|
|
|
|
|
|
|
|
return instance
|