| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942 |
- from django import forms
- from django.contrib.contenttypes.models import ContentType
- from django.core.validators import EMPTY_VALUES
- from django.utils.translation import gettext_lazy as _
- from timezone_field import TimeZoneFormField
- from dcim.choices import *
- from dcim.constants import *
- from dcim.models import *
- from extras.models import ConfigTemplate
- from ipam.choices import VLANQinQRoleChoices
- from ipam.models import ASN, IPAddress, VLAN, VLANGroup, VLANTranslationPolicy, VRF
- from netbox.forms import NestedGroupModelForm, NetBoxModelForm, OrganizationalModelForm, PrimaryModelForm
- from netbox.forms.mixins import ChangelogMessageMixin, OwnerMixin
- from tenancy.forms import TenancyForm
- from users.models import User
- from utilities.forms import add_blank_choice, get_field_value
- from utilities.forms.fields import (
- DynamicModelChoiceField, DynamicModelMultipleChoiceField, JSONField, NumericArrayField, SlugField,
- )
- from utilities.forms.rendering import FieldSet, InlineFields, TabbedGroups
- from utilities.forms.widgets import APISelect, ClearableFileInput, HTMXSelect, NumberWithOptions, SelectWithPK
- from utilities.jsonschema import JSONSchemaProperty
- from virtualization.models import Cluster, VMInterface
- from wireless.models import WirelessLAN, WirelessLANGroup
- from .common import InterfaceCommonForm, ModuleCommonForm
- __all__ = (
- 'CableForm',
- 'ConsolePortForm',
- 'ConsolePortTemplateForm',
- 'ConsoleServerPortForm',
- 'ConsoleServerPortTemplateForm',
- 'DeviceBayForm',
- 'DeviceBayTemplateForm',
- 'DeviceForm',
- 'DeviceRoleForm',
- 'DeviceTypeForm',
- 'DeviceVCMembershipForm',
- 'FrontPortForm',
- 'FrontPortTemplateForm',
- 'InterfaceForm',
- 'InterfaceTemplateForm',
- 'InventoryItemForm',
- 'InventoryItemRoleForm',
- 'InventoryItemTemplateForm',
- 'LocationForm',
- 'MACAddressForm',
- 'ManufacturerForm',
- 'ModuleForm',
- 'ModuleBayForm',
- 'ModuleBayTemplateForm',
- 'ModuleTypeForm',
- 'ModuleTypeProfileForm',
- 'PlatformForm',
- 'PopulateDeviceBayForm',
- 'PowerFeedForm',
- 'PowerOutletForm',
- 'PowerOutletTemplateForm',
- 'PowerPanelForm',
- 'PowerPortForm',
- 'PowerPortTemplateForm',
- 'RackForm',
- 'RackReservationForm',
- 'RackRoleForm',
- 'RackTypeForm',
- 'RearPortForm',
- 'RearPortTemplateForm',
- 'RegionForm',
- 'SiteForm',
- 'SiteGroupForm',
- 'VCMemberSelectForm',
- 'VirtualChassisForm',
- 'VirtualDeviceContextForm'
- )
- class RegionForm(NestedGroupModelForm):
- parent = DynamicModelChoiceField(
- label=_('Parent'),
- queryset=Region.objects.all(),
- required=False
- )
- fieldsets = (
- FieldSet('parent', 'name', 'slug', 'description', 'tags'),
- )
- class Meta:
- model = Region
- fields = (
- 'parent', 'name', 'slug', 'description', 'owner', 'tags', 'comments',
- )
- class SiteGroupForm(NestedGroupModelForm):
- parent = DynamicModelChoiceField(
- label=_('Parent'),
- queryset=SiteGroup.objects.all(),
- required=False
- )
- fieldsets = (
- FieldSet('parent', 'name', 'slug', 'description', 'tags'),
- )
- class Meta:
- model = SiteGroup
- fields = (
- 'parent', 'name', 'slug', 'description', 'owner', 'comments', 'tags',
- )
- class SiteForm(TenancyForm, PrimaryModelForm):
- region = DynamicModelChoiceField(
- label=_('Region'),
- queryset=Region.objects.all(),
- required=False,
- quick_add=True
- )
- group = DynamicModelChoiceField(
- label=_('Group'),
- queryset=SiteGroup.objects.all(),
- required=False,
- quick_add=True
- )
- asns = DynamicModelMultipleChoiceField(
- queryset=ASN.objects.all(),
- label=_('ASNs'),
- required=False
- )
- slug = SlugField()
- time_zone = TimeZoneFormField(
- label=_('Time zone'),
- choices=add_blank_choice(TimeZoneFormField().choices),
- required=False
- )
- fieldsets = (
- FieldSet(
- 'name', 'slug', 'status', 'region', 'group', 'facility', 'asns', 'time_zone', 'description', 'tags',
- name=_('Site')
- ),
- FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
- FieldSet('physical_address', 'shipping_address', 'latitude', 'longitude', name=_('Contact Info')),
- )
- class Meta:
- model = Site
- fields = (
- 'name', 'slug', 'status', 'region', 'group', 'tenant_group', 'tenant', 'facility', 'asns', 'time_zone',
- 'description', 'physical_address', 'shipping_address', 'latitude', 'longitude', 'owner', 'comments', 'tags',
- )
- widgets = {
- 'physical_address': forms.Textarea(
- attrs={
- 'rows': 3,
- }
- ),
- 'shipping_address': forms.Textarea(
- attrs={
- 'rows': 3,
- }
- ),
- }
- class LocationForm(TenancyForm, NestedGroupModelForm):
- site = DynamicModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- selector=True
- )
- parent = DynamicModelChoiceField(
- label=_('Parent'),
- queryset=Location.objects.all(),
- required=False,
- query_params={
- 'site_id': '$site'
- }
- )
- fieldsets = (
- FieldSet('site', 'parent', 'name', 'slug', 'status', 'facility', 'description', 'tags', name=_('Location')),
- FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
- )
- class Meta:
- model = Location
- fields = (
- 'site', 'parent', 'name', 'slug', 'status', 'description', 'tenant_group', 'tenant', 'facility', 'owner',
- 'comments', 'tags',
- )
- class RackRoleForm(OrganizationalModelForm):
- fieldsets = (
- FieldSet('name', 'slug', 'color', 'description', 'tags', name=_('Rack Role')),
- )
- class Meta:
- model = RackRole
- fields = [
- 'name', 'slug', 'color', 'description', 'owner', 'tags',
- ]
- class RackTypeForm(PrimaryModelForm):
- manufacturer = DynamicModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- quick_add=True
- )
- slug = SlugField(
- label=_('Slug'),
- slug_source='model'
- )
- fieldsets = (
- FieldSet('manufacturer', 'model', 'slug', 'description', 'form_factor', 'tags', name=_('Rack Type')),
- FieldSet(
- 'width', 'u_height',
- InlineFields('outer_width', 'outer_height', 'outer_depth', 'outer_unit', label=_('Outer Dimensions')),
- InlineFields('weight', 'max_weight', 'weight_unit', label=_('Weight')),
- 'mounting_depth', name=_('Dimensions')
- ),
- FieldSet('starting_unit', 'desc_units', name=_('Numbering')),
- )
- class Meta:
- model = RackType
- fields = [
- 'manufacturer', 'model', 'slug', 'form_factor', 'width', 'u_height', 'starting_unit', 'desc_units',
- 'outer_width', 'outer_height', 'outer_depth', 'outer_unit', 'mounting_depth', 'weight', 'max_weight',
- 'weight_unit', 'description', 'owner', 'comments', 'tags',
- ]
- class RackForm(TenancyForm, PrimaryModelForm):
- site = DynamicModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- selector=True
- )
- location = DynamicModelChoiceField(
- label=_('Location'),
- queryset=Location.objects.all(),
- required=False,
- query_params={
- 'site_id': '$site'
- }
- )
- role = DynamicModelChoiceField(
- label=_('Role'),
- queryset=RackRole.objects.all(),
- required=False
- )
- rack_type = DynamicModelChoiceField(
- label=_('Rack Type'),
- queryset=RackType.objects.all(),
- required=False,
- selector=True,
- help_text=_("Select a pre-defined rack type, or set physical characteristics below."),
- )
- fieldsets = (
- FieldSet(
- 'site', 'location', 'name', 'status', 'role', 'rack_type', 'description', 'airflow', 'tags',
- name=_('Rack')
- ),
- FieldSet('facility_id', 'serial', 'asset_tag', name=_('Inventory Control')),
- FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
- )
- class Meta:
- model = Rack
- fields = [
- 'site', 'location', 'name', 'facility_id', 'tenant_group', 'tenant', 'status', 'role', 'serial',
- 'asset_tag', 'rack_type', 'form_factor', 'width', 'u_height', 'starting_unit', 'desc_units', 'outer_width',
- 'outer_height', 'outer_depth', 'outer_unit', 'mounting_depth', 'airflow', 'weight', 'max_weight',
- 'weight_unit', 'description', 'owner', 'comments', 'tags',
- ]
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Mimic HTMXSelect()
- self.fields['rack_type'].widget.attrs.update({
- 'hx-get': '.',
- 'hx-include': '#form_fields',
- 'hx-target': '#form_fields',
- })
- # Omit RackType-defined fields if rack_type is set
- if get_field_value(self, 'rack_type'):
- for field_name in Rack.RACKTYPE_FIELDS:
- del self.fields[field_name]
- else:
- self.fieldsets = (
- *self.fieldsets,
- FieldSet(
- 'form_factor', 'width', 'starting_unit', 'u_height',
- InlineFields('outer_width', 'outer_height', 'outer_depth', 'outer_unit',
- label=_('Outer Dimensions')),
- InlineFields('weight', 'max_weight', 'weight_unit', label=_('Weight')),
- 'mounting_depth', 'desc_units', name=_('Dimensions')
- ),
- )
- class RackReservationForm(TenancyForm, PrimaryModelForm):
- rack = DynamicModelChoiceField(
- label=_('Rack'),
- queryset=Rack.objects.all(),
- selector=True
- )
- units = NumericArrayField(
- label=_('Units'),
- base_field=forms.IntegerField(),
- help_text=_("Comma-separated list of numeric unit IDs. A range may be specified using a hyphen.")
- )
- user = forms.ModelChoiceField(
- label=_('User'),
- queryset=User.objects.order_by('username')
- )
- fieldsets = (
- FieldSet('rack', 'units', 'status', 'user', 'description', 'tags', name=_('Reservation')),
- FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
- )
- class Meta:
- model = RackReservation
- fields = [
- 'rack', 'units', 'status', 'user', 'tenant_group', 'tenant', 'description', 'owner', 'comments', 'tags',
- ]
- class ManufacturerForm(OrganizationalModelForm):
- fieldsets = (
- FieldSet('name', 'slug', 'description', 'tags', name=_('Manufacturer')),
- )
- class Meta:
- model = Manufacturer
- fields = [
- 'name', 'slug', 'description', 'owner', 'tags',
- ]
- class DeviceTypeForm(PrimaryModelForm):
- manufacturer = DynamicModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- quick_add=True
- )
- default_platform = DynamicModelChoiceField(
- label=_('Default platform'),
- queryset=Platform.objects.all(),
- required=False,
- selector=True,
- query_params={
- 'manufacturer_id': ['$manufacturer', 'null'],
- }
- )
- slug = SlugField(
- label=_('Slug'),
- slug_source='model'
- )
- fieldsets = (
- FieldSet('manufacturer', 'model', 'slug', 'default_platform', 'description', 'tags', name=_('Device Type')),
- FieldSet(
- 'u_height', 'exclude_from_utilization', 'is_full_depth', 'part_number', 'subdevice_role', 'airflow',
- 'weight', 'weight_unit', name=_('Chassis')
- ),
- FieldSet('front_image', 'rear_image', name=_('Images')),
- )
- class Meta:
- model = DeviceType
- fields = [
- 'manufacturer', 'model', 'slug', 'default_platform', 'part_number', 'u_height', 'exclude_from_utilization',
- 'is_full_depth', 'subdevice_role', 'airflow', 'weight', 'weight_unit', 'front_image', 'rear_image',
- 'description', 'owner', 'comments', 'tags',
- ]
- widgets = {
- 'front_image': ClearableFileInput(attrs={
- 'accept': DEVICETYPE_IMAGE_FORMATS
- }),
- 'rear_image': ClearableFileInput(attrs={
- 'accept': DEVICETYPE_IMAGE_FORMATS
- }),
- }
- class ModuleTypeProfileForm(PrimaryModelForm):
- schema = JSONField(
- label=_('Schema'),
- required=False,
- help_text=_("Enter a valid JSON schema to define supported attributes.")
- )
- fieldsets = (
- FieldSet('name', 'description', 'schema', 'tags', name=_('Profile')),
- )
- class Meta:
- model = ModuleTypeProfile
- fields = [
- 'name', 'description', 'schema', 'owner', 'comments', 'tags',
- ]
- class ModuleTypeForm(PrimaryModelForm):
- profile = forms.ModelChoiceField(
- queryset=ModuleTypeProfile.objects.all(),
- label=_('Profile'),
- required=False,
- widget=HTMXSelect()
- )
- manufacturer = DynamicModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all()
- )
- @property
- def fieldsets(self):
- return [
- FieldSet('manufacturer', 'model', 'part_number', 'description', 'tags', name=_('Module Type')),
- FieldSet('airflow', 'weight', 'weight_unit', name=_('Hardware')),
- FieldSet('profile', *self.attr_fields, name=_('Profile & Attributes'))
- ]
- class Meta:
- model = ModuleType
- fields = [
- 'profile', 'manufacturer', 'model', 'part_number', 'description', 'airflow', 'weight', 'weight_unit',
- 'owner', 'comments', 'tags',
- ]
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Track profile-specific attribute fields
- self.attr_fields = []
- # Retrieve assigned ModuleTypeProfile, if any
- if not (profile_id := get_field_value(self, 'profile')):
- return
- if not (profile := ModuleTypeProfile.objects.filter(pk=profile_id).first()):
- return
- # Extend form with fields for profile attributes
- for attr, form_field in self._get_attr_form_fields(profile).items():
- field_name = f'attr_{attr}'
- self.attr_fields.append(field_name)
- self.fields[field_name] = form_field
- if self.instance.attribute_data:
- self.fields[field_name].initial = self.instance.attribute_data.get(attr)
- @staticmethod
- def _get_attr_form_fields(profile):
- """
- Return a dictionary mapping of attribute names to form fields, suitable for extending
- the form per the selected ModuleTypeProfile.
- """
- if not profile.schema:
- return {}
- properties = profile.schema.get('properties', {})
- required_fields = profile.schema.get('required', [])
- attr_fields = {}
- for name, options in properties.items():
- prop = JSONSchemaProperty(**options)
- attr_fields[name] = prop.to_form_field(name, required=name in required_fields)
- return dict(sorted(attr_fields.items()))
- def _post_clean(self):
- # Compile attribute data from the individual form fields
- if self.cleaned_data.get('profile'):
- self.instance.attribute_data = {
- name[5:]: self.cleaned_data[name] # Remove the attr_ prefix
- for name in self.attr_fields
- if self.cleaned_data.get(name) not in EMPTY_VALUES
- }
- return super()._post_clean()
- class DeviceRoleForm(NestedGroupModelForm):
- config_template = DynamicModelChoiceField(
- label=_('Config template'),
- queryset=ConfigTemplate.objects.all(),
- required=False
- )
- parent = DynamicModelChoiceField(
- label=_('Parent'),
- queryset=DeviceRole.objects.all(),
- required=False,
- )
- fieldsets = (
- FieldSet(
- 'name', 'slug', 'parent', 'color', 'vm_role', 'config_template', 'description',
- 'tags', name=_('Device Role')
- ),
- )
- class Meta:
- model = DeviceRole
- fields = [
- 'name', 'slug', 'parent', 'color', 'vm_role', 'config_template', 'description', 'owner', 'comments', 'tags',
- ]
- class PlatformForm(NestedGroupModelForm):
- parent = DynamicModelChoiceField(
- label=_('Parent'),
- queryset=Platform.objects.all(),
- required=False,
- )
- manufacturer = DynamicModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- required=False,
- quick_add=True
- )
- config_template = DynamicModelChoiceField(
- label=_('Config template'),
- queryset=ConfigTemplate.objects.all(),
- required=False
- )
- slug = SlugField(
- label=_('Slug'),
- max_length=64
- )
- fieldsets = (
- FieldSet(
- 'name', 'slug', 'parent', 'manufacturer', 'config_template', 'description', 'tags', name=_('Platform'),
- ),
- )
- class Meta:
- model = Platform
- fields = [
- 'name', 'slug', 'parent', 'manufacturer', 'config_template', 'description', 'owner', 'comments', 'tags',
- ]
- class DeviceForm(TenancyForm, PrimaryModelForm):
- site = DynamicModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- selector=True
- )
- location = DynamicModelChoiceField(
- label=_('Location'),
- queryset=Location.objects.all(),
- required=False,
- query_params={
- 'site_id': '$site'
- },
- initial_params={
- 'racks': '$rack'
- }
- )
- rack = DynamicModelChoiceField(
- label=_('Rack'),
- queryset=Rack.objects.all(),
- required=False,
- query_params={
- 'site_id': '$site',
- 'location_id': '$location',
- }
- )
- position = forms.DecimalField(
- label=_('Position'),
- required=False,
- help_text=_("The lowest-numbered unit occupied by the device"),
- localize=True,
- widget=APISelect(
- api_url='/api/dcim/racks/{{rack}}/elevation/',
- attrs={
- 'ts-disabled-field': 'device',
- 'data-dynamic-params': '[{"fieldName":"face","queryParam":"face"}]'
- },
- )
- )
- device_type = DynamicModelChoiceField(
- label=_('Device type'),
- queryset=DeviceType.objects.all(),
- context={
- 'parent': 'manufacturer',
- },
- selector=True
- )
- role = DynamicModelChoiceField(
- label=_('Device role'),
- queryset=DeviceRole.objects.all(),
- quick_add=True
- )
- platform = DynamicModelChoiceField(
- label=_('Platform'),
- queryset=Platform.objects.all(),
- required=False,
- selector=True,
- query_params={
- 'available_for_device_type': '$device_type',
- }
- )
- cluster = DynamicModelChoiceField(
- label=_('Cluster'),
- queryset=Cluster.objects.all(),
- required=False,
- selector=True,
- query_params={
- 'site_id': ['$site', 'null']
- },
- )
- local_context_data = JSONField(
- required=False,
- label=''
- )
- virtual_chassis = DynamicModelChoiceField(
- label=_('Virtual chassis'),
- queryset=VirtualChassis.objects.all(),
- required=False,
- context={
- 'parent': 'master',
- },
- selector=True
- )
- vc_position = forms.IntegerField(
- required=False,
- label=_('Position'),
- help_text=_("The position in the virtual chassis this device is identified by")
- )
- vc_priority = forms.IntegerField(
- required=False,
- label=_('Priority'),
- help_text=_("The priority of the device in the virtual chassis")
- )
- config_template = DynamicModelChoiceField(
- label=_('Config template'),
- queryset=ConfigTemplate.objects.all(),
- required=False
- )
- class Meta:
- model = Device
- fields = [
- 'name', 'role', 'device_type', 'serial', 'asset_tag', 'site', 'rack', 'location', 'position', 'face',
- 'latitude', 'longitude', 'status', 'airflow', 'platform', 'primary_ip4', 'primary_ip6', 'oob_ip', 'cluster',
- 'tenant_group', 'tenant', 'virtual_chassis', 'vc_position', 'vc_priority', 'description', 'config_template',
- 'owner', 'comments', 'tags', 'local_context_data',
- ]
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- if self.instance.pk:
- # Compile list of choices for primary IPv4 and IPv6 addresses
- oob_ip_choices = [(None, '---------')]
- for family in [4, 6]:
- ip_choices = [(None, '---------')]
- # Gather PKs of all interfaces belonging to this Device or a peer VirtualChassis member
- interface_ids = self.instance.vc_interfaces(if_master=False).values_list('pk', flat=True)
- # Collect interface IPs
- interface_ips = IPAddress.objects.filter(
- address__family=family,
- assigned_object_type=ContentType.objects.get_for_model(Interface),
- assigned_object_id__in=interface_ids
- ).prefetch_related('assigned_object')
- if interface_ips:
- ip_list = [(ip.id, f'{ip.address} ({ip.assigned_object})') for ip in interface_ips]
- ip_choices.append(('Interface IPs', ip_list))
- oob_ip_choices.extend(ip_list)
- # Collect NAT IPs
- nat_ips = IPAddress.objects.prefetch_related('nat_inside').filter(
- address__family=family,
- nat_inside__assigned_object_type=ContentType.objects.get_for_model(Interface),
- nat_inside__assigned_object_id__in=interface_ids
- ).prefetch_related('assigned_object')
- if nat_ips:
- ip_list = [(ip.id, f'{ip.address} (NAT)') for ip in nat_ips]
- ip_choices.append(('NAT IPs', ip_list))
- self.fields['primary_ip{}'.format(family)].choices = ip_choices
- self.fields['oob_ip'].choices = oob_ip_choices
- # If editing an existing device, exclude it from the list of occupied rack units. This ensures that a device
- # can be flipped from one face to another.
- self.fields['position'].widget.add_query_param('exclude', self.instance.pk)
- # Disable rack assignment if this is a child device installed in a parent device
- if 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.site_id
- self.initial['rack'] = self.instance.parent_bay.device.rack_id
- 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
- self.fields['oob_ip'].choices = []
- self.fields['oob_ip'].widget.attrs['readonly'] = True
- # Rack position
- position = self.data.get('position') or self.initial.get('position')
- if position:
- self.fields['position'].widget.choices = [(position, f'U{position}')]
- class ModuleForm(ModuleCommonForm, PrimaryModelForm):
- device = DynamicModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- initial_params={
- 'modulebays': '$module_bay'
- }
- )
- module_bay = DynamicModelChoiceField(
- label=_('Module bay'),
- queryset=ModuleBay.objects.all(),
- query_params={
- 'device_id': '$device'
- },
- context={
- 'disabled': 'installed_module',
- },
- )
- module_type = DynamicModelChoiceField(
- label=_('Module type'),
- queryset=ModuleType.objects.all(),
- context={
- 'parent': 'manufacturer',
- },
- selector=True
- )
- replicate_components = forms.BooleanField(
- label=_('Replicate components'),
- required=False,
- initial=True,
- help_text=_("Automatically populate components associated with this module type")
- )
- adopt_components = forms.BooleanField(
- label=_('Adopt components'),
- required=False,
- initial=False,
- help_text=_("Adopt already existing components")
- )
- fieldsets = (
- FieldSet('device', 'module_bay', 'module_type', 'status', 'description', 'tags', name=_('Module')),
- FieldSet('serial', 'asset_tag', 'replicate_components', 'adopt_components', name=_('Hardware')),
- )
- class Meta:
- model = Module
- fields = [
- 'device', 'module_bay', 'module_type', 'status', 'serial', 'asset_tag', 'tags', 'replicate_components',
- 'adopt_components', 'description', 'owner', 'comments',
- ]
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- if self.instance.pk:
- self.fields['device'].disabled = True
- self.fields['replicate_components'].initial = False
- self.fields['replicate_components'].disabled = True
- self.fields['adopt_components'].initial = False
- self.fields['adopt_components'].disabled = True
- def get_termination_type_choices():
- return add_blank_choice([
- (f'{ct.app_label}.{ct.model}', ct.model_class()._meta.verbose_name.title())
- for ct in ContentType.objects.filter(CABLE_TERMINATION_MODELS)
- ])
- class CableForm(TenancyForm, PrimaryModelForm):
- a_terminations_type = forms.ChoiceField(
- choices=get_termination_type_choices,
- required=False,
- widget=HTMXSelect(),
- label=_('Type')
- )
- b_terminations_type = forms.ChoiceField(
- choices=get_termination_type_choices,
- required=False,
- widget=HTMXSelect(),
- label=_('Type')
- )
- class Meta:
- model = Cable
- fields = [
- 'a_terminations_type', 'b_terminations_type', 'type', 'status', 'profile', 'tenant_group', 'tenant',
- 'label', 'color', 'length', 'length_unit', 'description', 'owner', 'comments', 'tags',
- ]
- class PowerPanelForm(PrimaryModelForm):
- site = DynamicModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- selector=True
- )
- location = DynamicModelChoiceField(
- label=_('Location'),
- queryset=Location.objects.all(),
- required=False,
- query_params={
- 'site_id': '$site'
- }
- )
- fieldsets = (
- FieldSet('site', 'location', 'name', 'description', 'tags', name=_('Power Panel')),
- )
- class Meta:
- model = PowerPanel
- fields = [
- 'site', 'location', 'name', 'description', 'owner', 'comments', 'tags',
- ]
- class PowerFeedForm(TenancyForm, PrimaryModelForm):
- power_panel = DynamicModelChoiceField(
- label=_('Power panel'),
- queryset=PowerPanel.objects.all(),
- selector=True,
- quick_add=True
- )
- rack = DynamicModelChoiceField(
- label=_('Rack'),
- queryset=Rack.objects.all(),
- required=False,
- selector=True
- )
- fieldsets = (
- FieldSet(
- 'power_panel', 'rack', 'name', 'status', 'type', 'description', 'mark_connected', 'tags',
- name=_('Power Feed')
- ),
- FieldSet('supply', 'voltage', 'amperage', 'phase', 'max_utilization', name=_('Characteristics')),
- FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
- )
- class Meta:
- model = PowerFeed
- fields = [
- 'power_panel', 'rack', 'name', 'status', 'type', 'mark_connected', 'supply', 'phase', 'voltage', 'amperage',
- 'max_utilization', 'tenant_group', 'tenant', 'description', 'owner', 'comments', 'tags'
- ]
- #
- # Virtual chassis
- #
- class VirtualChassisForm(PrimaryModelForm):
- master = forms.ModelChoiceField(
- label=_('Master'),
- queryset=Device.objects.all(),
- required=False,
- )
- class Meta:
- model = VirtualChassis
- fields = [
- 'name', 'domain', 'master', 'description', 'owner', 'comments', 'tags',
- ]
- widgets = {
- 'master': SelectWithPK(),
- }
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.fields['master'].queryset = Device.objects.filter(virtual_chassis=self.instance)
- class DeviceVCMembershipForm(forms.ModelForm):
- class Meta:
- model = Device
- fields = [
- 'vc_position', 'vc_priority',
- ]
- labels = {
- 'vc_position': 'Position',
- 'vc_priority': 'Priority',
- }
- def __init__(self, validate_vc_position=False, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Require VC position (only required when the Device is a VirtualChassis member)
- self.fields['vc_position'].required = True
- # Add bootstrap classes to form elements.
- self.fields['vc_position'].widget.attrs = {'class': 'form-control'}
- self.fields['vc_priority'].widget.attrs = {'class': 'form-control'}
- # Validation of vc_position is optional. This is only required when adding a new member to an existing
- # VirtualChassis. Otherwise, vc_position validation is handled by BaseVCMemberFormSet.
- self.validate_vc_position = validate_vc_position
- def clean_vc_position(self):
- vc_position = self.cleaned_data['vc_position']
- if self.validate_vc_position:
- conflicting_members = Device.objects.filter(
- virtual_chassis=self.instance.virtual_chassis,
- vc_position=vc_position
- )
- if conflicting_members.exists():
- raise forms.ValidationError(
- 'A virtual chassis member already exists in position {}.'.format(vc_position)
- )
- return vc_position
- class VCMemberSelectForm(forms.Form):
- device = DynamicModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- query_params={
- 'virtual_chassis_id': 'null',
- },
- selector=True
- )
- def clean_device(self):
- device = self.cleaned_data['device']
- if device.virtual_chassis is not None:
- raise forms.ValidationError(
- f"Device {device} is already assigned to a virtual chassis."
- )
- return device
- #
- # Device component templates
- #
- class ComponentTemplateForm(ChangelogMessageMixin, forms.ModelForm):
- device_type = DynamicModelChoiceField(
- label=_('Device type'),
- queryset=DeviceType.objects.all(),
- context={
- 'parent': 'manufacturer',
- }
- )
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Disable reassignment of DeviceType when editing an existing instance
- if self.instance.pk:
- self.fields['device_type'].disabled = True
- class ModularComponentTemplateForm(ComponentTemplateForm):
- device_type = DynamicModelChoiceField(
- label=_('Device type'),
- queryset=DeviceType.objects.all(),
- required=False,
- context={
- 'parent': 'manufacturer',
- }
- )
- module_type = DynamicModelChoiceField(
- label=_('Module type'),
- queryset=ModuleType.objects.all(),
- required=False,
- context={
- 'parent': 'manufacturer',
- }
- )
- fieldsets = (
- FieldSet(
- TabbedGroups(
- FieldSet('device_type', name=_('Device Type')),
- FieldSet('module_type', name=_('Module Type')),
- ),
- 'name', 'label', 'type', 'description'
- ),
- )
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Disable reassignment of ModuleType when editing an existing instance
- if self.instance.pk:
- self.fields['module_type'].disabled = True
- # Components attached to a module need to present this standardized substitution help text.
- self.fields['name'].help_text = _(
- "Alphanumeric ranges are supported for bulk creation. Mixed cases and types within a single range are not "
- "supported (example: <code>[ge,xe]-0/0/[0-9]</code>). The token <code>{module}</code>, if present, will be "
- "automatically replaced with the position value when creating a new module."
- )
- class ConsolePortTemplateForm(ModularComponentTemplateForm):
- class Meta:
- model = ConsolePortTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'description',
- ]
- class ConsoleServerPortTemplateForm(ModularComponentTemplateForm):
- class Meta:
- model = ConsoleServerPortTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'description',
- ]
- class PowerPortTemplateForm(ModularComponentTemplateForm):
- fieldsets = (
- FieldSet(
- TabbedGroups(
- FieldSet('device_type', name=_('Device Type')),
- FieldSet('module_type', name=_('Module Type')),
- ),
- 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description',
- ),
- )
- class Meta:
- model = PowerPortTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description',
- ]
- class PowerOutletTemplateForm(ModularComponentTemplateForm):
- power_port = DynamicModelChoiceField(
- label=_('Power port'),
- queryset=PowerPortTemplate.objects.all(),
- required=False,
- query_params={
- 'device_type_id': '$device_type',
- }
- )
- fieldsets = (
- FieldSet(
- TabbedGroups(
- FieldSet('device_type', name=_('Device Type')),
- FieldSet('module_type', name=_('Module Type')),
- ),
- 'name', 'label', 'type', 'color', 'power_port', 'feed_leg', 'description',
- ),
- )
- class Meta:
- model = PowerOutletTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'color', 'power_port', 'feed_leg', 'description',
- ]
- class InterfaceTemplateForm(ModularComponentTemplateForm):
- bridge = DynamicModelChoiceField(
- label=_('Bridge'),
- queryset=InterfaceTemplate.objects.all(),
- required=False,
- query_params={
- 'device_type_id': '$device_type',
- 'module_type_id': '$module_type',
- }
- )
- fieldsets = (
- FieldSet(
- TabbedGroups(
- FieldSet('device_type', name=_('Device Type')),
- FieldSet('module_type', name=_('Module Type')),
- ),
- 'name', 'label', 'type', 'enabled', 'mgmt_only', 'description', 'bridge',
- ),
- FieldSet('poe_mode', 'poe_type', name=_('PoE')),
- FieldSet('rf_role', name=_('Wireless')),
- )
- class Meta:
- model = InterfaceTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'mgmt_only', 'enabled', 'description', 'poe_mode',
- 'poe_type', 'bridge', 'rf_role',
- ]
- class FrontPortTemplateForm(ModularComponentTemplateForm):
- rear_port = DynamicModelChoiceField(
- label=_('Rear port'),
- queryset=RearPortTemplate.objects.all(),
- required=False,
- query_params={
- 'device_type_id': '$device_type',
- 'module_type_id': '$module_type',
- }
- )
- fieldsets = (
- FieldSet(
- TabbedGroups(
- FieldSet('device_type', name=_('Device Type')),
- FieldSet('module_type', name=_('Module Type')),
- ),
- 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position', 'description',
- ),
- )
- class Meta:
- model = FrontPortTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position',
- 'description',
- ]
- class RearPortTemplateForm(ModularComponentTemplateForm):
- fieldsets = (
- FieldSet(
- TabbedGroups(
- FieldSet('device_type', name=_('Device Type')),
- FieldSet('module_type', name=_('Module Type')),
- ),
- 'name', 'label', 'type', 'color', 'positions', 'description',
- ),
- )
- class Meta:
- model = RearPortTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'color', 'positions', 'description',
- ]
- class ModuleBayTemplateForm(ModularComponentTemplateForm):
- fieldsets = (
- FieldSet(
- TabbedGroups(
- FieldSet('device_type', name=_('Device Type')),
- FieldSet('module_type', name=_('Module Type')),
- ),
- 'name', 'label', 'position', 'description',
- ),
- )
- class Meta:
- model = ModuleBayTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'position', 'description',
- ]
- class DeviceBayTemplateForm(ComponentTemplateForm):
- fieldsets = (
- FieldSet('device_type', 'name', 'label', 'description'),
- )
- class Meta:
- model = DeviceBayTemplate
- fields = [
- 'device_type', 'name', 'label', 'description',
- ]
- class InventoryItemTemplateForm(ComponentTemplateForm):
- parent = DynamicModelChoiceField(
- label=_('Parent'),
- queryset=InventoryItemTemplate.objects.all(),
- required=False,
- query_params={
- 'device_type_id': '$device_type'
- }
- )
- role = DynamicModelChoiceField(
- label=_('Role'),
- queryset=InventoryItemRole.objects.all(),
- required=False
- )
- manufacturer = DynamicModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- required=False
- )
- # Assigned component selectors
- consoleporttemplate = DynamicModelChoiceField(
- queryset=ConsolePortTemplate.objects.all(),
- required=False,
- query_params={
- 'device_type_id': '$device_type'
- },
- label=_('Console port template')
- )
- consoleserverporttemplate = DynamicModelChoiceField(
- queryset=ConsoleServerPortTemplate.objects.all(),
- required=False,
- query_params={
- 'device_type_id': '$device_type'
- },
- label=_('Console server port template')
- )
- frontporttemplate = DynamicModelChoiceField(
- queryset=FrontPortTemplate.objects.all(),
- required=False,
- query_params={
- 'device_type_id': '$device_type'
- },
- label=_('Front port template')
- )
- interfacetemplate = DynamicModelChoiceField(
- queryset=InterfaceTemplate.objects.all(),
- required=False,
- query_params={
- 'device_type_id': '$device_type'
- },
- label=_('Interface template')
- )
- poweroutlettemplate = DynamicModelChoiceField(
- queryset=PowerOutletTemplate.objects.all(),
- required=False,
- query_params={
- 'device_type_id': '$device_type'
- },
- label=_('Power outlet template')
- )
- powerporttemplate = DynamicModelChoiceField(
- queryset=PowerPortTemplate.objects.all(),
- required=False,
- query_params={
- 'device_type_id': '$device_type'
- },
- label=_('Power port template')
- )
- rearporttemplate = DynamicModelChoiceField(
- queryset=RearPortTemplate.objects.all(),
- required=False,
- query_params={
- 'device_type_id': '$device_type'
- },
- label=_('Rear port template')
- )
- fieldsets = (
- FieldSet(
- 'device_type', 'parent', 'name', 'label', 'role', 'manufacturer', 'part_id', 'description',
- ),
- FieldSet(
- TabbedGroups(
- FieldSet('interfacetemplate', name=_('Interface')),
- FieldSet('consoleporttemplate', name=_('Console Port')),
- FieldSet('consoleserverporttemplate', name=_('Console Server Port')),
- FieldSet('frontporttemplate', name=_('Front Port')),
- FieldSet('rearporttemplate', name=_('Rear Port')),
- FieldSet('powerporttemplate', name=_('Power Port')),
- FieldSet('poweroutlettemplate', name=_('Power Outlet')),
- ),
- name=_('Component Assignment')
- )
- )
- class Meta:
- model = InventoryItemTemplate
- fields = [
- 'device_type', 'parent', 'name', 'label', 'role', 'manufacturer', 'part_id', 'description',
- ]
- def __init__(self, *args, **kwargs):
- instance = kwargs.get('instance')
- initial = kwargs.get('initial', {}).copy()
- component_type = initial.get('component_type')
- component_id = initial.get('component_id')
- if instance:
- # When editing set the initial value for component selection
- for component_model in ContentType.objects.filter(MODULAR_COMPONENT_TEMPLATE_MODELS):
- if type(instance.component) is component_model.model_class():
- initial[component_model.model] = instance.component
- break
- elif component_type and component_id:
- # When adding the InventoryItem from a component page
- content_type = ContentType.objects.filter(
- MODULAR_COMPONENT_TEMPLATE_MODELS
- ).filter(pk=component_type).first()
- if content_type:
- if component := content_type.model_class().objects.filter(pk=component_id).first():
- initial[content_type.model] = component
- kwargs['initial'] = initial
- super().__init__(*args, **kwargs)
- def clean(self):
- super().clean()
- # Handle object assignment
- selected_objects = [
- field for field in (
- 'consoleporttemplate', 'consoleserverporttemplate', 'frontporttemplate', 'interfacetemplate',
- 'poweroutlettemplate', 'powerporttemplate', 'rearporttemplate'
- ) if self.cleaned_data[field]
- ]
- if len(selected_objects) > 1:
- raise forms.ValidationError(_("An InventoryItem can only be assigned to a single component."))
- elif selected_objects:
- self.instance.component = self.cleaned_data[selected_objects[0]]
- else:
- self.instance.component = None
- #
- # Device components
- #
- class DeviceComponentForm(OwnerMixin, NetBoxModelForm):
- device = DynamicModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- selector=True
- )
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Disable reassignment of Device when editing an existing instance
- if self.instance.pk:
- self.fields['device'].disabled = True
- class ModularDeviceComponentForm(DeviceComponentForm):
- module = DynamicModelChoiceField(
- label=_('Module'),
- queryset=Module.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device',
- }
- )
- class ConsolePortForm(ModularDeviceComponentForm):
- fieldsets = (
- FieldSet(
- 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags',
- ),
- )
- class Meta:
- model = ConsolePort
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'owner', 'tags',
- ]
- class ConsoleServerPortForm(ModularDeviceComponentForm):
- fieldsets = (
- FieldSet(
- 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags',
- ),
- )
- class Meta:
- model = ConsoleServerPort
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'owner', 'tags',
- ]
- class PowerPortForm(ModularDeviceComponentForm):
- fieldsets = (
- FieldSet(
- 'device', 'module', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'mark_connected',
- 'description', 'tags',
- ),
- )
- class Meta:
- model = PowerPort
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'mark_connected',
- 'description', 'owner', 'tags',
- ]
- class PowerOutletForm(ModularDeviceComponentForm):
- power_port = DynamicModelChoiceField(
- label=_('Power port'),
- queryset=PowerPort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device',
- }
- )
- fieldsets = (
- FieldSet(
- 'device', 'module', 'name', 'label', 'type', 'status', 'color', 'power_port', 'feed_leg', 'mark_connected',
- 'description', 'owner', 'tags',
- ),
- )
- class Meta:
- model = PowerOutlet
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'status', 'color', 'power_port', 'feed_leg', 'mark_connected',
- 'description', 'tags',
- ]
- class InterfaceForm(InterfaceCommonForm, ModularDeviceComponentForm):
- vdcs = DynamicModelMultipleChoiceField(
- queryset=VirtualDeviceContext.objects.all(),
- required=False,
- label=_('Virtual device contexts'),
- initial_params={
- 'interfaces': '$parent',
- },
- query_params={
- 'device_id': '$device',
- }
- )
- parent = DynamicModelChoiceField(
- queryset=Interface.objects.all(),
- required=False,
- label=_('Parent interface'),
- query_params={
- 'virtual_chassis_member_id': '$device',
- }
- )
- bridge = DynamicModelChoiceField(
- queryset=Interface.objects.all(),
- required=False,
- label=_('Bridged interface'),
- query_params={
- 'virtual_chassis_member_id': '$device',
- }
- )
- lag = DynamicModelChoiceField(
- queryset=Interface.objects.all(),
- required=False,
- label=_('LAG interface'),
- query_params={
- 'virtual_chassis_member_id': '$device',
- 'type': 'lag',
- }
- )
- wireless_lan_group = DynamicModelChoiceField(
- queryset=WirelessLANGroup.objects.all(),
- required=False,
- label=_('Wireless LAN group')
- )
- wireless_lans = DynamicModelMultipleChoiceField(
- queryset=WirelessLAN.objects.all(),
- required=False,
- label=_('Wireless LANs'),
- query_params={
- 'group_id': '$wireless_lan_group',
- }
- )
- vlan_group = DynamicModelChoiceField(
- queryset=VLANGroup.objects.all(),
- required=False,
- label=_('VLAN group'),
- help_text=_("Filter VLANs available for assignment by group.")
- )
- untagged_vlan = DynamicModelChoiceField(
- queryset=VLAN.objects.all(),
- required=False,
- label=_('Untagged VLAN'),
- query_params={
- 'group_id': '$vlan_group',
- 'available_on_device': '$device',
- }
- )
- tagged_vlans = DynamicModelMultipleChoiceField(
- queryset=VLAN.objects.all(),
- required=False,
- label=_('Tagged VLANs'),
- query_params={
- 'group_id': '$vlan_group',
- 'available_on_device': '$device',
- }
- )
- qinq_svlan = DynamicModelChoiceField(
- queryset=VLAN.objects.all(),
- required=False,
- label=_('Q-in-Q Service VLAN'),
- query_params={
- 'group_id': '$vlan_group',
- 'available_on_device': '$device',
- 'qinq_role': VLANQinQRoleChoices.ROLE_SERVICE,
- }
- )
- vrf = DynamicModelChoiceField(
- queryset=VRF.objects.all(),
- required=False,
- label=_('VRF')
- )
- primary_mac_address = DynamicModelChoiceField(
- queryset=MACAddress.objects.all(),
- label=_('Primary MAC address'),
- required=False,
- quick_add=True,
- quick_add_params={'interface': '$pk'}
- )
- wwn = forms.CharField(
- empty_value=None,
- required=False,
- label=_('WWN')
- )
- vlan_translation_policy = DynamicModelChoiceField(
- queryset=VLANTranslationPolicy.objects.all(),
- required=False,
- label=_('VLAN Translation Policy')
- )
- fieldsets = (
- FieldSet(
- 'device', 'module', 'name', 'label', 'type', 'speed', 'duplex', 'description', 'tags', name=_('Interface')
- ),
- FieldSet('vrf', 'primary_mac_address', 'wwn', name=_('Addressing')),
- FieldSet('vdcs', 'mtu', 'tx_power', 'enabled', 'mgmt_only', 'mark_connected', name=_('Operation')),
- FieldSet('parent', 'bridge', 'lag', name=_('Related Interfaces')),
- FieldSet('poe_mode', 'poe_type', name=_('PoE')),
- FieldSet(
- 'mode', 'vlan_group', 'untagged_vlan', 'tagged_vlans', 'qinq_svlan', 'vlan_translation_policy',
- name=_('802.1Q Switching')
- ),
- FieldSet(
- 'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'wireless_lan_group', 'wireless_lans',
- name=_('Wireless')
- ),
- )
- class Meta:
- model = Interface
- fields = [
- 'device', 'module', 'vdcs', 'name', 'label', 'type', 'speed', 'duplex', 'enabled', 'parent', 'bridge',
- 'lag', 'wwn', 'mtu', 'mgmt_only', 'mark_connected', 'description', 'poe_mode', 'poe_type', 'mode',
- 'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'tx_power', 'wireless_lans',
- 'untagged_vlan', 'tagged_vlans', 'qinq_svlan', 'vlan_translation_policy', 'vrf', 'primary_mac_address',
- 'owner', 'tags',
- ]
- widgets = {
- 'speed': NumberWithOptions(
- options=InterfaceSpeedChoices
- ),
- 'mode': HTMXSelect(),
- }
- labels = {
- 'mode': '802.1Q Mode',
- }
- class FrontPortForm(ModularDeviceComponentForm):
- rear_port = DynamicModelChoiceField(
- queryset=RearPort.objects.all(),
- query_params={
- 'device_id': '$device',
- }
- )
- fieldsets = (
- FieldSet(
- 'device', 'module', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position', 'mark_connected',
- 'description', 'tags',
- ),
- )
- class Meta:
- model = FrontPort
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position', 'mark_connected',
- 'description', 'owner', 'tags',
- ]
- class RearPortForm(ModularDeviceComponentForm):
- fieldsets = (
- FieldSet(
- 'device', 'module', 'name', 'label', 'type', 'color', 'positions', 'mark_connected', 'description', 'tags',
- ),
- )
- class Meta:
- model = RearPort
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'color', 'positions', 'mark_connected', 'description', 'owner',
- 'tags',
- ]
- class ModuleBayForm(ModularDeviceComponentForm):
- fieldsets = (
- FieldSet('device', 'module', 'name', 'label', 'position', 'description', 'tags',),
- )
- class Meta:
- model = ModuleBay
- fields = [
- 'device', 'module', 'name', 'label', 'position', 'description', 'owner', 'tags',
- ]
- class DeviceBayForm(DeviceComponentForm):
- fieldsets = (
- FieldSet('device', 'name', 'label', 'description', 'tags',),
- )
- class Meta:
- model = DeviceBay
- fields = [
- 'device', 'name', 'label', 'description', 'owner', 'tags',
- ]
- class PopulateDeviceBayForm(forms.Form):
- installed_device = forms.ModelChoiceField(
- queryset=Device.objects.all(),
- label=_('Child Device'),
- help_text=_("Child devices must first be created and assigned to the site and rack of the parent device.")
- )
- def __init__(self, device_bay, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.fields['installed_device'].queryset = Device.objects.filter(
- site=device_bay.device.site,
- rack=device_bay.device.rack,
- parent_bay__isnull=True,
- device_type__u_height=0,
- device_type__subdevice_role=SubdeviceRoleChoices.ROLE_CHILD
- ).exclude(pk=device_bay.device.pk)
- class InventoryItemForm(DeviceComponentForm):
- parent = DynamicModelChoiceField(
- label=_('Parent'),
- queryset=InventoryItem.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- }
- )
- role = DynamicModelChoiceField(
- label=_('Role'),
- queryset=InventoryItemRole.objects.all(),
- required=False
- )
- manufacturer = DynamicModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- required=False
- )
- # Assigned component selectors
- consoleport = DynamicModelChoiceField(
- queryset=ConsolePort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Console port')
- )
- consoleserverport = DynamicModelChoiceField(
- queryset=ConsoleServerPort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Console server port')
- )
- frontport = DynamicModelChoiceField(
- queryset=FrontPort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Front port')
- )
- interface = DynamicModelChoiceField(
- queryset=Interface.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Interface')
- )
- poweroutlet = DynamicModelChoiceField(
- queryset=PowerOutlet.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Power outlet')
- )
- powerport = DynamicModelChoiceField(
- queryset=PowerPort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Power port')
- )
- rearport = DynamicModelChoiceField(
- queryset=RearPort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Rear port')
- )
- fieldsets = (
- FieldSet(
- 'device', 'parent', 'name', 'label', 'status', 'role', 'description', 'tags',
- name=_('Inventory Item')
- ),
- FieldSet('manufacturer', 'part_id', 'serial', 'asset_tag', name=_('Hardware')),
- FieldSet(
- TabbedGroups(
- FieldSet('interface', name=_('Interface')),
- FieldSet('consoleport', name=_('Console Port')),
- FieldSet('consoleserverport', name=_('Console Server Port')),
- FieldSet('frontport', name=_('Front Port')),
- FieldSet('rearport', name=_('Rear Port')),
- FieldSet('powerport', name=_('Power Port')),
- FieldSet('poweroutlet', name=_('Power Outlet')),
- ),
- name=_('Component Assignment')
- )
- )
- class Meta:
- model = InventoryItem
- fields = [
- 'device', 'parent', 'name', 'label', 'role', 'manufacturer', 'part_id', 'serial', 'asset_tag',
- 'status', 'description', 'owner', 'tags',
- ]
- def __init__(self, *args, **kwargs):
- instance = kwargs.get('instance')
- initial = kwargs.get('initial', {}).copy()
- component_type = initial.get('component_type')
- component_id = initial.get('component_id')
- if instance:
- # When editing set the initial value for component selection
- for component_model in ContentType.objects.filter(MODULAR_COMPONENT_MODELS):
- if type(instance.component) is component_model.model_class():
- initial[component_model.model] = instance.component
- break
- elif component_type and component_id:
- # When adding the InventoryItem from a component page
- if content_type := ContentType.objects.filter(MODULAR_COMPONENT_MODELS).filter(pk=component_type).first():
- if component := content_type.model_class().objects.filter(pk=component_id).first():
- initial[content_type.model] = component
- kwargs['initial'] = initial
- super().__init__(*args, **kwargs)
- # Specifically allow editing the device of IntentoryItems
- if self.instance.pk:
- self.fields['device'].disabled = False
- def clean(self):
- super().clean()
- # Handle object assignment
- selected_objects = [
- field for field in (
- 'consoleport', 'consoleserverport', 'frontport', 'interface', 'poweroutlet', 'powerport', 'rearport'
- ) if self.cleaned_data[field]
- ]
- if len(selected_objects) > 1:
- raise forms.ValidationError(_("An InventoryItem can only be assigned to a single component."))
- elif selected_objects:
- self.instance.component = self.cleaned_data[selected_objects[0]]
- else:
- self.instance.component = None
- class InventoryItemRoleForm(OrganizationalModelForm):
- fieldsets = (
- FieldSet('name', 'slug', 'color', 'description', 'tags', name=_('Inventory Item Role')),
- )
- class Meta:
- model = InventoryItemRole
- fields = [
- 'name', 'slug', 'color', 'description', 'owner', 'tags',
- ]
- class VirtualDeviceContextForm(TenancyForm, PrimaryModelForm):
- device = DynamicModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- selector=True
- )
- primary_ip4 = DynamicModelChoiceField(
- queryset=IPAddress.objects.all(),
- label=_('Primary IPv4'),
- required=False,
- query_params={
- 'device_id': '$device',
- 'family': '4',
- }
- )
- primary_ip6 = DynamicModelChoiceField(
- queryset=IPAddress.objects.all(),
- label=_('Primary IPv6'),
- required=False,
- query_params={
- 'device_id': '$device',
- 'family': '6',
- }
- )
- fieldsets = (
- FieldSet(
- 'device', 'name', 'status', 'identifier', 'primary_ip4', 'primary_ip6', 'tags',
- name=_('Virtual Device Context')
- ),
- FieldSet('tenant_group', 'tenant', name=_('Tenancy'))
- )
- class Meta:
- model = VirtualDeviceContext
- fields = [
- 'device', 'name', 'status', 'identifier', 'primary_ip4', 'primary_ip6', 'tenant_group', 'tenant', 'owner',
- 'comments', 'tags'
- ]
- #
- # Addressing
- #
- class MACAddressForm(PrimaryModelForm):
- mac_address = forms.CharField(
- required=True,
- label=_('MAC address')
- )
- interface = DynamicModelChoiceField(
- label=_('Interface'),
- queryset=Interface.objects.all(),
- required=False,
- selector=True,
- context={
- 'parent': 'device',
- },
- )
- vminterface = DynamicModelChoiceField(
- label=_('VM Interface'),
- queryset=VMInterface.objects.all(),
- required=False,
- selector=True,
- context={
- 'parent': 'virtual_machine',
- },
- )
- fieldsets = (
- FieldSet(
- 'mac_address', 'description', 'tags',
- ),
- FieldSet(
- TabbedGroups(
- FieldSet('interface', name=_('Device')),
- FieldSet('vminterface', name=_('Virtual Machine')),
- ),
- ),
- )
- class Meta:
- model = MACAddress
- fields = [
- 'mac_address', 'interface', 'vminterface', 'description', 'owner', 'tags',
- ]
- def __init__(self, *args, **kwargs):
- # Initialize helper selectors
- instance = kwargs.get('instance')
- initial = kwargs.get('initial', {}).copy()
- if instance:
- if type(instance.assigned_object) is Interface:
- initial['interface'] = instance.assigned_object
- elif type(instance.assigned_object) is VMInterface:
- initial['vminterface'] = instance.assigned_object
- kwargs['initial'] = initial
- super().__init__(*args, **kwargs)
- if instance and instance.assigned_object and instance.assigned_object.primary_mac_address:
- if instance.assigned_object.primary_mac_address.pk == instance.pk:
- self.fields['interface'].disabled = True
- self.fields['vminterface'].disabled = True
- def clean(self):
- super().clean()
- # Handle object assignment
- selected_objects = [
- field for field in ('interface', 'vminterface') if self.cleaned_data[field]
- ]
- if len(selected_objects) > 1:
- raise forms.ValidationError({
- selected_objects[1]: _("A MAC address can only be assigned to a single object.")
- })
- elif selected_objects:
- self.instance.assigned_object = self.cleaned_data[selected_objects[0]]
- else:
- self.instance.assigned_object = None
|