1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258 |
- import re
- from django import forms
- from django.db.models import Count, Q
- from extras.forms import CustomFieldForm, CustomFieldBulkEditForm, CustomFieldFilterForm
- from ipam.models import IPAddress
- from tenancy.forms import bulkedit_tenant_choices
- from tenancy.models import Tenant
- from utilities.forms import (
- APISelect, add_blank_choice, BootstrapMixin, BulkImportForm, CommentField, CSVDataField, ExpandableNameField,
- FilterChoiceField, FlexibleModelChoiceField, Livesearch, SelectWithDisabled, SmallTextarea, SlugField,
- )
- from .models import (
- DeviceBay, DeviceBayTemplate, CONNECTION_STATUS_CHOICES, CONNECTION_STATUS_PLANNED, CONNECTION_STATUS_CONNECTED,
- ConsolePort, ConsolePortTemplate, ConsoleServerPort, ConsoleServerPortTemplate, Device, DeviceRole, DeviceType,
- Interface, IFACE_FF_VIRTUAL, InterfaceConnection, InterfaceTemplate, Manufacturer, Module, Platform, PowerOutlet,
- PowerOutletTemplate, PowerPort, PowerPortTemplate, RACK_TYPE_CHOICES, RACK_WIDTH_CHOICES, Rack, RackGroup, RackRole,
- Site, STATUS_CHOICES, SUBDEVICE_ROLE_CHILD
- )
- FORM_STATUS_CHOICES = [
- ['', '---------'],
- ]
- FORM_STATUS_CHOICES += STATUS_CHOICES
- DEVICE_BY_PK_RE = '{\d+\}'
- def get_device_by_name_or_pk(name):
- """
- Attempt to retrieve a device by either its name or primary key ('{pk}').
- """
- if re.match(DEVICE_BY_PK_RE, name):
- pk = name.strip('{}')
- device = Device.objects.get(pk=pk)
- else:
- device = Device.objects.get(name=name)
- return device
- def bulkedit_platform_choices():
- choices = [
- (None, '---------'),
- (0, 'None'),
- ]
- choices += [(p.pk, p.name) for p in Platform.objects.all()]
- return choices
- def bulkedit_rackgroup_choices():
- """
- Include an option to remove the currently assigned group from a rack.
- """
- choices = [
- (None, '---------'),
- (0, 'None'),
- ]
- choices += [(r.pk, r) for r in RackGroup.objects.all()]
- return choices
- def bulkedit_rackrole_choices():
- """
- Include an option to remove the currently assigned role from a rack.
- """
- choices = [
- (None, '---------'),
- (0, 'None'),
- ]
- choices += [(r.pk, r.name) for r in RackRole.objects.all()]
- return choices
- #
- # Sites
- #
- class SiteForm(BootstrapMixin, CustomFieldForm):
- slug = SlugField()
- comments = CommentField()
- class Meta:
- model = Site
- fields = ['name', 'slug', 'tenant', 'facility', 'asn', 'physical_address', 'shipping_address', 'comments']
- widgets = {
- 'physical_address': SmallTextarea(attrs={'rows': 3}),
- 'shipping_address': SmallTextarea(attrs={'rows': 3}),
- }
- help_texts = {
- 'name': "Full name of the site",
- 'facility': "Data center provider and facility (e.g. Equinix NY7)",
- 'asn': "BGP autonomous system number",
- 'physical_address': "Physical location of the building (e.g. for GPS)",
- 'shipping_address': "If different from the physical address"
- }
- class SiteFromCSVForm(forms.ModelForm):
- tenant = forms.ModelChoiceField(Tenant.objects.all(), to_field_name='name', required=False,
- error_messages={'invalid_choice': 'Tenant not found.'})
- class Meta:
- model = Site
- fields = ['name', 'slug', 'tenant', 'facility', 'asn']
- class SiteImportForm(BulkImportForm, BootstrapMixin):
- csv = CSVDataField(csv_form=SiteFromCSVForm)
- class SiteBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(queryset=Site.objects.all(), widget=forms.MultipleHiddenInput)
- tenant = forms.TypedChoiceField(choices=bulkedit_tenant_choices, coerce=int, required=False, label='Tenant')
- class SiteFilterForm(BootstrapMixin, CustomFieldFilterForm):
- model = Site
- tenant = FilterChoiceField(queryset=Tenant.objects.annotate(filter_count=Count('sites')), to_field_name='slug',
- null_option=(0, 'None'))
- #
- # Rack groups
- #
- class RackGroupForm(forms.ModelForm, BootstrapMixin):
- slug = SlugField()
- class Meta:
- model = RackGroup
- fields = ['site', 'name', 'slug']
- class RackGroupFilterForm(forms.Form, BootstrapMixin):
- site = FilterChoiceField(queryset=Site.objects.annotate(filter_count=Count('rack_groups')), to_field_name='slug')
- #
- # Rack roles
- #
- class RackRoleForm(forms.ModelForm, BootstrapMixin):
- slug = SlugField()
- class Meta:
- model = RackRole
- fields = ['name', 'slug', 'color']
- #
- # Racks
- #
- class RackForm(BootstrapMixin, CustomFieldForm):
- group = forms.ModelChoiceField(queryset=RackGroup.objects.all(), required=False, label='Group', widget=APISelect(
- api_url='/api/dcim/rack-groups/?site_id={{site}}',
- ))
- comments = CommentField()
- class Meta:
- model = Rack
- fields = ['site', 'group', 'name', 'facility_id', 'tenant', 'role', 'type', 'width', 'u_height', 'comments']
- help_texts = {
- 'site': "The site at which the rack exists",
- 'name': "Organizational rack name",
- 'facility_id': "The unique rack ID assigned by the facility",
- 'u_height': "Height in rack units",
- }
- widgets = {
- 'site': forms.Select(attrs={'filter-for': 'group'}),
- }
- def __init__(self, *args, **kwargs):
- super(RackForm, self).__init__(*args, **kwargs)
- # Limit rack group choices
- if self.is_bound and self.data.get('site'):
- self.fields['group'].queryset = RackGroup.objects.filter(site__pk=self.data['site'])
- elif self.initial.get('site'):
- self.fields['group'].queryset = RackGroup.objects.filter(site=self.initial['site'])
- else:
- self.fields['group'].choices = []
- class RackFromCSVForm(forms.ModelForm):
- site = forms.ModelChoiceField(queryset=Site.objects.all(), to_field_name='name',
- error_messages={'invalid_choice': 'Site not found.'})
- group_name = forms.CharField(required=False)
- tenant = forms.ModelChoiceField(Tenant.objects.all(), to_field_name='name', required=False,
- error_messages={'invalid_choice': 'Tenant not found.'})
- role = forms.ModelChoiceField(RackRole.objects.all(), to_field_name='name', required=False,
- error_messages={'invalid_choice': 'Role not found.'})
- type = forms.CharField(required=False)
- class Meta:
- model = Rack
- fields = ['site', 'group_name', 'name', 'facility_id', 'tenant', 'role', 'type', 'width', 'u_height']
- def clean(self):
- site = self.cleaned_data.get('site')
- group = self.cleaned_data.get('group_name')
- # Validate rack group
- if site and group:
- try:
- self.instance.group = RackGroup.objects.get(site=site, name=group)
- except RackGroup.DoesNotExist:
- self.add_error('group_name', "Invalid rack group ({})".format(group))
- def clean_type(self):
- rack_type = self.cleaned_data['type']
- if not rack_type:
- return None
- try:
- choices = {v.lower(): k for k, v in RACK_TYPE_CHOICES}
- return choices[rack_type.lower()]
- except KeyError:
- raise forms.ValidationError('Invalid rack type ({}). Valid choices are: {}.'.format(
- rack_type,
- ', '.join({v: k for k, v in RACK_TYPE_CHOICES}),
- ))
- class RackImportForm(BulkImportForm, BootstrapMixin):
- csv = CSVDataField(csv_form=RackFromCSVForm)
- class RackBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(queryset=Rack.objects.all(), widget=forms.MultipleHiddenInput)
- site = forms.ModelChoiceField(queryset=Site.objects.all(), required=False, label='Site')
- group = forms.TypedChoiceField(choices=bulkedit_rackgroup_choices, coerce=int, required=False, label='Group')
- tenant = forms.TypedChoiceField(choices=bulkedit_tenant_choices, coerce=int, required=False, label='Tenant')
- role = forms.TypedChoiceField(choices=bulkedit_rackrole_choices, coerce=int, required=False, label='Role')
- type = forms.ChoiceField(choices=add_blank_choice(RACK_TYPE_CHOICES), required=False, label='Type')
- width = forms.ChoiceField(choices=add_blank_choice(RACK_WIDTH_CHOICES), required=False, label='Width')
- u_height = forms.IntegerField(required=False, label='Height (U)')
- comments = CommentField()
- class RackFilterForm(BootstrapMixin, CustomFieldFilterForm):
- model = Rack
- site = FilterChoiceField(queryset=Site.objects.annotate(filter_count=Count('racks')), to_field_name='slug')
- group_id = FilterChoiceField(queryset=RackGroup.objects.select_related('site')
- .annotate(filter_count=Count('racks')), label='Rack group', null_option=(0, 'None'))
- tenant = FilterChoiceField(queryset=Tenant.objects.annotate(filter_count=Count('racks')), to_field_name='slug',
- null_option=(0, 'None'))
- role = FilterChoiceField(queryset=RackRole.objects.annotate(filter_count=Count('racks')), to_field_name='slug',
- null_option=(0, 'None'))
- #
- # Manufacturers
- #
- class ManufacturerForm(forms.ModelForm, BootstrapMixin):
- slug = SlugField()
- class Meta:
- model = Manufacturer
- fields = ['name', 'slug']
- #
- # Device types
- #
- class DeviceTypeForm(forms.ModelForm, BootstrapMixin):
- slug = SlugField(slug_source='model')
- class Meta:
- model = DeviceType
- fields = ['manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', 'is_console_server',
- 'is_pdu', 'is_network_device', 'subdevice_role']
- class DeviceTypeBulkEditForm(forms.Form, BootstrapMixin):
- pk = forms.ModelMultipleChoiceField(queryset=DeviceType.objects.all(), widget=forms.MultipleHiddenInput)
- manufacturer = forms.ModelChoiceField(queryset=Manufacturer.objects.all(), required=False)
- u_height = forms.IntegerField(min_value=1, required=False)
- class DeviceTypeFilterForm(forms.Form, BootstrapMixin):
- manufacturer = FilterChoiceField(queryset=Manufacturer.objects.annotate(filter_count=Count('device_types')),
- to_field_name='slug')
- #
- # Device component templates
- #
- class ConsolePortTemplateForm(forms.ModelForm, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class Meta:
- model = ConsolePortTemplate
- fields = ['name_pattern']
- class ConsoleServerPortTemplateForm(forms.ModelForm, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class Meta:
- model = ConsoleServerPortTemplate
- fields = ['name_pattern']
- class PowerPortTemplateForm(forms.ModelForm, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class Meta:
- model = PowerPortTemplate
- fields = ['name_pattern']
- class PowerOutletTemplateForm(forms.ModelForm, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class Meta:
- model = PowerOutletTemplate
- fields = ['name_pattern']
- class InterfaceTemplateForm(forms.ModelForm, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class Meta:
- model = InterfaceTemplate
- fields = ['name_pattern', 'form_factor', 'mgmt_only']
- class DeviceBayTemplateForm(forms.ModelForm, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class Meta:
- model = DeviceBayTemplate
- fields = ['name_pattern']
- #
- # Device roles
- #
- class DeviceRoleForm(forms.ModelForm, BootstrapMixin):
- slug = SlugField()
- class Meta:
- model = DeviceRole
- fields = ['name', 'slug', 'color']
- #
- # Platforms
- #
- class PlatformForm(forms.ModelForm, BootstrapMixin):
- slug = SlugField()
- class Meta:
- model = Platform
- fields = ['name', 'slug']
- #
- # Devices
- #
- class DeviceForm(BootstrapMixin, CustomFieldForm):
- site = forms.ModelChoiceField(queryset=Site.objects.all(), widget=forms.Select(attrs={'filter-for': 'rack'}))
- rack = forms.ModelChoiceField(queryset=Rack.objects.all(), widget=APISelect(
- api_url='/api/dcim/racks/?site_id={{site}}',
- display_field='display_name',
- attrs={'filter-for': 'position'}
- ))
- position = forms.TypedChoiceField(required=False, empty_value=None,
- help_text="For multi-U devices, this is the lowest occupied rack unit.",
- widget=APISelect(api_url='/api/dcim/racks/{{rack}}/rack-units/?face={{face}}',
- disabled_indicator='device'))
- manufacturer = forms.ModelChoiceField(queryset=Manufacturer.objects.all(),
- widget=forms.Select(attrs={'filter-for': 'device_type'}))
- device_type = forms.ModelChoiceField(queryset=DeviceType.objects.all(), label='Device type', widget=APISelect(
- api_url='/api/dcim/device-types/?manufacturer_id={{manufacturer}}',
- display_field='model'
- ))
- comments = CommentField()
- class Meta:
- model = Device
- fields = ['name', 'device_role', 'tenant', 'device_type', 'serial', 'asset_tag', 'site', 'rack', 'position',
- 'face', 'status', 'platform', 'primary_ip4', 'primary_ip6', 'comments']
- help_texts = {
- 'device_role': "The function this device serves",
- 'serial': "Chassis serial number",
- }
- widgets = {
- 'face': forms.Select(attrs={'filter-for': 'position'}),
- 'manufacturer': forms.Select(attrs={'filter-for': 'device_type'}),
- }
- def __init__(self, *args, **kwargs):
- super(DeviceForm, self).__init__(*args, **kwargs)
- if self.instance.pk:
- # Initialize helper selections
- self.initial['site'] = self.instance.rack.site
- self.initial['manufacturer'] = self.instance.device_type.manufacturer
- # Compile list of choices for primary IPv4 and IPv6 addresses
- for family in [4, 6]:
- ip_choices = []
- interface_ips = IPAddress.objects.filter(family=family, interface__device=self.instance)
- ip_choices += [(ip.id, u'{} ({})'.format(ip.address, ip.interface)) for ip in interface_ips]
- nat_ips = IPAddress.objects.filter(family=family, nat_inside__interface__device=self.instance)\
- .select_related('nat_inside__interface')
- ip_choices += [(ip.id, u'{} ({} NAT)'.format(ip.address, ip.nat_inside.interface)) for ip in nat_ips]
- self.fields['primary_ip{}'.format(family)].choices = [(None, '---------')] + ip_choices
- else:
- # An object that doesn't exist yet can't have any IPs assigned to it
- self.fields['primary_ip4'].choices = []
- self.fields['primary_ip4'].widget.attrs['readonly'] = True
- self.fields['primary_ip6'].choices = []
- self.fields['primary_ip6'].widget.attrs['readonly'] = True
- # Limit rack choices
- if self.is_bound and self.data.get('site'):
- self.fields['rack'].queryset = Rack.objects.filter(site__pk=self.data['site'])
- elif self.initial.get('site'):
- self.fields['rack'].queryset = Rack.objects.filter(site=self.initial['site'])
- else:
- self.fields['rack'].choices = []
- # Rack position
- pk = self.instance.pk if self.instance.pk else None
- try:
- if self.is_bound and self.data.get('rack') and str(self.data.get('face')):
- position_choices = Rack.objects.get(pk=self.data['rack'])\
- .get_rack_units(face=self.data.get('face'), exclude=pk)
- elif self.initial.get('rack') and str(self.initial.get('face')):
- position_choices = Rack.objects.get(pk=self.initial['rack'])\
- .get_rack_units(face=self.initial.get('face'), exclude=pk)
- else:
- position_choices = []
- except Rack.DoesNotExist:
- position_choices = []
- self.fields['position'].choices = [('', '---------')] + [
- (p['id'], {
- 'label': p['name'],
- 'disabled': bool(p['device'] and p['id'] != self.initial.get('position')),
- }) for p in position_choices
- ]
- # Limit device_type choices
- if self.is_bound:
- self.fields['device_type'].queryset = DeviceType.objects.filter(manufacturer__pk=self.data['manufacturer'])\
- .select_related('manufacturer')
- elif self.initial.get('manufacturer'):
- self.fields['device_type'].queryset = DeviceType.objects.filter(manufacturer=self.initial['manufacturer'])\
- .select_related('manufacturer')
- else:
- self.fields['device_type'].choices = []
- # Disable rack assignment if this is a child device installed in a parent device
- if pk and self.instance.device_type.is_child_device and hasattr(self.instance, 'parent_bay'):
- self.fields['site'].disabled = True
- self.fields['rack'].disabled = True
- self.initial['site'] = self.instance.parent_bay.device.rack.site_id
- self.initial['rack'] = self.instance.parent_bay.device.rack_id
- class BaseDeviceFromCSVForm(forms.ModelForm):
- device_role = forms.ModelChoiceField(queryset=DeviceRole.objects.all(), to_field_name='name',
- error_messages={'invalid_choice': 'Invalid device role.'})
- tenant = forms.ModelChoiceField(Tenant.objects.all(), to_field_name='name', required=False,
- error_messages={'invalid_choice': 'Tenant not found.'})
- manufacturer = forms.ModelChoiceField(queryset=Manufacturer.objects.all(), to_field_name='name',
- error_messages={'invalid_choice': 'Invalid manufacturer.'})
- model_name = forms.CharField()
- platform = forms.ModelChoiceField(queryset=Platform.objects.all(), required=False, to_field_name='name',
- error_messages={'invalid_choice': 'Invalid platform.'})
- class Meta:
- fields = []
- model = Device
- def clean(self):
- manufacturer = self.cleaned_data.get('manufacturer')
- model_name = self.cleaned_data.get('model_name')
- # Validate device type
- if manufacturer and model_name:
- try:
- self.instance.device_type = DeviceType.objects.get(manufacturer=manufacturer, model=model_name)
- except DeviceType.DoesNotExist:
- self.add_error('model_name', "Invalid device type ({} {})".format(manufacturer, model_name))
- class DeviceFromCSVForm(BaseDeviceFromCSVForm):
- site = forms.ModelChoiceField(queryset=Site.objects.all(), to_field_name='name', error_messages={
- 'invalid_choice': 'Invalid site name.',
- })
- rack_name = forms.CharField()
- face = forms.CharField(required=False)
- class Meta(BaseDeviceFromCSVForm.Meta):
- fields = ['name', 'device_role', 'tenant', 'manufacturer', 'model_name', 'platform', 'serial', 'asset_tag',
- 'site', 'rack_name', 'position', 'face']
- def clean(self):
- super(DeviceFromCSVForm, self).clean()
- site = self.cleaned_data.get('site')
- rack_name = self.cleaned_data.get('rack_name')
- # Validate rack
- if site and rack_name:
- try:
- self.instance.rack = Rack.objects.get(site=site, name=rack_name)
- except Rack.DoesNotExist:
- self.add_error('rack_name', "Invalid rack ({})".format(rack_name))
- def clean_face(self):
- face = self.cleaned_data['face']
- if not face:
- return None
- try:
- return {
- 'front': 0,
- 'rear': 1,
- }[face.lower()]
- except KeyError:
- raise forms.ValidationError('Invalid rack face ({}); must be "front" or "rear".'.format(face))
- class ChildDeviceFromCSVForm(BaseDeviceFromCSVForm):
- parent = FlexibleModelChoiceField(queryset=Device.objects.all(), to_field_name='name', required=False,
- error_messages={'invalid_choice': 'Parent device not found.'})
- device_bay_name = forms.CharField(required=False)
- class Meta(BaseDeviceFromCSVForm.Meta):
- fields = ['name', 'device_role', 'tenant', 'manufacturer', 'model_name', 'platform', 'serial', 'asset_tag',
- 'parent', 'device_bay_name']
- def clean(self):
- super(ChildDeviceFromCSVForm, self).clean()
- parent = self.cleaned_data.get('parent')
- device_bay_name = self.cleaned_data.get('device_bay_name')
- # Validate device bay
- if parent and device_bay_name:
- try:
- device_bay = DeviceBay.objects.get(device=parent, name=device_bay_name)
- if device_bay.installed_device:
- self.add_error('device_bay_name',
- "Device bay ({} {}) is already occupied".format(parent, device_bay_name))
- else:
- self.instance.parent_bay = device_bay
- except DeviceBay.DoesNotExist:
- self.add_error('device_bay_name', "Parent device/bay ({} {}) not found".format(parent, device_bay_name))
- class DeviceImportForm(BulkImportForm, BootstrapMixin):
- csv = CSVDataField(csv_form=DeviceFromCSVForm)
- class ChildDeviceImportForm(BulkImportForm, BootstrapMixin):
- csv = CSVDataField(csv_form=ChildDeviceFromCSVForm)
- class DeviceBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(queryset=Device.objects.all(), widget=forms.MultipleHiddenInput)
- device_type = forms.ModelChoiceField(queryset=DeviceType.objects.all(), required=False, label='Type')
- device_role = forms.ModelChoiceField(queryset=DeviceRole.objects.all(), required=False, label='Role')
- tenant = forms.TypedChoiceField(choices=bulkedit_tenant_choices, coerce=int, required=False, label='Tenant')
- platform = forms.TypedChoiceField(choices=bulkedit_platform_choices, coerce=int, required=False,
- label='Platform')
- status = forms.ChoiceField(choices=FORM_STATUS_CHOICES, required=False, initial='', label='Status')
- serial = forms.CharField(max_length=50, required=False, label='Serial Number')
- class DeviceFilterForm(BootstrapMixin, CustomFieldFilterForm):
- model = Device
- site = FilterChoiceField(queryset=Site.objects.annotate(filter_count=Count('racks__devices')), to_field_name='slug')
- rack_group_id = FilterChoiceField(queryset=RackGroup.objects.annotate(filter_count=Count('racks__devices')),
- label='Rack Group')
- role = FilterChoiceField(queryset=DeviceRole.objects.annotate(filter_count=Count('devices')), to_field_name='slug')
- tenant = FilterChoiceField(queryset=Tenant.objects.annotate(filter_count=Count('devices')), to_field_name='slug',
- null_option=(0, 'None'))
- device_type_id = FilterChoiceField(queryset=DeviceType.objects.select_related('manufacturer')
- .annotate(filter_count=Count('instances')), label='Type')
- platform = FilterChoiceField(queryset=Platform.objects.annotate(filter_count=Count('devices')),
- to_field_name='slug', null_option=(0, 'None'))
- status = forms.NullBooleanField(required=False, widget=forms.Select(choices=FORM_STATUS_CHOICES))
- #
- # Console ports
- #
- class ConsolePortForm(forms.ModelForm, BootstrapMixin):
- class Meta:
- model = ConsolePort
- fields = ['device', 'name']
- widgets = {
- 'device': forms.HiddenInput(),
- }
- class ConsolePortCreateForm(forms.Form, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class ConsoleConnectionCSVForm(forms.Form):
- console_server = FlexibleModelChoiceField(queryset=Device.objects.filter(device_type__is_console_server=True),
- to_field_name='name',
- error_messages={'invalid_choice': 'Console server not found'})
- cs_port = forms.CharField()
- device = FlexibleModelChoiceField(queryset=Device.objects.all(), to_field_name='name',
- error_messages={'invalid_choice': 'Device not found'})
- console_port = forms.CharField()
- status = forms.ChoiceField(choices=[('planned', 'Planned'), ('connected', 'Connected')])
- def clean(self):
- # Validate console server port
- if self.cleaned_data.get('console_server'):
- try:
- cs_port = ConsoleServerPort.objects.get(device=self.cleaned_data['console_server'],
- name=self.cleaned_data['cs_port'])
- if ConsolePort.objects.filter(cs_port=cs_port):
- raise forms.ValidationError("Console server port is already occupied (by {} {})"
- .format(cs_port.connected_console.device, cs_port.connected_console))
- except ConsoleServerPort.DoesNotExist:
- raise forms.ValidationError("Invalid console server port ({} {})"
- .format(self.cleaned_data['console_server'], self.cleaned_data['cs_port']))
- # Validate console port
- if self.cleaned_data.get('device'):
- try:
- console_port = ConsolePort.objects.get(device=self.cleaned_data['device'],
- name=self.cleaned_data['console_port'])
- if console_port.cs_port:
- raise forms.ValidationError("Console port is already connected (to {} {})"
- .format(console_port.cs_port.device, console_port.cs_port))
- except ConsolePort.DoesNotExist:
- raise forms.ValidationError("Invalid console port ({} {})"
- .format(self.cleaned_data['device'], self.cleaned_data['console_port']))
- class ConsoleConnectionImportForm(BulkImportForm, BootstrapMixin):
- csv = CSVDataField(csv_form=ConsoleConnectionCSVForm)
- def clean(self):
- records = self.cleaned_data.get('csv')
- if not records:
- return
- connection_list = []
- for i, record in enumerate(records, start=1):
- form = self.fields['csv'].csv_form(data=record)
- if form.is_valid():
- console_port = ConsolePort.objects.get(device=form.cleaned_data['device'],
- name=form.cleaned_data['console_port'])
- console_port.cs_port = ConsoleServerPort.objects.get(device=form.cleaned_data['console_server'],
- name=form.cleaned_data['cs_port'])
- if form.cleaned_data['status'] == 'planned':
- console_port.connection_status = CONNECTION_STATUS_PLANNED
- else:
- console_port.connection_status = CONNECTION_STATUS_CONNECTED
- connection_list.append(console_port)
- else:
- for field, errors in form.errors.items():
- for e in errors:
- self.add_error('csv', "Record {} {}: {}".format(i, field, e))
- self.cleaned_data['csv'] = connection_list
- class ConsolePortConnectionForm(forms.ModelForm, BootstrapMixin):
- rack = forms.ModelChoiceField(queryset=Rack.objects.all(), label='Rack', required=False,
- widget=forms.Select(attrs={'filter-for': 'console_server'}))
- console_server = forms.ModelChoiceField(queryset=Device.objects.all(), label='Console Server', required=False,
- widget=APISelect(api_url='/api/dcim/devices/?rack_id={{rack}}&is_console_server=True',
- attrs={'filter-for': 'cs_port'}))
- livesearch = forms.CharField(required=False, label='Console Server', widget=Livesearch(
- query_key='q', query_url='dcim-api:device_list', field_to_update='console_server')
- )
- cs_port = forms.ModelChoiceField(queryset=ConsoleServerPort.objects.all(), label='Port',
- widget=APISelect(api_url='/api/dcim/devices/{{console_server}}/console-server-ports/',
- disabled_indicator='connected_console'))
- class Meta:
- model = ConsolePort
- fields = ['rack', 'console_server', 'livesearch', 'cs_port', 'connection_status']
- labels = {
- 'cs_port': 'Port',
- 'connection_status': 'Status',
- }
- def __init__(self, *args, **kwargs):
- super(ConsolePortConnectionForm, self).__init__(*args, **kwargs)
- if not self.instance.pk:
- raise RuntimeError("ConsolePortConnectionForm must be initialized with an existing ConsolePort instance.")
- self.fields['rack'].queryset = Rack.objects.filter(site=self.instance.device.rack.site)
- self.fields['cs_port'].required = True
- self.fields['connection_status'].choices = CONNECTION_STATUS_CHOICES
- # Initialize console server choices
- if self.is_bound and self.data.get('rack'):
- self.fields['console_server'].queryset = Device.objects.filter(rack=self.data['rack'], device_type__is_console_server=True)
- elif self.initial.get('rack'):
- self.fields['console_server'].queryset = Device.objects.filter(rack=self.initial['rack'], device_type__is_console_server=True)
- else:
- self.fields['console_server'].choices = []
- # Initialize CS port choices
- if self.is_bound:
- self.fields['cs_port'].queryset = ConsoleServerPort.objects.filter(device__pk=self.data['console_server'])
- elif self.initial.get('console_server', None):
- self.fields['cs_port'].queryset = ConsoleServerPort.objects.filter(device__pk=self.initial['console_server'])
- else:
- self.fields['cs_port'].choices = []
- #
- # Console server ports
- #
- class ConsoleServerPortForm(forms.ModelForm, BootstrapMixin):
- class Meta:
- model = ConsoleServerPort
- fields = ['device', 'name']
- widgets = {
- 'device': forms.HiddenInput(),
- }
- class ConsoleServerPortCreateForm(forms.Form, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class ConsoleServerPortConnectionForm(forms.Form, BootstrapMixin):
- rack = forms.ModelChoiceField(queryset=Rack.objects.all(), label='Rack', required=False,
- widget=forms.Select(attrs={'filter-for': 'device'}))
- device = forms.ModelChoiceField(queryset=Device.objects.all(), label='Device', required=False,
- widget=APISelect(api_url='/api/dcim/devices/?rack_id={{rack}}',
- attrs={'filter-for': 'port'}))
- livesearch = forms.CharField(required=False, label='Device', widget=Livesearch(
- query_key='q', query_url='dcim-api:device_list', field_to_update='device')
- )
- port = forms.ModelChoiceField(queryset=ConsolePort.objects.all(), label='Port',
- widget=APISelect(api_url='/api/dcim/devices/{{device}}/console-ports/',
- disabled_indicator='cs_port'))
- connection_status = forms.BooleanField(required=False, initial=CONNECTION_STATUS_CONNECTED, label='Status',
- widget=forms.Select(choices=CONNECTION_STATUS_CHOICES))
- class Meta:
- fields = ['rack', 'device', 'livesearch', 'port', 'connection_status']
- labels = {
- 'connection_status': 'Status',
- }
- def __init__(self, consoleserverport, *args, **kwargs):
- super(ConsoleServerPortConnectionForm, self).__init__(*args, **kwargs)
- self.fields['rack'].queryset = Rack.objects.filter(site=consoleserverport.device.rack.site)
- # Initialize device choices
- if self.is_bound and self.data.get('rack'):
- self.fields['device'].queryset = Device.objects.filter(rack=self.data['rack'])
- elif self.initial.get('rack', None):
- self.fields['device'].queryset = Device.objects.filter(rack=self.initial['rack'])
- else:
- self.fields['device'].choices = []
- # Initialize port choices
- if self.is_bound:
- self.fields['port'].queryset = ConsolePort.objects.filter(device__pk=self.data['device'])
- elif self.initial.get('device', None):
- self.fields['port'].queryset = ConsolePort.objects.filter(device_pk=self.initial['device'])
- else:
- self.fields['port'].choices = []
- #
- # Power ports
- #
- class PowerPortForm(forms.ModelForm, BootstrapMixin):
- class Meta:
- model = PowerPort
- fields = ['device', 'name']
- widgets = {
- 'device': forms.HiddenInput(),
- }
- class PowerPortCreateForm(forms.Form, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class PowerConnectionCSVForm(forms.Form):
- pdu = FlexibleModelChoiceField(queryset=Device.objects.filter(device_type__is_pdu=True), to_field_name='name',
- error_messages={'invalid_choice': 'PDU not found.'})
- power_outlet = forms.CharField()
- device = FlexibleModelChoiceField(queryset=Device.objects.all(), to_field_name='name',
- error_messages={'invalid_choice': 'Device not found'})
- power_port = forms.CharField()
- status = forms.ChoiceField(choices=[('planned', 'Planned'), ('connected', 'Connected')])
- def clean(self):
- # Validate power outlet
- if self.cleaned_data.get('pdu'):
- try:
- power_outlet = PowerOutlet.objects.get(device=self.cleaned_data['pdu'],
- name=self.cleaned_data['power_outlet'])
- if PowerPort.objects.filter(power_outlet=power_outlet):
- raise forms.ValidationError("Power outlet is already occupied (by {} {})"
- .format(power_outlet.connected_port.device,
- power_outlet.connected_port))
- except PowerOutlet.DoesNotExist:
- raise forms.ValidationError("Invalid PDU port ({} {})"
- .format(self.cleaned_data['pdu'], self.cleaned_data['power_outlet']))
- # Validate power port
- if self.cleaned_data.get('device'):
- try:
- power_port = PowerPort.objects.get(device=self.cleaned_data['device'],
- name=self.cleaned_data['power_port'])
- if power_port.power_outlet:
- raise forms.ValidationError("Power port is already connected (to {} {})"
- .format(power_port.power_outlet.device, power_port.power_outlet))
- except PowerPort.DoesNotExist:
- raise forms.ValidationError("Invalid power port ({} {})"
- .format(self.cleaned_data['device'], self.cleaned_data['power_port']))
- class PowerConnectionImportForm(BulkImportForm, BootstrapMixin):
- csv = CSVDataField(csv_form=PowerConnectionCSVForm)
- def clean(self):
- records = self.cleaned_data.get('csv')
- if not records:
- return
- connection_list = []
- for i, record in enumerate(records, start=1):
- form = self.fields['csv'].csv_form(data=record)
- if form.is_valid():
- power_port = PowerPort.objects.get(device=form.cleaned_data['device'],
- name=form.cleaned_data['power_port'])
- power_port.power_outlet = PowerOutlet.objects.get(device=form.cleaned_data['pdu'],
- name=form.cleaned_data['power_outlet'])
- if form.cleaned_data['status'] == 'planned':
- power_port.connection_status = CONNECTION_STATUS_PLANNED
- else:
- power_port.connection_status = CONNECTION_STATUS_CONNECTED
- connection_list.append(power_port)
- else:
- for field, errors in form.errors.items():
- for e in errors:
- self.add_error('csv', "Record {} {}: {}".format(i, field, e))
- self.cleaned_data['csv'] = connection_list
- class PowerPortConnectionForm(forms.ModelForm, BootstrapMixin):
- rack = forms.ModelChoiceField(queryset=Rack.objects.all(), label='Rack', required=False,
- widget=forms.Select(attrs={'filter-for': 'pdu'}))
- pdu = forms.ModelChoiceField(queryset=Device.objects.all(), label='PDU', required=False,
- widget=APISelect(api_url='/api/dcim/devices/?rack_id={{rack}}&is_pdu=True',
- attrs={'filter-for': 'power_outlet'}))
- livesearch = forms.CharField(required=False, label='PDU', widget=Livesearch(
- query_key='q', query_url='dcim-api:device_list', field_to_update='pdu')
- )
- power_outlet = forms.ModelChoiceField(queryset=PowerOutlet.objects.all(), label='Outlet',
- widget=APISelect(api_url='/api/dcim/devices/{{pdu}}/power-outlets/',
- disabled_indicator='connected_port'))
- class Meta:
- model = PowerPort
- fields = ['rack', 'pdu', 'livesearch', 'power_outlet', 'connection_status']
- labels = {
- 'power_outlet': 'Outlet',
- 'connection_status': 'Status',
- }
- def __init__(self, *args, **kwargs):
- super(PowerPortConnectionForm, self).__init__(*args, **kwargs)
- if not self.instance.pk:
- raise RuntimeError("PowerPortConnectionForm must be initialized with an existing PowerPort instance.")
- self.fields['rack'].queryset = Rack.objects.filter(site=self.instance.device.rack.site)
- self.fields['power_outlet'].required = True
- self.fields['connection_status'].choices = CONNECTION_STATUS_CHOICES
- # Initialize PDU choices
- if self.is_bound and self.data.get('rack'):
- self.fields['pdu'].queryset = Device.objects.filter(rack=self.data['rack'], device_type__is_pdu=True)
- elif self.initial.get('rack', None):
- self.fields['pdu'].queryset = Device.objects.filter(rack=self.initial['rack'], device_type__is_pdu=True)
- else:
- self.fields['pdu'].choices = []
- # Initialize power outlet choices
- if self.is_bound:
- self.fields['power_outlet'].queryset = PowerOutlet.objects.filter(device__pk=self.data['pdu'])
- elif self.initial.get('pdu', None):
- self.fields['power_outlet'].queryset = PowerOutlet.objects.filter(device__pk=self.initial['pdu'])
- else:
- self.fields['power_outlet'].choices = []
- #
- # Power outlets
- #
- class PowerOutletForm(forms.ModelForm, BootstrapMixin):
- class Meta:
- model = PowerOutlet
- fields = ['device', 'name']
- widgets = {
- 'device': forms.HiddenInput(),
- }
- class PowerOutletCreateForm(forms.Form, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class PowerOutletConnectionForm(forms.Form, BootstrapMixin):
- rack = forms.ModelChoiceField(queryset=Rack.objects.all(), label='Rack', required=False,
- widget=forms.Select(attrs={'filter-for': 'device'}))
- device = forms.ModelChoiceField(queryset=Device.objects.all(), label='Device', required=False,
- widget=APISelect(api_url='/api/dcim/devices/?rack_id={{rack}}',
- attrs={'filter-for': 'port'}))
- livesearch = forms.CharField(required=False, label='Device', widget=Livesearch(
- query_key='q', query_url='dcim-api:device_list', field_to_update='device')
- )
- port = forms.ModelChoiceField(queryset=PowerPort.objects.all(), label='Port',
- widget=APISelect(api_url='/api/dcim/devices/{{device}}/power-ports/',
- disabled_indicator='power_outlet'))
- connection_status = forms.BooleanField(required=False, initial=CONNECTION_STATUS_CONNECTED, label='Status',
- widget=forms.Select(choices=CONNECTION_STATUS_CHOICES))
- class Meta:
- fields = ['rack', 'device', 'livesearch', 'port', 'connection_status']
- labels = {
- 'connection_status': 'Status',
- }
- def __init__(self, poweroutlet, *args, **kwargs):
- super(PowerOutletConnectionForm, self).__init__(*args, **kwargs)
- self.fields['rack'].queryset = Rack.objects.filter(site=poweroutlet.device.rack.site)
- # Initialize device choices
- if self.is_bound and self.data.get('rack'):
- self.fields['device'].queryset = Device.objects.filter(rack=self.data['rack'])
- elif self.initial.get('rack', None):
- self.fields['device'].queryset = Device.objects.filter(rack=self.initial['rack'])
- else:
- self.fields['device'].choices = []
- # Initialize port choices
- if self.is_bound:
- self.fields['port'].queryset = PowerPort.objects.filter(device__pk=self.data['device'])
- elif self.initial.get('device', None):
- self.fields['port'].queryset = PowerPort.objects.filter(device_pk=self.initial['device'])
- else:
- self.fields['port'].choices = []
- #
- # Interfaces
- #
- class InterfaceForm(forms.ModelForm, BootstrapMixin):
- class Meta:
- model = Interface
- fields = ['device', 'name', 'form_factor', 'mac_address', 'mgmt_only', 'description']
- widgets = {
- 'device': forms.HiddenInput(),
- }
- class InterfaceCreateForm(forms.ModelForm, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class Meta:
- model = Interface
- fields = ['name_pattern', 'form_factor', 'mac_address', 'mgmt_only', 'description']
- class InterfaceBulkCreateForm(InterfaceCreateForm, BootstrapMixin):
- pk = forms.ModelMultipleChoiceField(queryset=Device.objects.all(), widget=forms.MultipleHiddenInput)
- #
- # Interface connections
- #
- class InterfaceConnectionForm(forms.ModelForm, BootstrapMixin):
- interface_a = forms.ChoiceField(choices=[], widget=SelectWithDisabled, label='Interface')
- rack_b = forms.ModelChoiceField(queryset=Rack.objects.all(), label='Rack', required=False,
- widget=forms.Select(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}}',
- attrs={'filter-for': 'interface_b'}))
- livesearch = forms.CharField(required=False, label='Device', widget=Livesearch(
- query_key='q', query_url='dcim-api:device_list', field_to_update='device_b')
- )
- interface_b = forms.ModelChoiceField(queryset=Interface.objects.all(), label='Interface',
- widget=APISelect(api_url='/api/dcim/devices/{{device_b}}/interfaces/?type=physical',
- disabled_indicator='is_connected'))
- class Meta:
- model = InterfaceConnection
- fields = ['interface_a', '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 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'])
- elif self.initial.get('rack_b'):
- self.fields['device_b'].queryset = Device.objects.filter(rack=self.initial['rack_b'])
- else:
- self.fields['device_b'].choices = []
- # Initialize interface_b choices if device_b is set
- if self.is_bound:
- device_b_interfaces = Interface.objects.filter(device=self.data['device_b']) \
- .exclude(form_factor=IFACE_FF_VIRTUAL).select_related('circuit', 'connected_as_a', 'connected_as_b')
- elif self.initial.get('device_b'):
- device_b_interfaces = Interface.objects.filter(device=self.initial['device_b']) \
- .exclude(form_factor=IFACE_FF_VIRTUAL).select_related('circuit', 'connected_as_a', 'connected_as_b')
- else:
- device_b_interfaces = []
- self.fields['interface_b'].choices = [
- (iface.id, {'label': iface.name, 'disabled': iface.is_connected}) for iface in device_b_interfaces
- ]
- class InterfaceConnectionCSVForm(forms.Form):
- device_a = FlexibleModelChoiceField(queryset=Device.objects.all(), to_field_name='name',
- error_messages={'invalid_choice': 'Device A not found.'})
- interface_a = forms.CharField()
- device_b = FlexibleModelChoiceField(queryset=Device.objects.all(), to_field_name='name',
- error_messages={'invalid_choice': 'Device B not found.'})
- interface_b = forms.CharField()
- status = forms.ChoiceField(choices=[('planned', 'Planned'), ('connected', 'Connected')])
- def clean(self):
- # Validate interface A
- if self.cleaned_data.get('device_a'):
- try:
- interface_a = Interface.objects.get(device=self.cleaned_data['device_a'],
- name=self.cleaned_data['interface_a'])
- except Interface.DoesNotExist:
- raise forms.ValidationError("Invalid interface ({} {})"
- .format(self.cleaned_data['device_a'], self.cleaned_data['interface_a']))
- try:
- InterfaceConnection.objects.get(Q(interface_a=interface_a) | Q(interface_b=interface_a))
- raise forms.ValidationError("{} {} is already connected"
- .format(self.cleaned_data['device_a'], self.cleaned_data['interface_a']))
- except InterfaceConnection.DoesNotExist:
- pass
- # Validate interface B
- if self.cleaned_data.get('device_b'):
- try:
- interface_b = Interface.objects.get(device=self.cleaned_data['device_b'],
- name=self.cleaned_data['interface_b'])
- except Interface.DoesNotExist:
- raise forms.ValidationError("Invalid interface ({} {})"
- .format(self.cleaned_data['device_b'], self.cleaned_data['interface_b']))
- try:
- InterfaceConnection.objects.get(Q(interface_a=interface_b) | Q(interface_b=interface_b))
- raise forms.ValidationError("{} {} is already connected"
- .format(self.cleaned_data['device_b'], self.cleaned_data['interface_b']))
- except InterfaceConnection.DoesNotExist:
- pass
- class InterfaceConnectionImportForm(BulkImportForm, BootstrapMixin):
- csv = CSVDataField(csv_form=InterfaceConnectionCSVForm)
- def clean(self):
- records = self.cleaned_data.get('csv')
- if not records:
- return
- connection_list = []
- occupied_interfaces = []
- for i, record in enumerate(records, start=1):
- form = self.fields['csv'].csv_form(data=record)
- if form.is_valid():
- interface_a = Interface.objects.get(device=form.cleaned_data['device_a'],
- name=form.cleaned_data['interface_a'])
- if interface_a in occupied_interfaces:
- raise forms.ValidationError("{} {} found in multiple connections"
- .format(interface_a.device.name, interface_a.name))
- interface_b = Interface.objects.get(device=form.cleaned_data['device_b'],
- name=form.cleaned_data['interface_b'])
- if interface_b in occupied_interfaces:
- raise forms.ValidationError("{} {} found in multiple connections"
- .format(interface_b.device.name, interface_b.name))
- connection = InterfaceConnection(interface_a=interface_a, interface_b=interface_b)
- if form.cleaned_data['status'] == 'planned':
- connection.connection_status = CONNECTION_STATUS_PLANNED
- else:
- connection.connection_status = CONNECTION_STATUS_CONNECTED
- connection_list.append(connection)
- occupied_interfaces.append(interface_a)
- occupied_interfaces.append(interface_b)
- else:
- for field, errors in form.errors.items():
- for e in errors:
- self.add_error('csv', "Record {} {}: {}".format(i, field, e))
- self.cleaned_data['csv'] = connection_list
- class InterfaceConnectionDeletionForm(forms.Form, BootstrapMixin):
- confirm = forms.BooleanField(required=True)
- # Used for HTTP redirect upon successful deletion
- device = forms.ModelChoiceField(queryset=Device.objects.all(), widget=forms.HiddenInput(), required=False)
- #
- # Device bays
- #
- class DeviceBayForm(forms.ModelForm, BootstrapMixin):
- class Meta:
- model = DeviceBay
- fields = ['device', 'name']
- widgets = {
- 'device': forms.HiddenInput(),
- }
- class DeviceBayCreateForm(forms.Form, BootstrapMixin):
- name_pattern = ExpandableNameField(label='Name')
- class PopulateDeviceBayForm(forms.Form, BootstrapMixin):
- installed_device = forms.ModelChoiceField(queryset=Device.objects.all(), label='Child Device',
- help_text="Child devices must first be created within the rack occupied "
- "by the parent device. Then they can be assigned to a bay.")
- def __init__(self, device_bay, *args, **kwargs):
- super(PopulateDeviceBayForm, self).__init__(*args, **kwargs)
- children_queryset = Device.objects.filter(rack=device_bay.device.rack,
- parent_bay__isnull=True,
- device_type__u_height=0,
- device_type__subdevice_role=SUBDEVICE_ROLE_CHILD)\
- .exclude(pk=device_bay.device.pk)
- self.fields['installed_device'].queryset = children_queryset
- #
- # Connections
- #
- class ConsoleConnectionFilterForm(forms.Form, BootstrapMixin):
- site = forms.ModelChoiceField(required=False, queryset=Site.objects.all(), to_field_name='slug')
- class PowerConnectionFilterForm(forms.Form, BootstrapMixin):
- site = forms.ModelChoiceField(required=False, queryset=Site.objects.all(), to_field_name='slug')
- class InterfaceConnectionFilterForm(forms.Form, BootstrapMixin):
- site = forms.ModelChoiceField(required=False, queryset=Site.objects.all(), to_field_name='slug')
- #
- # IP addresses
- #
- class IPAddressForm(forms.ModelForm, BootstrapMixin):
- set_as_primary = forms.BooleanField(label='Set as primary IP for device', required=False)
- class Meta:
- model = IPAddress
- fields = ['address', 'vrf', 'interface', 'set_as_primary']
- help_texts = {
- 'address': 'IPv4 or IPv6 address (with mask)'
- }
- def __init__(self, device, *args, **kwargs):
- super(IPAddressForm, self).__init__(*args, **kwargs)
- self.fields['vrf'].empty_label = 'Global'
- self.fields['interface'].queryset = device.interfaces.all()
- self.fields['interface'].required = True
- # If this device does not have any IP addresses assigned, default to setting the first IP as its primary
- if not IPAddress.objects.filter(interface__device=device).count():
- self.fields['set_as_primary'].initial = True
- #
- # Interfaces
- #
- class ModuleForm(forms.ModelForm, BootstrapMixin):
- class Meta:
- model = Module
- fields = ['name', 'manufacturer', 'part_id', 'serial']
|