| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526 |
- from django import forms
- from django.contrib.contenttypes.models import ContentType
- from django.contrib.postgres.forms.array import SimpleArrayField
- from django.core.exceptions import NON_FIELD_ERRORS, MultipleObjectsReturned, ObjectDoesNotExist
- from django.utils.functional import lazy
- from django.utils.html import format_html
- from django.utils.safestring import SafeString, mark_safe
- from django.utils.translation import gettext_lazy as _
- from dcim.choices import *
- from dcim.constants import *
- from dcim.models import *
- from dcim.utils import reconcile_port_mappings
- from extras.models import ConfigTemplate
- from ipam.choices import VLANQinQRoleChoices
- from ipam.models import VLAN, VRF, IPAddress, VLANGroup
- from netbox.choices import *
- from netbox.forms import (
- NestedGroupModelImportForm,
- NetBoxModelImportForm,
- OrganizationalModelImportForm,
- OwnerCSVMixin,
- PrimaryModelImportForm,
- )
- from tenancy.models import Tenant
- from utilities.forms.fields import (
- CSVChoiceField,
- CSVContentTypeField,
- CSVModelChoiceField,
- CSVModelMultipleChoiceField,
- CSVTypedChoiceField,
- SlugField,
- )
- from virtualization.models import Cluster, VirtualMachine, VMInterface
- from wireless.choices import WirelessRoleChoices
- from .common import ModuleCommonForm
- __all__ = (
- 'CableBundleImportForm',
- 'CableImportForm',
- 'ConsolePortImportForm',
- 'ConsoleServerPortImportForm',
- 'CoolingFeedImportForm',
- 'CoolingIntakeImportForm',
- 'CoolingOutflowImportForm',
- 'CoolingSourceImportForm',
- 'DeviceBayImportForm',
- 'DeviceImportForm',
- 'DeviceRoleImportForm',
- 'DeviceTypeImportForm',
- 'FrontPortImportForm',
- 'InterfaceImportForm',
- 'InventoryItemImportForm',
- 'InventoryItemRoleImportForm',
- 'LocationImportForm',
- 'MACAddressImportForm',
- 'MACBlockImportForm',
- 'ManufacturerImportForm',
- 'ModuleBayImportForm',
- 'ModuleBayTypeImportForm',
- 'ModuleImportForm',
- 'ModuleTypeImportForm',
- 'ModuleTypeProfileImportForm',
- 'PlatformImportForm',
- 'PowerFeedImportForm',
- 'PowerOutletImportForm',
- 'PowerPanelImportForm',
- 'PowerPortImportForm',
- 'RackGroupImportForm',
- 'RackImportForm',
- 'RackReservationImportForm',
- 'RackRoleImportForm',
- 'RackTypeImportForm',
- 'RearPortImportForm',
- 'RegionImportForm',
- 'SiteGroupImportForm',
- 'SiteImportForm',
- 'VirtualChassisImportForm',
- 'VirtualDeviceContextImportForm'
- )
- # A lazily evaluated format_html(), for help text which must not resolve its translated content until
- # the field is rendered. Unlike mark_safe(), this escapes the interpolated arguments.
- format_html_lazy = lazy(format_html, SafeString)
- class RegionImportForm(NestedGroupModelImportForm):
- parent = CSVModelChoiceField(
- label=_('Parent'),
- queryset=Region.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Name of parent region')
- )
- class Meta:
- model = Region
- fields = ('name', 'slug', 'parent', 'description', 'owner', 'comments', 'tags')
- class SiteGroupImportForm(NestedGroupModelImportForm):
- parent = CSVModelChoiceField(
- label=_('Parent'),
- queryset=SiteGroup.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Name of parent site group')
- )
- class Meta:
- model = SiteGroup
- fields = ('name', 'slug', 'parent', 'description', 'owner', 'comments', 'tags')
- class SiteImportForm(PrimaryModelImportForm):
- status = CSVChoiceField(
- label=_('Status'),
- choices=SiteStatusChoices,
- help_text=_('Operational status')
- )
- region = CSVModelChoiceField(
- label=_('Region'),
- queryset=Region.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Assigned region')
- )
- group = CSVModelChoiceField(
- label=_('Group'),
- queryset=SiteGroup.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Assigned group')
- )
- tenant = CSVModelChoiceField(
- label=_('Tenant'),
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Assigned tenant')
- )
- class Meta:
- model = Site
- fields = (
- 'name', 'slug', 'status', 'region', 'group', 'tenant', 'facility', 'time_zone', 'description',
- 'physical_address', 'shipping_address', 'latitude', 'longitude', 'owner', 'comments', 'tags'
- )
- help_texts = {
- 'time_zone': mark_safe(
- '{} (<a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones">{}</a>)'.format(
- _('Time zone'), _('available options')
- )
- )
- }
- class LocationImportForm(NestedGroupModelImportForm):
- site = CSVModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text=_('Assigned site')
- )
- parent = CSVModelChoiceField(
- label=_('Parent'),
- queryset=Location.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Parent location'),
- error_messages={
- 'invalid_choice': _('Location not found.'),
- }
- )
- status = CSVChoiceField(
- label=_('Status'),
- choices=LocationStatusChoices,
- help_text=_('Operational status')
- )
- tenant = CSVModelChoiceField(
- label=_('Tenant'),
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Assigned tenant')
- )
- class Meta:
- model = Location
- fields = (
- 'site', 'parent', 'name', 'slug', 'status', 'tenant', 'facility', 'description', 'owner', 'comments',
- 'tags',
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit location queryset by assigned site
- params = self.get_related_filter('site', 'site', data.get('site'))
- self.fields['parent'].queryset = self.fields['parent'].queryset.filter(**params)
- class RackGroupImportForm(OrganizationalModelImportForm):
- class Meta:
- model = RackGroup
- fields = ('name', 'slug', 'description', 'owner', 'comments', 'tags')
- class RackRoleImportForm(OrganizationalModelImportForm):
- class Meta:
- model = RackRole
- fields = ('name', 'slug', 'color', 'description', 'owner', 'comments', 'tags')
- class RackTypeImportForm(PrimaryModelImportForm):
- manufacturer = forms.ModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- to_field_name='name',
- help_text=_('The manufacturer of this rack type')
- )
- form_factor = CSVChoiceField(
- label=_('Type'),
- choices=RackFormFactorChoices,
- help_text=_('Form factor')
- )
- starting_unit = forms.IntegerField(
- required=False,
- min_value=1,
- help_text=_('The lowest-numbered position in the rack')
- )
- width = forms.ChoiceField(
- label=_('Width'),
- choices=RackWidthChoices,
- help_text=_('Rail-to-rail width (in inches)')
- )
- outer_unit = CSVChoiceField(
- label=_('Outer unit'),
- choices=RackDimensionUnitChoices,
- required=False,
- help_text=_('Unit for outer dimensions')
- )
- weight_unit = CSVChoiceField(
- label=_('Weight unit'),
- choices=WeightUnitChoices,
- required=False,
- help_text=_('Unit for rack weights')
- )
- cooling_capability = CSVChoiceField(
- label=_('Cooling capability'),
- choices=RackCoolingCapabilityChoices,
- required=False,
- help_text=_('Cooling capability')
- )
- 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', 'cooling_capability', 'cooling_capacity', 'description', 'owner', 'comments', 'tags',
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- class RackImportForm(PrimaryModelImportForm):
- site = CSVModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- to_field_name='name'
- )
- location = CSVModelChoiceField(
- label=_('Location'),
- queryset=Location.objects.all(),
- required=False,
- to_field_name='name'
- )
- tenant = CSVModelChoiceField(
- label=_('Tenant'),
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Name of assigned tenant')
- )
- group = CSVModelChoiceField(
- label=_('Rack group'),
- queryset=RackGroup.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Name of assigned group')
- )
- status = CSVChoiceField(
- label=_('Status'),
- choices=RackStatusChoices,
- help_text=_('Operational status')
- )
- role = CSVModelChoiceField(
- label=_('Role'),
- queryset=RackRole.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Name of assigned role')
- )
- rack_type = CSVModelChoiceField(
- label=_('Rack type'),
- queryset=RackType.objects.all(),
- to_field_name='model',
- required=False,
- help_text=_('Rack type model')
- )
- form_factor = CSVChoiceField(
- label=_('Type'),
- choices=RackFormFactorChoices,
- required=False,
- help_text=_('Form factor')
- )
- width = forms.ChoiceField(
- label=_('Width'),
- choices=RackWidthChoices,
- required=False,
- help_text=_('Rail-to-rail width (in inches)')
- )
- u_height = forms.IntegerField(
- required=False,
- label=_('Height (U)')
- )
- outer_unit = CSVChoiceField(
- label=_('Outer unit'),
- choices=RackDimensionUnitChoices,
- required=False,
- help_text=_('Unit for outer dimensions')
- )
- airflow = CSVChoiceField(
- label=_('Airflow'),
- choices=RackAirflowChoices,
- required=False,
- help_text=_('Airflow direction')
- )
- cooling_capability = CSVChoiceField(
- label=_('Cooling capability'),
- choices=RackCoolingCapabilityChoices,
- required=False,
- help_text=_('Cooling capability')
- )
- weight_unit = CSVChoiceField(
- label=_('Weight unit'),
- choices=WeightUnitChoices,
- required=False,
- help_text=_('Unit for rack weights')
- )
- class Meta:
- model = Rack
- fields = (
- 'site', 'location', 'group', 'name', 'facility_id', 'tenant', 'status', 'role', 'rack_type', 'form_factor',
- 'serial', 'asset_tag', 'width', 'u_height', 'desc_units', 'outer_width', 'outer_height', 'outer_depth',
- 'outer_unit', 'mounting_depth', 'airflow', 'cooling_capability', 'cooling_capacity', 'weight',
- 'max_weight', 'weight_unit', 'description', 'owner', 'comments', 'tags',
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit location queryset by assigned site
- params = self.get_related_filter('site', 'site', data.get('site'))
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- def clean(self):
- super().clean()
- # width & u_height must be set if not specifying a rack type on import
- if not self.instance.pk:
- if not self.cleaned_data.get('rack_type') and not self.cleaned_data.get('width'):
- raise forms.ValidationError(_("Width must be set if not specifying a rack type."))
- if not self.cleaned_data.get('rack_type') and not self.cleaned_data.get('u_height'):
- raise forms.ValidationError(_("U height must be set if not specifying a rack type."))
- class RackReservationImportForm(PrimaryModelImportForm):
- site = CSVModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text=_('Parent site')
- )
- location = CSVModelChoiceField(
- label=_('Location'),
- queryset=Location.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_("Rack's location (if any)")
- )
- rack = CSVModelChoiceField(
- label=_('Rack'),
- queryset=Rack.objects.all(),
- to_field_name='name',
- help_text=_('Rack')
- )
- units = SimpleArrayField(
- label=_('Units'),
- base_field=forms.IntegerField(),
- required=True,
- help_text=_('Comma-separated list of individual unit numbers')
- )
- status = CSVChoiceField(
- label=_('Status'),
- choices=RackReservationStatusChoices,
- help_text=_('Operational status')
- )
- tenant = CSVModelChoiceField(
- label=_('Tenant'),
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Assigned tenant')
- )
- class Meta:
- model = RackReservation
- fields = ('site', 'location', 'rack', 'units', 'status', 'tenant', 'description', 'owner', 'comments', 'tags')
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit location queryset by assigned site
- params = self.get_related_filter('site', 'site', data.get('site'))
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- # Limit rack queryset by assigned site and group
- params = {
- **self.get_related_filter('site', 'site', data.get('site')),
- **self.get_related_filter('location', 'location', data.get('location')),
- }
- self.fields['rack'].queryset = self.fields['rack'].queryset.filter(**params)
- class ManufacturerImportForm(OrganizationalModelImportForm):
- class Meta:
- model = Manufacturer
- fields = ('name', 'slug', 'description', 'owner', 'comments', 'tags')
- class DeviceTypeImportForm(PrimaryModelImportForm):
- manufacturer = CSVModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- to_field_name='name',
- help_text=_('The manufacturer which produces this device type')
- )
- default_platform = CSVModelChoiceField(
- label=_('Default platform'),
- queryset=Platform.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('The default platform for devices of this type (optional)')
- )
- weight = forms.DecimalField(
- label=_('Weight'),
- required=False,
- help_text=_('Device weight'),
- )
- weight_unit = CSVChoiceField(
- label=_('Weight unit'),
- choices=WeightUnitChoices,
- required=False,
- help_text=_('Unit for device weight')
- )
- class Meta:
- model = DeviceType
- fields = [
- 'manufacturer', 'default_platform', 'model', 'slug', 'part_number', 'u_height', 'exclude_from_utilization',
- 'is_full_depth', 'subdevice_role', 'airflow', 'cooling_method', 'description', 'weight', 'weight_unit',
- 'end_of_life', 'owner', 'comments', 'tags',
- ]
- class ModuleBayTypeImportForm(PrimaryModelImportForm):
- manufacturer = CSVModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- to_field_name='name',
- required=False,
- )
- class Meta:
- model = ModuleBayType
- fields = [
- 'name', 'slug', 'manufacturer', 'color', 'description', 'owner', 'comments', 'tags',
- ]
- class ModuleTypeProfileImportForm(PrimaryModelImportForm):
- class Meta:
- model = ModuleTypeProfile
- fields = [
- 'name', 'description', 'schema', 'owner', 'comments', 'tags',
- ]
- class ModuleTypeImportForm(PrimaryModelImportForm):
- module_bay_types = CSVModelMultipleChoiceField(
- label=_('Module bay types'),
- queryset=ModuleBayType.objects.all(),
- to_field_name='name',
- required=False,
- )
- profile = forms.ModelChoiceField(
- label=_('Profile'),
- queryset=ModuleTypeProfile.objects.all(),
- to_field_name='name',
- required=False
- )
- manufacturer = forms.ModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- to_field_name='name'
- )
- airflow = CSVChoiceField(
- label=_('Airflow'),
- choices=ModuleAirflowChoices,
- required=False,
- help_text=_('Airflow direction')
- )
- cooling_method = CSVChoiceField(
- label=_('Cooling method'),
- choices=CoolingMethodChoices,
- required=False,
- help_text=_('Cooling method')
- )
- weight = forms.DecimalField(
- label=_('Weight'),
- required=False,
- help_text=_('Module weight'),
- )
- weight_unit = CSVChoiceField(
- label=_('Weight unit'),
- choices=WeightUnitChoices,
- required=False,
- help_text=_('Unit for module weight')
- )
- attribute_data = forms.JSONField(
- label=_('Attributes'),
- required=False,
- help_text=_('Attribute values for the assigned profile, passed as a dictionary')
- )
- class Meta:
- model = ModuleType
- fields = [
- 'manufacturer', 'model', 'part_number', 'description', 'cooling_method', 'airflow', 'weight', 'weight_unit',
- 'end_of_life', 'profile', 'attribute_data', 'module_bay_types', 'owner', 'comments', 'tags',
- ]
- def clean(self):
- super().clean()
- # Attribute data may be included only if a profile is specified
- if self.cleaned_data.get('attribute_data') and not self.cleaned_data.get('profile'):
- raise forms.ValidationError(_("Profile must be specified if attribute data is provided."))
- # Default attribute_data to an empty dictionary if a profile is specified (to enforce schema validation)
- if self.cleaned_data.get('profile') and not self.cleaned_data.get('attribute_data'):
- self.cleaned_data['attribute_data'] = {}
- module_bay_types = self.cleaned_data.get('module_bay_types')
- manufacturer = self.cleaned_data.get('manufacturer')
- # If importing an update to an existing module type and the manufacturer is not in
- # the import data, use the existing manufacturer.
- if manufacturer is None and self.instance.pk:
- manufacturer = self.instance.manufacturer
- if not module_bay_types or manufacturer is None:
- return
- # Match the module's manufacturer or a global bay type, preferring the former
- # when a name is shared, as with module bay template imports.
- by_name = {}
- for module_bay_type in module_bay_types:
- if module_bay_type.manufacturer_id not in (None, manufacturer.pk):
- continue
- existing = by_name.get(module_bay_type.name)
- if existing is None or module_bay_type.manufacturer_id is not None:
- by_name[module_bay_type.name] = module_bay_type
- # A name matching only some other manufacturer's bay type is rejected rather than
- # resolved to it.
- for module_bay_type in module_bay_types:
- if module_bay_type.name not in by_name:
- raise forms.ValidationError({
- 'module_bay_types': forms.ValidationError(
- self.fields['module_bay_types'].error_messages['invalid_choice'],
- code='invalid_choice',
- params={'value': module_bay_type.name},
- )
- })
- self.cleaned_data['module_bay_types'] = list(by_name.values())
- class DeviceRoleImportForm(NestedGroupModelImportForm):
- parent = CSVModelChoiceField(
- label=_('Parent'),
- queryset=DeviceRole.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Parent Device Role'),
- error_messages={
- 'invalid_choice': _('Device role not found.'),
- }
- )
- config_template = CSVModelChoiceField(
- label=_('Config template'),
- queryset=ConfigTemplate.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Config template')
- )
- class Meta:
- model = DeviceRole
- fields = (
- 'name', 'slug', 'parent', 'color', 'vm_role', 'config_template', 'description', 'owner', 'comments', 'tags'
- )
- class PlatformImportForm(NestedGroupModelImportForm):
- parent = CSVModelChoiceField(
- label=_('Parent'),
- queryset=Platform.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Parent platform'),
- error_messages={
- 'invalid_choice': _('Platform not found.'),
- }
- )
- manufacturer = CSVModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Limit platform assignments to this manufacturer')
- )
- config_template = CSVModelChoiceField(
- label=_('Config template'),
- queryset=ConfigTemplate.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Config template')
- )
- class Meta:
- model = Platform
- fields = (
- 'name', 'slug', 'parent', 'manufacturer', 'config_template', 'description', 'owner', 'comments', 'tags',
- )
- class BaseDeviceImportForm(PrimaryModelImportForm):
- role = CSVModelChoiceField(
- label=_('Device role'),
- queryset=DeviceRole.objects.all(),
- to_field_name='name',
- help_text=_('Assigned role')
- )
- tenant = CSVModelChoiceField(
- label=_('Tenant'),
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Assigned tenant')
- )
- manufacturer = CSVModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- to_field_name='name',
- help_text=_('Device type manufacturer')
- )
- device_type = CSVModelChoiceField(
- label=_('Device type'),
- queryset=DeviceType.objects.all(),
- to_field_name='model',
- help_text=_('Device type model')
- )
- platform = CSVModelChoiceField(
- label=_('Platform'),
- queryset=Platform.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Assigned platform')
- )
- status = CSVChoiceField(
- label=_('Status'),
- choices=DeviceStatusChoices,
- help_text=_('Operational status')
- )
- virtual_chassis = CSVModelChoiceField(
- label=_('Virtual chassis'),
- queryset=VirtualChassis.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Virtual chassis')
- )
- cluster = CSVModelChoiceField(
- label=_('Cluster'),
- queryset=Cluster.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Virtualization cluster')
- )
- class Meta:
- fields = []
- model = Device
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit device type queryset by manufacturer
- params = self.get_related_filter('manufacturer', 'manufacturer', data.get('manufacturer'))
- self.fields['device_type'].queryset = self.fields['device_type'].queryset.filter(**params)
- class DeviceImportForm(BaseDeviceImportForm):
- site = CSVModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text=_('Assigned site')
- )
- location = CSVModelChoiceField(
- label=_('Location'),
- queryset=Location.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_("Assigned location (if any)")
- )
- rack = CSVModelChoiceField(
- label=_('Rack'),
- queryset=Rack.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_("Assigned rack (if any)")
- )
- face = CSVChoiceField(
- label=_('Face'),
- choices=DeviceFaceChoices,
- required=False,
- help_text=_('Mounted rack face')
- )
- parent = CSVModelChoiceField(
- label=_('Parent'),
- queryset=Device.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Parent device (for child devices)')
- )
- device_bay = CSVModelChoiceField(
- label=_('Device bay'),
- queryset=DeviceBay.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Device bay in which this device is installed (for child devices)')
- )
- airflow = CSVChoiceField(
- label=_('Airflow'),
- choices=DeviceAirflowChoices,
- required=False,
- help_text=_('Airflow direction')
- )
- cooling_method = CSVChoiceField(
- label=_('Cooling method'),
- choices=CoolingMethodChoices,
- required=False,
- help_text=_('Cooling method')
- )
- config_template = CSVModelChoiceField(
- label=_('Config template'),
- queryset=ConfigTemplate.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Config template')
- )
- replicate_components = forms.BooleanField(
- label=_('Replicate components'),
- required=False,
- help_text=_('Automatically populate components associated with this device type (enabled by default)')
- )
- class Meta(BaseDeviceImportForm.Meta):
- fields = [
- 'name', 'role', 'tenant', 'manufacturer', 'device_type', 'platform', 'serial', 'asset_tag', 'status',
- 'site', 'location', 'rack', 'position', 'face', 'latitude', 'longitude', 'parent', 'device_bay', 'airflow',
- 'cooling_method', 'virtual_chassis', 'vc_position', 'vc_priority', 'cluster', 'description',
- 'config_template', 'owner', 'comments', 'replicate_components', 'tags',
- ]
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit location queryset by assigned site
- params = self.get_related_filter('site', 'site', data.get('site'))
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- self.fields['parent'].queryset = self.fields['parent'].queryset.filter(**params)
- # Limit rack queryset by assigned site and location
- params = self.get_related_filter('site', 'site', data.get('site'))
- if location := data.get('location'):
- params.update(self.get_related_filter('location', 'location', location))
- self.fields['rack'].queryset = self.fields['rack'].queryset.filter(**params)
- # Limit platform queryset by manufacturer
- params = self.get_related_filter('manufacturer', 'manufacturer', data.get('manufacturer'))
- self.fields['platform'].queryset = self.fields['platform'].queryset.filter(
- Q(**params) | Q(manufacturer=None)
- )
- # Limit device bay queryset by parent device
- if parent := data.get('parent'):
- params = self.get_related_filter('device', 'parent', parent)
- self.fields['device_bay'].queryset = self.fields['device_bay'].queryset.filter(**params)
- def clean(self):
- super().clean()
- # Inherit site and rack from parent device
- if parent := self.cleaned_data.get('parent'):
- self.instance.site = parent.site
- self.instance.rack = parent.rack
- # Set parent_bay reverse relationship
- if device_bay := self.cleaned_data.get('device_bay'):
- self.instance.parent_bay = device_bay
- # Disable component replication for a new device if requested
- if not self.instance.pk and not self.cleaned_data.get('replicate_components'):
- self.instance._disable_replication = True
- def clean_replicate_components(self):
- # Default to True when replicate_components is omitted or left blank in the uploaded data
- if self.data.get('replicate_components') in (None, ''):
- return True
- return self.cleaned_data['replicate_components']
- class ModuleImportForm(ModuleCommonForm, PrimaryModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name',
- help_text=_('The device in which this module is installed')
- )
- module_bay = CSVModelChoiceField(
- label=_('Module bay'),
- queryset=ModuleBay.objects.all(),
- to_field_name='name',
- help_text=_('The module bay in which this module is installed')
- )
- module_type = CSVModelChoiceField(
- label=_('Module type'),
- queryset=ModuleType.objects.all(),
- to_field_name='model',
- help_text=_('The type of module')
- )
- status = CSVChoiceField(
- label=_('Status'),
- choices=ModuleStatusChoices,
- help_text=_('Operational status')
- )
- replicate_components = forms.BooleanField(
- label=_('Replicate components'),
- required=False,
- help_text=_('Automatically populate components associated with this module type (enabled by default)')
- )
- adopt_components = forms.BooleanField(
- label=_('Adopt components'),
- required=False,
- help_text=_('Adopt already existing components')
- )
- class Meta:
- model = Module
- fields = (
- 'device', 'module_bay', 'module_type', 'serial', 'asset_tag', 'status', 'description', 'owner', 'comments',
- 'replicate_components', 'adopt_components', 'tags',
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit module_bay queryset by assigned device
- params = self.get_related_filter('device', 'device', data.get('device'))
- self.fields['module_bay'].queryset = self.fields['module_bay'].queryset.filter(**params)
- def clean_replicate_components(self):
- # Make sure replicate_components is True when it's not included in the uploaded data
- if 'replicate_components' not in self.data:
- return True
- return self.cleaned_data['replicate_components']
- #
- # Device components
- #
- class ConsolePortImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- label=_('Type'),
- choices=ConsolePortTypeChoices,
- required=False,
- help_text=_('Port type')
- )
- speed = CSVTypedChoiceField(
- label=_('Speed'),
- choices=ConsolePortSpeedChoices,
- coerce=int,
- empty_value=None,
- required=False,
- help_text=_('Port speed in bps')
- )
- class Meta:
- model = ConsolePort
- fields = ('device', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'owner', 'tags')
- class ConsoleServerPortImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- label=_('Type'),
- choices=ConsolePortTypeChoices,
- required=False,
- help_text=_('Port type')
- )
- speed = CSVTypedChoiceField(
- label=_('Speed'),
- choices=ConsolePortSpeedChoices,
- coerce=int,
- empty_value=None,
- required=False,
- help_text=_('Port speed in bps')
- )
- class Meta:
- model = ConsoleServerPort
- fields = ('device', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'owner', 'tags')
- class PowerPortImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- label=_('Type'),
- choices=PowerPortTypeChoices,
- required=False,
- help_text=_('Port type')
- )
- class Meta:
- model = PowerPort
- fields = (
- 'device', 'name', 'label', 'type', 'mark_connected', 'maximum_draw', 'allocated_draw', 'description',
- 'owner', 'tags',
- )
- class PowerOutletImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- label=_('Type'),
- choices=PowerOutletTypeChoices,
- required=False,
- help_text=_('Outlet type')
- )
- power_port = CSVModelChoiceField(
- label=_('Power port'),
- queryset=PowerPort.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Local power port which feeds this outlet')
- )
- feed_leg = CSVChoiceField(
- label=_('Feed leg'),
- choices=PowerOutletFeedLegChoices,
- required=False,
- help_text=_('Electrical phase (for three-phase circuits)')
- )
- class Meta:
- model = PowerOutlet
- fields = (
- 'device', 'name', 'label', 'type', 'color', 'mark_connected', 'power_port', 'feed_leg', 'description',
- 'owner', 'tags',
- )
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Limit PowerPort choices to those belonging to this device (or VC master)
- if self.is_bound and 'device' in self.data:
- try:
- device = self.fields['device'].to_python(self.data['device'])
- except forms.ValidationError:
- device = None
- else:
- try:
- device = self.instance.device
- except Device.DoesNotExist:
- device = None
- if device:
- self.fields['power_port'].queryset = PowerPort.objects.filter(
- device__in=[device, device.get_vc_master()]
- )
- else:
- self.fields['power_port'].queryset = PowerPort.objects.none()
- class CoolingIntakeImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- label=_('Type'),
- choices=CoolingConnectorTypeChoices,
- required=False,
- help_text=_('Physical connector type')
- )
- diameter_unit = CSVChoiceField(
- label=_('Diameter unit'),
- choices=DiameterUnitChoices,
- required=False,
- help_text=_('Diameter unit')
- )
- max_flow_unit = CSVChoiceField(
- label=_('Max flow unit'),
- choices=FlowRateUnitChoices,
- required=False,
- help_text=_('Unit for maximum flow')
- )
- cooling_outflow_device = CSVModelChoiceField(
- label=_('Cooling outflow device'),
- queryset=Device.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Device bearing the upstream cooling outflow (defaults to this intake\'s device)')
- )
- cooling_outflow = CSVModelChoiceField(
- label=_('Cooling outflow'),
- queryset=CoolingOutflow.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Upstream cooling outflow which feeds this intake')
- )
- class Meta:
- model = CoolingIntake
- fields = (
- 'device', 'name', 'label', 'type', 'diameter', 'diameter_unit', 'max_flow',
- 'max_flow_unit', 'cooling_outflow_device', 'cooling_outflow', 'description', 'owner', 'tags',
- )
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # The supplying outflow typically belongs to an upstream device (e.g. a CDU or manifold), so scope the
- # CoolingOutflow choices to cooling_outflow_device where given, falling back to this intake's own device.
- if self.is_bound and 'device' in self.data:
- try:
- device = self.fields['device'].to_python(self.data['device'])
- except forms.ValidationError:
- device = None
- else:
- try:
- device = self.instance.device
- except Device.DoesNotExist:
- device = None
- outflow_device = None
- if self.is_bound and self.data.get('cooling_outflow_device'):
- try:
- outflow_device = self.fields['cooling_outflow_device'].to_python(
- self.data['cooling_outflow_device']
- )
- except forms.ValidationError:
- outflow_device = None
- if outflow_device:
- self.fields['cooling_outflow'].queryset = CoolingOutflow.objects.filter(device=outflow_device)
- elif device:
- self.fields['cooling_outflow'].queryset = CoolingOutflow.objects.filter(
- device__in=[device, device.get_vc_master()]
- )
- else:
- self.fields['cooling_outflow'].queryset = CoolingOutflow.objects.none()
- class CoolingOutflowImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- label=_('Type'),
- choices=CoolingConnectorTypeChoices,
- required=False,
- help_text=_('Physical connector type')
- )
- diameter_unit = CSVChoiceField(
- label=_('Diameter unit'),
- choices=DiameterUnitChoices,
- required=False,
- help_text=_('Diameter unit')
- )
- cooling_intake = CSVModelChoiceField(
- label=_('Cooling intake'),
- queryset=CoolingIntake.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Local cooling intake which feeds this outflow')
- )
- class Meta:
- model = CoolingOutflow
- fields = (
- 'device', 'name', 'label', 'type', 'diameter', 'diameter_unit',
- 'cooling_intake', 'description', 'owner', 'tags',
- )
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Limit CoolingIntake choices to those belonging to this device (or VC master)
- if self.is_bound and 'device' in self.data:
- try:
- device = self.fields['device'].to_python(self.data['device'])
- except forms.ValidationError:
- device = None
- else:
- try:
- device = self.instance.device
- except Device.DoesNotExist:
- device = None
- if device:
- self.fields['cooling_intake'].queryset = CoolingIntake.objects.filter(
- device__in=[device, device.get_vc_master()]
- )
- else:
- self.fields['cooling_intake'].queryset = CoolingIntake.objects.none()
- class InterfaceImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- parent = CSVModelChoiceField(
- label=_('Parent'),
- queryset=Interface.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Parent interface')
- )
- bridge = CSVModelChoiceField(
- label=_('Bridge'),
- queryset=Interface.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Bridged interface')
- )
- lag = CSVModelChoiceField(
- label=_('Lag'),
- queryset=Interface.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Parent LAG interface')
- )
- vdcs = CSVModelMultipleChoiceField(
- label=_('Vdcs'),
- queryset=VirtualDeviceContext.objects.all(),
- required=False,
- to_field_name='name',
- help_text=mark_safe(
- _('VDC names separated by commas, encased with double quotes. Example:') + ' <code>"vdc1,vdc2,vdc3"</code>'
- )
- )
- type = CSVChoiceField(
- label=_('Type'),
- choices=InterfaceTypeChoices,
- help_text=_('Physical medium')
- )
- duplex = CSVChoiceField(
- label=_('Duplex'),
- choices=InterfaceDuplexChoices,
- required=False
- )
- poe_mode = CSVChoiceField(
- label=_('Poe mode'),
- choices=InterfacePoEModeChoices,
- required=False,
- help_text=_('PoE mode')
- )
- poe_type = CSVChoiceField(
- label=_('Poe type'),
- choices=InterfacePoETypeChoices,
- required=False,
- help_text=_('PoE type')
- )
- lag_mode = CSVChoiceField(
- label=_('LAG mode'),
- choices=InterfaceLAGModeChoices,
- required=False,
- help_text=_('LAG mode')
- )
- lag_hash_policy = CSVChoiceField(
- label=_('LAG hash policy'),
- choices=InterfaceLAGHashPolicyChoices,
- required=False,
- help_text=_('LAG hash policy')
- )
- mode = CSVChoiceField(
- label=_('Mode'),
- choices=InterfaceModeChoices,
- required=False,
- help_text=_('IEEE 802.1Q operational mode (for L2 interfaces)'),
- )
- vlan_group = CSVModelChoiceField(
- label=_('VLAN group'),
- queryset=VLANGroup.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Filter VLANs available for assignment by group'),
- )
- untagged_vlan = CSVModelChoiceField(
- label=_('Untagged VLAN'),
- queryset=VLAN.objects.all(),
- required=False,
- to_field_name='vid',
- help_text=_('Assigned untagged VLAN ID (filtered by VLAN group)'),
- )
- tagged_vlans = CSVModelMultipleChoiceField(
- label=_('Tagged VLANs'),
- queryset=VLAN.objects.all(),
- required=False,
- to_field_name='vid',
- help_text=mark_safe(
- _(
- 'Assigned tagged VLAN IDs separated by commas, encased with double quotes '
- '(filtered by VLAN group). Example:'
- )
- + ' <code>"100,200,300"</code>'
- ),
- )
- qinq_svlan = CSVModelChoiceField(
- label=_('Q-in-Q Service VLAN'),
- queryset=VLAN.objects.filter(qinq_role=VLANQinQRoleChoices.ROLE_SERVICE),
- required=False,
- to_field_name='vid',
- help_text=_('Assigned Q-in-Q Service VLAN ID (filtered by VLAN group)'),
- )
- vrf = CSVModelChoiceField(
- label=_('VRF'),
- queryset=VRF.objects.all(),
- required=False,
- to_field_name='rd',
- help_text=_('Assigned VRF')
- )
- rf_role = CSVChoiceField(
- label=_('Rf role'),
- choices=WirelessRoleChoices,
- required=False,
- help_text=_('Wireless role (AP/station)')
- )
- class Meta:
- model = Interface
- fields = (
- 'device', 'name', 'label', 'parent', 'bridge', 'lag', 'type', 'channels', 'channel_id', 'speed', 'duplex',
- 'enabled', 'mark_connected', 'wwn', 'vdcs', 'mtu', 'mgmt_only', 'description', 'poe_mode', 'poe_type',
- 'lag_mode', 'lag_hash_policy', 'mode', 'vlan_group', 'untagged_vlan', 'tagged_vlans', 'qinq_svlan', 'vrf',
- 'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'tx_power', 'owner', 'tags'
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit choices for parent, bridge, and LAG interfaces to the assigned device
- if device := data.get('device'):
- params = self.get_related_filter('device', 'device', device)
- self.fields['parent'].queryset = self.fields['parent'].queryset.filter(**params)
- self.fields['bridge'].queryset = self.fields['bridge'].queryset.filter(**params)
- self.fields['lag'].queryset = self.fields['lag'].queryset.filter(**params)
- self.fields['vdcs'].queryset = self.fields['vdcs'].queryset.filter(**params)
- # Limit choices for VLANs to the assigned VLAN group
- if vlan_group := data.get('vlan_group'):
- params = self.get_related_filter('group', 'vlan_group', vlan_group)
- self.fields['untagged_vlan'].queryset = self.fields['untagged_vlan'].queryset.filter(**params)
- self.fields['tagged_vlans'].queryset = self.fields['tagged_vlans'].queryset.filter(**params)
- self.fields['qinq_svlan'].queryset = self.fields['qinq_svlan'].queryset.filter(**params)
- def clean_enabled(self):
- # Make sure enabled is True when it's not included in the uploaded data
- if 'enabled' not in self.data:
- return True
- return self.cleaned_data['enabled']
- def clean_vdcs(self):
- for vdc in self.cleaned_data['vdcs']:
- if vdc.device != self.cleaned_data['device']:
- raise forms.ValidationError(
- _("VDC {vdc} is not assigned to device {device}").format(
- vdc=vdc, device=self.cleaned_data['device']
- )
- )
- return self.cleaned_data['vdcs']
- class FrontPortImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- label=_('Type'),
- choices=PortTypeChoices,
- help_text=_('Physical medium classification')
- )
- rear_port = CSVModelChoiceField(
- label=_('Rear port'),
- queryset=RearPort.objects.all(),
- to_field_name='name',
- help_text=_('Corresponding rear port (mapped to the front port\'s first position)')
- )
- rear_port_position = forms.IntegerField(
- label=_('Rear port position'),
- required=False,
- help_text=_('Mapped position on the corresponding rear port (defaults to 1)')
- )
- class Meta:
- model = FrontPort
- fields = (
- 'device', 'name', 'label', 'type', 'color', 'mark_connected', 'positions', 'rear_port',
- 'rear_port_position', 'description', 'owner', 'tags'
- )
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Limit RearPort choices to those belonging to this device (or VC master)
- if self.is_bound and 'device' in self.data:
- try:
- device = self.fields['device'].to_python(self.data['device'])
- except forms.ValidationError:
- device = None
- else:
- try:
- device = self.instance.device
- except Device.DoesNotExist:
- device = None
- if device:
- self.fields['rear_port'].queryset = RearPort.objects.filter(
- device__in=[device, device.get_vc_master()]
- )
- else:
- self.fields['rear_port'].queryset = RearPort.objects.none()
- def clean(self):
- super().clean()
- rear_port = self.cleaned_data.get('rear_port')
- rear_port_position = self.cleaned_data.get('rear_port_position') or 1
- if not rear_port:
- return
- # Validate the rear port position against the selected rear port
- if rear_port_position > rear_port.positions:
- raise forms.ValidationError({
- 'rear_port_position': _(
- "Invalid rear port position ({rear_port_position}): Rear port {name} has only {positions} "
- "positions."
- ).format(
- rear_port_position=rear_port_position,
- name=rear_port.name,
- positions=rear_port.positions
- )
- })
- # Ensure the target rear port position isn't already occupied. reconcile_port_mappings() creates the
- # mapping via create() (bypassing validate_unique()), so without this check a collision would surface
- # as an uncaught IntegrityError (HTTP 500) rather than a row-level validation error.
- occupied = PortMapping.objects.filter(
- rear_port=rear_port, rear_port_position=rear_port_position
- ).exclude(front_port=self.instance.pk)
- if occupied.exists():
- raise forms.ValidationError({
- 'rear_port_position': _(
- "Rear port {name} position {rear_port_position} is already occupied."
- ).format(
- name=rear_port.name,
- rear_port_position=rear_port_position
- )
- })
- def _save_m2m(self):
- super()._save_m2m()
- # Map the front port's first position to the specified rear port & position
- if rear_port := self.cleaned_data.get('rear_port'):
- reconcile_port_mappings(
- PortMapping,
- parent_field='front_port',
- parent=self.instance,
- desired=[{
- 'front_port_position': 1,
- 'rear_port_id': rear_port.pk,
- 'rear_port_position': self.cleaned_data.get('rear_port_position') or 1,
- }],
- )
- class RearPortImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- label=_('Type'),
- help_text=_('Physical medium classification'),
- choices=PortTypeChoices,
- )
- class Meta:
- model = RearPort
- fields = (
- 'device', 'name', 'label', 'type', 'color', 'mark_connected', 'positions', 'description', 'owner', 'tags',
- )
- class ModuleBayImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- class Meta:
- model = ModuleBay
- fields = ('device', 'name', 'label', 'position', 'enabled', 'description', 'owner', 'tags')
- def clean_enabled(self):
- # Make sure enabled is True when it's not included in the uploaded data
- if 'enabled' not in self.data:
- return True
- return self.cleaned_data['enabled']
- class DeviceBayImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- installed_device = CSVModelChoiceField(
- label=_('Installed device'),
- queryset=Device.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Child device installed within this bay'),
- error_messages={
- 'invalid_choice': _('Child device not found.'),
- }
- )
- class Meta:
- model = DeviceBay
- fields = ('device', 'name', 'label', 'enabled', 'installed_device', 'description', 'owner', 'tags')
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Limit installed device choices to devices of the correct type and location
- if self.is_bound and 'device' in self.data:
- try:
- device = self.fields['device'].to_python(self.data['device'])
- except forms.ValidationError:
- device = None
- else:
- try:
- device = self.instance.device
- except Device.DoesNotExist:
- device = None
- if device:
- self.fields['installed_device'].queryset = Device.objects.filter(
- site=device.site,
- rack=device.rack,
- parent_bay__isnull=True,
- device_type__u_height=0,
- device_type__subdevice_role=SubdeviceRoleChoices.ROLE_CHILD
- ).exclude(pk=device.pk)
- else:
- self.fields['installed_device'].queryset = Device.objects.none()
- def clean_enabled(self):
- # Make sure enabled is True when it's not included in the uploaded data
- if 'enabled' not in self.data:
- return True
- return self.cleaned_data['enabled']
- class InventoryItemImportForm(OwnerCSVMixin, NetBoxModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- role = CSVModelChoiceField(
- label=_('Role'),
- queryset=InventoryItemRole.objects.all(),
- to_field_name='name',
- required=False
- )
- manufacturer = CSVModelChoiceField(
- label=_('Manufacturer'),
- queryset=Manufacturer.objects.all(),
- to_field_name='name',
- required=False
- )
- parent = CSVModelChoiceField(
- label=_('Parent'),
- queryset=Device.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Parent inventory item')
- )
- component_type = CSVContentTypeField(
- label=_('Component type'),
- queryset=ContentType.objects.all(),
- limit_choices_to=MODULAR_COMPONENT_MODELS,
- required=False,
- help_text=_('Component Type')
- )
- component_name = forms.CharField(
- label=_('Component name'),
- required=False,
- help_text=_('Component Name')
- )
- status = CSVChoiceField(
- label=_('Status'),
- choices=InventoryItemStatusChoices,
- help_text=_('Operational status')
- )
- class Meta:
- model = InventoryItem
- fields = (
- 'device', 'name', 'label', 'status', 'role', 'manufacturer', 'parent', 'part_id', 'serial', 'asset_tag',
- 'discovered', 'description', 'owner', 'tags', 'component_type', 'component_name',
- )
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Limit parent choices to inventory items belonging to this device
- device = None
- if self.is_bound and 'device' in self.data:
- try:
- device = self.fields['device'].to_python(self.data['device'])
- except forms.ValidationError:
- pass
- if device:
- self.fields['parent'].queryset = InventoryItem.objects.filter(device=device)
- else:
- self.fields['parent'].queryset = InventoryItem.objects.none()
- def clean(self):
- super().clean()
- cleaned_data = self.cleaned_data
- component_type = cleaned_data.get('component_type')
- component_name = cleaned_data.get('component_name')
- device = self.cleaned_data.get("device")
- if component_type:
- if device is None:
- cleaned_data.pop('component_type', None)
- if component_name is None:
- cleaned_data.pop('component_type', None)
- raise forms.ValidationError(
- _("Component name must be specified when component type is specified")
- )
- if all([device, component_name]):
- try:
- model = component_type.model_class()
- self.instance.component = model.objects.get(device=device, name=component_name)
- except ObjectDoesNotExist:
- cleaned_data.pop('component_type', None)
- cleaned_data.pop('component_name', None)
- raise forms.ValidationError(
- _("Component not found: {device} - {component_name}").format(
- device=device, component_name=component_name
- )
- )
- else:
- cleaned_data.pop('component_type', None)
- if not component_name:
- raise forms.ValidationError(
- _("Component name must be specified when component type is specified")
- )
- else:
- if component_name:
- raise forms.ValidationError(
- _("Component type must be specified when component name is specified")
- )
- return cleaned_data
- #
- # Device component roles
- #
- class InventoryItemRoleImportForm(OrganizationalModelImportForm):
- slug = SlugField()
- class Meta:
- model = InventoryItemRole
- fields = ('name', 'slug', 'color', 'description', 'owner', 'comments')
- #
- # Addressing
- #
- class MACAddressImportForm(PrimaryModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Parent device of assigned interface (if any)')
- )
- virtual_machine = CSVModelChoiceField(
- label=_('Virtual machine'),
- queryset=VirtualMachine.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Parent VM of assigned interface (if any)')
- )
- interface = CSVModelChoiceField(
- label=_('Interface'),
- queryset=Interface.objects.none(), # Can also refer to VMInterface
- required=False,
- to_field_name='name',
- help_text=_('Assigned interface')
- )
- is_primary = forms.BooleanField(
- label=_('Is primary'),
- help_text=_('Make this the primary MAC address for the assigned interface'),
- required=False
- )
- class Meta:
- model = MACAddress
- fields = [
- 'mac_address', 'device', 'virtual_machine', 'interface', 'is_primary', 'description', 'owner', 'comments',
- 'tags',
- ]
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit interface queryset by assigned device
- if data.get('device'):
- self.fields['interface'].queryset = Interface.objects.filter(
- **self.get_related_filter('device', 'device', data['device'])
- )
- # Limit interface queryset by assigned device
- elif data.get('virtual_machine'):
- self.fields['interface'].queryset = VMInterface.objects.filter(
- **self.get_related_filter('virtual_machine', 'virtual_machine', data['virtual_machine'])
- )
- def clean(self):
- super().clean()
- device = self.cleaned_data.get('device')
- virtual_machine = self.cleaned_data.get('virtual_machine')
- interface = self.cleaned_data.get('interface')
- # Validate interface assignment
- if interface and not device and not virtual_machine:
- raise forms.ValidationError({
- "interface": _("Must specify the parent device or VM when assigning an interface")
- })
- def save(self, *args, **kwargs):
- # Set interface assignment
- if interface := self.cleaned_data.get('interface'):
- self.instance.assigned_object = interface
- instance = super().save(*args, **kwargs)
- # Assign the MAC address as primary for its interface, if designated as such
- if interface and self.cleaned_data['is_primary'] and self.instance.pk:
- interface.snapshot()
- interface.primary_mac_address = self.instance
- interface.save()
- return instance
- class MACBlockImportForm(PrimaryModelImportForm):
- class Meta:
- model = MACBlock
- fields = [
- 'prefix', 'prefix_length', 'name', 'enforce_unique', 'description', 'owner', 'comments', 'tags',
- ]
- #
- # Cables
- #
- class CableBundleImportForm(PrimaryModelImportForm):
- class Meta:
- model = CableBundle
- fields = ('name', 'description', 'owner', 'comments', 'tags')
- class CableImportForm(PrimaryModelImportForm):
- # Cable.clean() reports termination errors (e.g. cable profile violations) against the model's
- # a_terminations/b_terminations attributes, which have no corresponding fields on this form.
- # Map them onto the columns which define each side's terminations.
- TERMINATION_ERROR_FIELDS = {
- 'a_terminations': 'side_a_name',
- 'b_terminations': 'side_b_name',
- }
- # Columns which take effect only by resolving a side's terminations, and are therefore
- # meaningless without that side's name column.
- TERMINATION_DEPENDENT_COLUMNS = ('site', 'device', 'power_panel', 'type')
- # Termination A
- side_a_site = CSVModelChoiceField(
- label=_('Side A site'),
- queryset=Site.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Site of parent device A (if any). Restricts the devices & power panels which may be matched.'),
- )
- side_a_device = CSVModelMultipleChoiceField(
- label=_('Side A device'),
- queryset=Device.objects.all(),
- required=False,
- to_field_name='name',
- help_text=format_html_lazy(
- '{} <code>{}</code>',
- _('Device name(s) for device component terminations. Separate multiple values with commas, '
- 'encased with double quotes. Example:'),
- '"device1,device2"'
- )
- )
- side_a_power_panel = CSVModelMultipleChoiceField(
- label=_('Side A power panel'),
- queryset=PowerPanel.objects.all(),
- required=False,
- to_field_name='name',
- help_text=format_html_lazy(
- '{} <code>{}</code>',
- _('Power panel name(s) for power feed terminations. Separate multiple values with commas, '
- 'encased with double quotes. Example:'),
- '"panel1,panel2"'
- )
- )
- side_a_type = CSVContentTypeField(
- label=_('Side A type'),
- queryset=ContentType.objects.all(),
- limit_choices_to=CABLE_TERMINATION_MODELS,
- help_text=_('Termination type')
- )
- side_a_name = forms.CharField(
- label=_('Side A name'),
- help_text=format_html_lazy(
- '{} <code>{}</code>',
- _('Termination name(s). Separate multiple values with commas, encased with double quotes. '
- 'Example:'),
- '"eth0,eth1"'
- )
- )
- # Termination B
- side_b_site = CSVModelChoiceField(
- label=_('Side B site'),
- queryset=Site.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Site of parent device B (if any). Restricts the devices & power panels which may be matched.'),
- )
- side_b_device = CSVModelMultipleChoiceField(
- label=_('Side B device'),
- queryset=Device.objects.all(),
- required=False,
- to_field_name='name',
- help_text=format_html_lazy(
- '{} <code>{}</code>',
- _('Device name(s) for device component terminations. Separate multiple values with commas, '
- 'encased with double quotes. Example:'),
- '"device1,device2"'
- )
- )
- side_b_power_panel = CSVModelMultipleChoiceField(
- label=_('Side B power panel'),
- queryset=PowerPanel.objects.all(),
- required=False,
- to_field_name='name',
- help_text=format_html_lazy(
- '{} <code>{}</code>',
- _('Power panel name(s) for power feed terminations. Separate multiple values with commas, '
- 'encased with double quotes. Example:'),
- '"panel1,panel2"'
- )
- )
- side_b_type = CSVContentTypeField(
- label=_('Side B type'),
- queryset=ContentType.objects.all(),
- limit_choices_to=CABLE_TERMINATION_MODELS,
- help_text=_('Termination type')
- )
- side_b_name = forms.CharField(
- label=_('Side B name'),
- help_text=format_html_lazy(
- '{} <code>{}</code>',
- _('Termination name(s). Separate multiple values with commas, encased with double quotes. '
- 'Example:'),
- '"eth0,eth1"'
- )
- )
- # Cable attributes
- status = CSVChoiceField(
- label=_('Status'),
- choices=LinkStatusChoices,
- required=False,
- help_text=_('Connection status')
- )
- profile = CSVChoiceField(
- label=_('Profile'),
- choices=CableProfileChoices,
- required=False,
- help_text=_('Cable connection profile')
- )
- type = CSVChoiceField(
- label=_('Type'),
- choices=CableTypeChoices,
- required=False,
- help_text=_('Physical medium classification')
- )
- tenant = CSVModelChoiceField(
- label=_('Tenant'),
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Assigned tenant')
- )
- bundle = CSVModelChoiceField(
- label=_('Bundle'),
- queryset=CableBundle.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Cable bundle name'),
- )
- length_unit = CSVChoiceField(
- label=_('Length unit'),
- choices=CableLengthUnitChoices,
- required=False,
- help_text=_('Length unit')
- )
- color = forms.CharField(
- label=_('Color'),
- required=False,
- max_length=16,
- help_text=_('Color name (e.g. "Red") or hex code (e.g. "f44336")')
- )
- class Meta:
- model = Cable
- fields = [
- 'side_a_site', 'side_a_device', 'side_a_power_panel', 'side_a_type', 'side_a_name',
- 'side_b_site', 'side_b_device', 'side_b_power_panel', 'side_b_type', 'side_b_name',
- 'type', 'status', 'profile', 'tenant', 'bundle', 'label', 'color', 'length', 'length_unit',
- 'description', 'owner', 'comments', 'tags',
- ]
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit choices for side_a_device to the assigned side_a_site
- if side_a_site := data.get('side_a_site'):
- side_a_parent_params = self.get_related_filter('site', 'side_a_site', side_a_site)
- self.fields['side_a_device'].queryset = self.fields['side_a_device'].queryset.filter(
- **side_a_parent_params
- )
- self.fields['side_a_power_panel'].queryset = self.fields['side_a_power_panel'].queryset.filter(
- **side_a_parent_params
- )
- # Limit choices for side_b_device to the assigned side_b_site
- if side_b_site := data.get('side_b_site'):
- side_b_parent_params = self.get_related_filter('site', 'side_b_site', side_b_site)
- self.fields['side_b_device'].queryset = self.fields['side_b_device'].queryset.filter(
- **side_b_parent_params
- )
- self.fields['side_b_power_panel'].queryset = self.fields['side_b_power_panel'].queryset.filter(
- **side_b_parent_params
- )
- def _update_errors(self, errors):
- # Remap any termination errors raised by Cable.clean() onto the relevant import column.
- # Without this, the base form reports them as prefixed non-field errors, as neither
- # a_terminations nor b_terminations exists as a field on this form. Errors are dispatched
- # per key so that both sides can fall back to a non-field error without colliding.
- if hasattr(errors, 'error_dict'):
- remapped = {}
- for name, error_list in errors.error_dict.items():
- mapped_name = self._map_termination_field(name) or NON_FIELD_ERRORS
- remapped.setdefault(mapped_name, []).extend(error_list)
- errors = forms.ValidationError(remapped)
- super()._update_errors(errors)
- def _map_termination_field(self, field):
- """
- Return the import column against which a model-level termination error should be reported,
- or None (i.e. a non-field error) if that column is not present on the form. Columns absent
- from an update record are removed from the form by BulkImportView, so a cable profile
- violation can be reported against a side whose terminations were not being modified.
- """
- if field not in self.TERMINATION_ERROR_FIELDS:
- return field
- mapped_field = self.TERMINATION_ERROR_FIELDS[field]
- return mapped_field if mapped_field in self.fields else None
- @staticmethod
- def _split_side_values(value):
- """
- Split a side_* cell into an ordered list of values, preserving duplicates and empty
- entries. Accepts a comma-separated string (CSV) or a native list (JSON/YAML).
- """
- if value in (None, ''):
- return []
- if not isinstance(value, (list, tuple)):
- value = str(value).split(',')
- return ['' if item is None else str(item).strip() for item in value]
- def _check_companion_column(self, side, field_name):
- """
- Verify that a column needed to resolve a side's terminations is present on the form.
- When updating an existing object, BulkImportView removes every field which does not appear
- in the record. A record which redefines a side's termination names must therefore also
- include the columns identifying their type and parent, otherwise the names cannot be
- resolved and the update would appear to succeed while changing nothing.
- """
- if field_name not in self.fields:
- raise forms.ValidationError(
- _(
- "Side {side_upper}: The {column} column must be included when modifying terminations"
- ).format(side_upper=side.upper(), column=field_name)
- )
- def _resolve_side_parent_objects(self, field_name):
- """
- Resolve a side's parent objects from the raw submitted values, preserving their order.
- CSVModelMultipleChoiceField cleans to an unordered queryset, which cannot be used to pair
- each parent with its corresponding termination name by position. Resolution errors are
- reported on the parent field itself.
- """
- if field_name not in self.cleaned_data:
- # The parent field has already raised its own validation error
- return None
- field = self.fields[field_name]
- parents = []
- for value in self._split_side_values(self.data.get(field_name)):
- try:
- parents.append(field.filter_by_value(value).get())
- except ObjectDoesNotExist:
- self.add_error(field_name, _("Object not found: {value}").format(value=value))
- return None
- except MultipleObjectsReturned:
- self.add_error(
- field_name,
- _('"{value}" is not a unique value for this field; multiple objects were found').format(
- value=value
- )
- )
- return None
- return parents
- @staticmethod
- def _get_device_component_termination(model, device, name):
- """
- Resolve a device component by its device and name. If the device is a virtual chassis
- master and the component is not found on it, search all virtual chassis members.
- """
- queryset = model.objects.filter(device=device, name=name)
- if (
- device.virtual_chassis and
- device.virtual_chassis.master == device and
- not queryset.exists()
- ):
- queryset = model.objects.filter(device__in=device.virtual_chassis.members.all(), name=name)
- return queryset.get()
- def _clean_side(self, side):
- """
- Derive a Cable's A/B termination objects.
- :param side: 'a' or 'b'
- """
- if side not in ('a', 'b'):
- raise ValueError(_("Invalid side designation: {side}").format(side=side))
- content_type = self.cleaned_data.get(f'side_{side}_type')
- # Native list values (JSON/YAML) bypass the CharField; strings use its cleaned value
- names = self.data.get(f'side_{side}_name')
- if not isinstance(names, (list, tuple)):
- names = self.cleaned_data.get(f'side_{side}_name')
- names = self._split_side_values(names)
- if not names:
- return None
- if not content_type:
- # BulkImportView removes any field absent from an update record, so a missing termination
- # type here means the column was omitted rather than left blank. Reject it: silently
- # ignoring the submitted names would report a successful update which changed nothing.
- self._check_companion_column(side, f'side_{side}_type')
- return None
- if '' in names:
- raise forms.ValidationError(
- _("Side {side_upper}: Empty termination names are not permitted").format(side_upper=side.upper())
- )
- model = content_type.model_class()
- # Identify the parent field for the termination type. PowerFeed terminations reference a
- # PowerPanel; all other supported types reference a Device.
- if content_type.model == 'powerfeed':
- parent_field_name = f'side_{side}_power_panel'
- parent_label = _('power panel')
- elif any(field.name == 'device' for field in model._meta.fields):
- parent_field_name = f'side_{side}_device'
- parent_label = _('device')
- else:
- raise forms.ValidationError(
- _("Bulk import does not support {type} terminations").format(type=content_type)
- )
- self._check_companion_column(side, parent_field_name)
- parents = self._resolve_side_parent_objects(parent_field_name)
- if parents is None:
- # The parent field has already raised its own validation error
- return None
- if not parents:
- raise forms.ValidationError(
- _("Side {side_upper}: Must specify a {parent} for the selected termination type").format(
- side_upper=side.upper(), parent=parent_label
- )
- )
- if len(parents) == 1:
- parents = parents * len(names)
- elif len(parents) != len(names):
- raise forms.ValidationError(
- _(
- "Side {side_upper}: Must specify either one {parent} for all terminations or one {parent} "
- "per termination name"
- ).format(side_upper=side.upper(), parent=parent_label)
- )
- terminations = []
- for parent, name in zip(parents, names):
- try:
- if content_type.model == 'powerfeed':
- termination_object = model.objects.get(power_panel=parent, name=name)
- else:
- termination_object = self._get_device_component_termination(model, parent, name)
- except ObjectDoesNotExist:
- raise forms.ValidationError(
- _("{side_upper} side termination not found: {parent} {name}").format(
- side_upper=side.upper(), parent=parent, name=name
- )
- )
- except MultipleObjectsReturned:
- raise forms.ValidationError(
- _("{side_upper} side termination not unique: {parent} {name}").format(
- side_upper=side.upper(), parent=parent, name=name
- )
- )
- if termination_object.cable is not None and termination_object.cable != self.instance:
- raise forms.ValidationError(
- _("Side {side_upper}: {parent} {termination_object} is already connected").format(
- side_upper=side.upper(), parent=parent, termination_object=termination_object
- )
- )
- terminations.append(termination_object)
- if len({termination.pk for termination in terminations}) != len(terminations):
- raise forms.ValidationError(
- _("Side {side_upper}: Duplicate termination specified").format(side_upper=side.upper())
- )
- setattr(self.instance, f'{side}_terminations', terminations)
- return terminations
- def _clean_color(self, color):
- """
- Derive a colors hex code
- :param color: color as hex or color name
- """
- color_parsed = color.strip().lower()
- for hex_code, label in ColorChoices.CHOICES:
- if color.lower() == label.lower():
- color_parsed = hex_code
- if len(color_parsed) > 6:
- raise forms.ValidationError(
- _(f"{color} did not match any used color name and was longer than six characters: invalid hex.")
- )
- return color_parsed
- def clean(self):
- cleaned_data = super().clean()
- # Termination resolution is driven by clean_side_<x>_name(), which Django never calls for a
- # field BulkImportView has removed. An update record which supplies a side's supporting
- # columns but omits its name column would therefore have those columns silently discarded
- # and report an update which changed nothing; reject it instead. This cannot trigger on
- # creation, where the name columns are always present (and required).
- for side in ('a', 'b'):
- if f'side_{side}_name' in self.fields:
- continue
- if supplied := [
- column for column in self.TERMINATION_DEPENDENT_COLUMNS
- if f'side_{side}_{column}' in self.fields
- ]:
- self.add_error(None, _(
- "Side {side_upper}: The side_{side}_name column must be included when modifying "
- "terminations (found {columns})"
- ).format(
- side_upper=side.upper(),
- side=side,
- columns=', '.join(f'side_{side}_{column}' for column in supplied),
- ))
- return cleaned_data
- def clean_side_a_name(self):
- return self._clean_side('a')
- def clean_side_b_name(self):
- return self._clean_side('b')
- def clean_length_unit(self):
- # Avoid trying to save as NULL
- length_unit = self.cleaned_data.get('length_unit', None)
- return length_unit if length_unit is not None else ''
- def clean_color(self):
- color = self.cleaned_data.get('color', None)
- return self._clean_color(color) if color is not None else ''
- #
- # Virtual chassis
- #
- class VirtualChassisImportForm(PrimaryModelImportForm):
- master = CSVModelChoiceField(
- label=_('Master'),
- queryset=Device.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Master device')
- )
- class Meta:
- model = VirtualChassis
- fields = ('name', 'domain', 'master', 'description', 'owner', 'comments', 'tags')
- #
- # Power
- #
- class PowerPanelImportForm(PrimaryModelImportForm):
- site = CSVModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text=_('Name of parent site')
- )
- location = CSVModelChoiceField(
- label=_('Location'),
- queryset=Location.objects.all(),
- required=False,
- to_field_name='name'
- )
- class Meta:
- model = PowerPanel
- fields = ('site', 'location', 'name', 'description', 'owner', 'comments', 'tags')
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit group queryset by assigned site
- params = self.get_related_filter('site', 'site', data.get('site'))
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- class PowerFeedImportForm(PrimaryModelImportForm):
- site = CSVModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text=_('Assigned site')
- )
- power_panel = CSVModelChoiceField(
- label=_('Power panel'),
- queryset=PowerPanel.objects.all(),
- to_field_name='name',
- help_text=_('Upstream power panel')
- )
- location = CSVModelChoiceField(
- label=_('Location'),
- queryset=Location.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_("Rack's location (if any)")
- )
- rack = CSVModelChoiceField(
- label=_('Rack'),
- queryset=Rack.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Rack')
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Assigned tenant')
- )
- status = CSVChoiceField(
- label=_('Status'),
- choices=PowerFeedStatusChoices,
- help_text=_('Operational status')
- )
- type = CSVChoiceField(
- label=_('Type'),
- choices=PowerFeedTypeChoices,
- help_text=_('Primary or redundant')
- )
- supply = CSVChoiceField(
- label=_('Supply'),
- choices=PowerFeedSupplyChoices,
- help_text=_('Supply type (AC/DC)')
- )
- phase = CSVChoiceField(
- label=_('Phase'),
- choices=PowerFeedPhaseChoices,
- help_text=_('Single or three-phase')
- )
- class Meta:
- model = PowerFeed
- fields = (
- 'site', 'power_panel', 'location', 'rack', 'name', 'status', 'type', 'mark_connected', 'supply', 'phase',
- 'voltage', 'amperage', 'max_utilization', 'tenant', 'description', 'owner', 'comments', 'tags',
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit power_panel queryset by site
- params = self.get_related_filter('site', 'site', data.get('site'))
- self.fields['power_panel'].queryset = self.fields['power_panel'].queryset.filter(**params)
- # Limit location queryset by site
- params = self.get_related_filter('site', 'site', data.get('site'))
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- # Limit rack queryset by site and group
- params = {
- **self.get_related_filter('site', 'site', data.get('site')),
- **self.get_related_filter('location', 'location', data.get('location')),
- }
- self.fields['rack'].queryset = self.fields['rack'].queryset.filter(**params)
- class CoolingSourceImportForm(PrimaryModelImportForm):
- site = CSVModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text=_('Name of parent site')
- )
- location = CSVModelChoiceField(
- label=_('Location'),
- queryset=Location.objects.all(),
- required=False,
- to_field_name='name'
- )
- type = CSVChoiceField(
- label=_('Type'),
- choices=CoolingSourceTypeChoices,
- help_text=_('Cooling source type')
- )
- status = CSVChoiceField(
- label=_('Status'),
- choices=CoolingSourceStatusChoices,
- help_text=_('Operational status')
- )
- fluid_type = CSVChoiceField(
- label=_('Fluid type'),
- choices=FluidTypeChoices,
- required=False,
- help_text=_('Coolant fluid type')
- )
- class Meta:
- model = CoolingSource
- fields = (
- 'site', 'location', 'name', 'type', 'status', 'fluid_type', 'cooling_capacity', 'description', 'owner',
- 'comments', 'tags',
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit location queryset by assigned site
- params = self.get_related_filter('site', 'site', data.get('site'))
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- class CoolingFeedImportForm(PrimaryModelImportForm):
- site = CSVModelChoiceField(
- label=_('Site'),
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text=_('Assigned site')
- )
- cooling_source = CSVModelChoiceField(
- label=_('Cooling source'),
- queryset=CoolingSource.objects.all(),
- to_field_name='name',
- help_text=_('Upstream cooling source')
- )
- location = CSVModelChoiceField(
- label=_('Location'),
- queryset=Location.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_("Rack's location (if any)")
- )
- rack = CSVModelChoiceField(
- label=_('Rack'),
- queryset=Rack.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Rack')
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- to_field_name='name',
- required=False,
- help_text=_('Assigned tenant')
- )
- status = CSVChoiceField(
- label=_('Status'),
- choices=CoolingFeedStatusChoices,
- help_text=_('Operational status')
- )
- max_flow_unit = CSVChoiceField(
- label=_('Max flow unit'),
- choices=FlowRateUnitChoices,
- required=False,
- help_text=_('Unit for maximum flow')
- )
- class Meta:
- model = CoolingFeed
- fields = (
- 'site', 'cooling_source', 'location', 'rack', 'name', 'status',
- 'cooling_capacity', 'max_flow', 'max_flow_unit', 'tenant', 'description', 'owner',
- 'comments', 'tags',
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit cooling_source queryset by site
- params = self.get_related_filter('site', 'site', data.get('site'))
- self.fields['cooling_source'].queryset = self.fields['cooling_source'].queryset.filter(**params)
- # Limit location queryset by site
- params = self.get_related_filter('site', 'site', data.get('site'))
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- # Limit rack queryset by site and group
- params = {
- **self.get_related_filter('site', 'site', data.get('site')),
- **self.get_related_filter('location', 'location', data.get('location')),
- }
- self.fields['rack'].queryset = self.fields['rack'].queryset.filter(**params)
- class VirtualDeviceContextImportForm(PrimaryModelImportForm):
- device = CSVModelChoiceField(
- label=_('Device'),
- queryset=Device.objects.all(),
- to_field_name='name',
- help_text=_('Assigned role')
- )
- tenant = CSVModelChoiceField(
- label=_('Tenant'),
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text=_('Assigned tenant')
- )
- status = CSVChoiceField(
- label=_('Status'),
- choices=VirtualDeviceContextStatusChoices,
- )
- primary_ip4 = CSVModelChoiceField(
- label=_('Primary IPv4'),
- queryset=IPAddress.objects.all(),
- required=False,
- to_field_name='address',
- help_text=_('IPv4 address with mask, e.g. 1.2.3.4/24')
- )
- primary_ip6 = CSVModelChoiceField(
- label=_('Primary IPv6'),
- queryset=IPAddress.objects.all(),
- required=False,
- to_field_name='address',
- help_text=_('IPv6 address with prefix length, e.g. 2001:db8::1/64')
- )
- class Meta:
- fields = [
- 'name', 'device', 'status', 'tenant', 'identifier', 'owner', 'comments', 'primary_ip4', 'primary_ip6',
- ]
- model = VirtualDeviceContext
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit primary_ip4/ip6 querysets by assigned device
- params = self.get_related_filter('interface__device', 'device', data.get('device'))
- self.fields['primary_ip4'].queryset = self.fields['primary_ip4'].queryset.filter(**params)
- self.fields['primary_ip6'].queryset = self.fields['primary_ip6'].queryset.filter(**params)
|