| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085 |
- from django import forms
- from django.contrib.contenttypes.models import ContentType
- from django.contrib.postgres.forms.array import SimpleArrayField
- from django.core.exceptions import ObjectDoesNotExist
- from django.utils.safestring import mark_safe
- from dcim.choices import *
- from dcim.constants import *
- from dcim.models import *
- from ipam.models import VRF
- from netbox.forms import NetBoxModelCSVForm
- from tenancy.models import Tenant
- from utilities.forms import CSVChoiceField, CSVContentTypeField, CSVModelChoiceField, CSVTypedChoiceField, SlugField
- from virtualization.models import Cluster
- from wireless.choices import WirelessRoleChoices
- __all__ = (
- 'CableCSVForm',
- 'ChildDeviceCSVForm',
- 'ConsolePortCSVForm',
- 'ConsoleServerPortCSVForm',
- 'DeviceBayCSVForm',
- 'DeviceCSVForm',
- 'DeviceRoleCSVForm',
- 'FrontPortCSVForm',
- 'InterfaceCSVForm',
- 'InventoryItemCSVForm',
- 'InventoryItemRoleCSVForm',
- 'LocationCSVForm',
- 'ManufacturerCSVForm',
- 'ModuleCSVForm',
- 'ModuleBayCSVForm',
- 'PlatformCSVForm',
- 'PowerFeedCSVForm',
- 'PowerOutletCSVForm',
- 'PowerPanelCSVForm',
- 'PowerPortCSVForm',
- 'RackCSVForm',
- 'RackReservationCSVForm',
- 'RackRoleCSVForm',
- 'RearPortCSVForm',
- 'RegionCSVForm',
- 'SiteCSVForm',
- 'SiteGroupCSVForm',
- 'VirtualChassisCSVForm',
- )
- class RegionCSVForm(NetBoxModelCSVForm):
- parent = CSVModelChoiceField(
- 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')
- class SiteGroupCSVForm(NetBoxModelCSVForm):
- parent = CSVModelChoiceField(
- 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')
- class SiteCSVForm(NetBoxModelCSVForm):
- status = CSVChoiceField(
- choices=SiteStatusChoices,
- help_text='Operational status'
- )
- region = CSVModelChoiceField(
- queryset=Region.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned region'
- )
- group = CSVModelChoiceField(
- queryset=SiteGroup.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned group'
- )
- tenant = CSVModelChoiceField(
- 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', 'comments',
- )
- help_texts = {
- 'time_zone': mark_safe(
- 'Time zone (<a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones">available options</a>)'
- )
- }
- class LocationCSVForm(NetBoxModelCSVForm):
- site = CSVModelChoiceField(
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text='Assigned site'
- )
- parent = CSVModelChoiceField(
- queryset=Location.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Parent location',
- error_messages={
- 'invalid_choice': 'Location not found.',
- }
- )
- status = CSVChoiceField(
- choices=LocationStatusChoices,
- help_text='Operational status'
- )
- tenant = CSVModelChoiceField(
- 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', 'description')
- class RackRoleCSVForm(NetBoxModelCSVForm):
- slug = SlugField()
- class Meta:
- model = RackRole
- fields = ('name', 'slug', 'color', 'description')
- help_texts = {
- 'color': mark_safe('RGB color in hexadecimal (e.g. <code>00ff00</code>)'),
- }
- class RackCSVForm(NetBoxModelCSVForm):
- site = CSVModelChoiceField(
- queryset=Site.objects.all(),
- to_field_name='name'
- )
- location = CSVModelChoiceField(
- queryset=Location.objects.all(),
- required=False,
- to_field_name='name'
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Name of assigned tenant'
- )
- status = CSVChoiceField(
- choices=RackStatusChoices,
- help_text='Operational status'
- )
- role = CSVModelChoiceField(
- queryset=RackRole.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Name of assigned role'
- )
- type = CSVChoiceField(
- choices=RackTypeChoices,
- required=False,
- help_text='Rack type'
- )
- width = forms.ChoiceField(
- choices=RackWidthChoices,
- help_text='Rail-to-rail width (in inches)'
- )
- outer_unit = CSVChoiceField(
- choices=RackDimensionUnitChoices,
- required=False,
- help_text='Unit for outer dimensions'
- )
- class Meta:
- model = Rack
- fields = (
- 'site', 'location', 'name', 'facility_id', 'tenant', 'status', 'role', 'type', 'serial', 'asset_tag',
- 'width', 'u_height', 'desc_units', 'outer_width', 'outer_depth', 'outer_unit', 'mounting_depth', 'comments',
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit location queryset by assigned site
- params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- class RackReservationCSVForm(NetBoxModelCSVForm):
- site = CSVModelChoiceField(
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text='Parent site'
- )
- location = CSVModelChoiceField(
- queryset=Location.objects.all(),
- to_field_name='name',
- required=False,
- help_text="Rack's location (if any)"
- )
- rack = CSVModelChoiceField(
- queryset=Rack.objects.all(),
- to_field_name='name',
- help_text='Rack'
- )
- units = SimpleArrayField(
- base_field=forms.IntegerField(),
- required=True,
- help_text='Comma-separated list of individual unit numbers'
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned tenant'
- )
- class Meta:
- model = RackReservation
- fields = ('site', 'location', 'rack', 'units', 'tenant', 'description')
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit location queryset by assigned site
- params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- # Limit rack queryset by assigned site and group
- params = {
- f"site__{self.fields['site'].to_field_name}": data.get('site'),
- f"location__{self.fields['location'].to_field_name}": data.get('location'),
- }
- self.fields['rack'].queryset = self.fields['rack'].queryset.filter(**params)
- class ManufacturerCSVForm(NetBoxModelCSVForm):
- class Meta:
- model = Manufacturer
- fields = ('name', 'slug', 'description')
- class DeviceRoleCSVForm(NetBoxModelCSVForm):
- slug = SlugField()
- class Meta:
- model = DeviceRole
- fields = ('name', 'slug', 'color', 'vm_role', 'description')
- help_texts = {
- 'color': mark_safe('RGB color in hexadecimal (e.g. <code>00ff00</code>)'),
- }
- class PlatformCSVForm(NetBoxModelCSVForm):
- slug = SlugField()
- manufacturer = CSVModelChoiceField(
- queryset=Manufacturer.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Limit platform assignments to this manufacturer'
- )
- class Meta:
- model = Platform
- fields = ('name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description')
- class BaseDeviceCSVForm(NetBoxModelCSVForm):
- device_role = CSVModelChoiceField(
- queryset=DeviceRole.objects.all(),
- to_field_name='name',
- help_text='Assigned role'
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned tenant'
- )
- manufacturer = CSVModelChoiceField(
- queryset=Manufacturer.objects.all(),
- to_field_name='name',
- help_text='Device type manufacturer'
- )
- device_type = CSVModelChoiceField(
- queryset=DeviceType.objects.all(),
- to_field_name='model',
- help_text='Device type model'
- )
- platform = CSVModelChoiceField(
- queryset=Platform.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned platform'
- )
- status = CSVChoiceField(
- choices=DeviceStatusChoices,
- help_text='Operational status'
- )
- virtual_chassis = CSVModelChoiceField(
- queryset=VirtualChassis.objects.all(),
- to_field_name='name',
- required=False,
- help_text='Virtual chassis'
- )
- cluster = CSVModelChoiceField(
- queryset=Cluster.objects.all(),
- to_field_name='name',
- required=False,
- help_text='Virtualization cluster'
- )
- class Meta:
- fields = []
- model = Device
- help_texts = {
- 'vc_position': 'Virtual chassis position',
- 'vc_priority': 'Virtual chassis priority',
- }
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit device type queryset by manufacturer
- params = {f"manufacturer__{self.fields['manufacturer'].to_field_name}": data.get('manufacturer')}
- self.fields['device_type'].queryset = self.fields['device_type'].queryset.filter(**params)
- class DeviceCSVForm(BaseDeviceCSVForm):
- site = CSVModelChoiceField(
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text='Assigned site'
- )
- location = CSVModelChoiceField(
- queryset=Location.objects.all(),
- to_field_name='name',
- required=False,
- help_text="Assigned location (if any)"
- )
- rack = CSVModelChoiceField(
- queryset=Rack.objects.all(),
- to_field_name='name',
- required=False,
- help_text="Assigned rack (if any)"
- )
- face = CSVChoiceField(
- choices=DeviceFaceChoices,
- required=False,
- help_text='Mounted rack face'
- )
- airflow = CSVChoiceField(
- choices=DeviceAirflowChoices,
- required=False,
- help_text='Airflow direction'
- )
- class Meta(BaseDeviceCSVForm.Meta):
- fields = [
- 'name', 'device_role', 'tenant', 'manufacturer', 'device_type', 'platform', 'serial', 'asset_tag', 'status',
- 'site', 'location', 'rack', 'position', 'face', 'airflow', 'virtual_chassis', 'vc_position', 'vc_priority',
- 'cluster', 'comments',
- ]
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit location queryset by assigned site
- params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- # Limit rack queryset by assigned site and group
- params = {
- f"site__{self.fields['site'].to_field_name}": data.get('site'),
- f"location__{self.fields['location'].to_field_name}": data.get('location'),
- }
- self.fields['rack'].queryset = self.fields['rack'].queryset.filter(**params)
- class ModuleCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- module_bay = CSVModelChoiceField(
- queryset=ModuleBay.objects.all(),
- to_field_name='name'
- )
- module_type = CSVModelChoiceField(
- queryset=ModuleType.objects.all(),
- to_field_name='model'
- )
- class Meta:
- model = Module
- fields = (
- 'device', 'module_bay', 'module_type', 'serial', 'asset_tag', 'comments',
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit module_bay queryset by assigned device
- params = {f"device__{self.fields['device'].to_field_name}": data.get('device')}
- self.fields['module_bay'].queryset = self.fields['module_bay'].queryset.filter(**params)
- class ChildDeviceCSVForm(BaseDeviceCSVForm):
- parent = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name',
- help_text='Parent device'
- )
- device_bay = CSVModelChoiceField(
- queryset=DeviceBay.objects.all(),
- to_field_name='name',
- help_text='Device bay in which this device is installed'
- )
- class Meta(BaseDeviceCSVForm.Meta):
- fields = [
- 'name', 'device_role', 'tenant', 'manufacturer', 'device_type', 'platform', 'serial', 'asset_tag', 'status',
- 'parent', 'device_bay', 'virtual_chassis', 'vc_position', 'vc_priority', 'cluster', 'comments',
- ]
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit device bay queryset by parent device
- params = {f"device__{self.fields['parent'].to_field_name}": data.get('parent')}
- self.fields['device_bay'].queryset = self.fields['device_bay'].queryset.filter(**params)
- def clean(self):
- super().clean()
- # Set parent_bay reverse relationship
- device_bay = self.cleaned_data.get('device_bay')
- if device_bay:
- self.instance.parent_bay = device_bay
- # Inherit site and rack from parent device
- parent = self.cleaned_data.get('parent')
- if parent:
- self.instance.site = parent.site
- self.instance.rack = parent.rack
- #
- # Device components
- #
- class ConsolePortCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- choices=ConsolePortTypeChoices,
- required=False,
- help_text='Port type'
- )
- speed = CSVTypedChoiceField(
- 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')
- class ConsoleServerPortCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- choices=ConsolePortTypeChoices,
- required=False,
- help_text='Port type'
- )
- speed = CSVTypedChoiceField(
- 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')
- class PowerPortCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- choices=PowerPortTypeChoices,
- required=False,
- help_text='Port type'
- )
- class Meta:
- model = PowerPort
- fields = (
- 'device', 'name', 'label', 'type', 'mark_connected', 'maximum_draw', 'allocated_draw', 'description',
- )
- class PowerOutletCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- choices=PowerOutletTypeChoices,
- required=False,
- help_text='Outlet type'
- )
- power_port = CSVModelChoiceField(
- queryset=PowerPort.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Local power port which feeds this outlet'
- )
- feed_leg = CSVChoiceField(
- choices=PowerOutletFeedLegChoices,
- required=False,
- help_text='Electrical phase (for three-phase circuits)'
- )
- class Meta:
- model = PowerOutlet
- fields = ('device', 'name', 'label', 'type', 'mark_connected', 'power_port', 'feed_leg', 'description')
- 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 InterfaceCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- parent = CSVModelChoiceField(
- queryset=Interface.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Parent interface'
- )
- bridge = CSVModelChoiceField(
- queryset=Interface.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Bridged interface'
- )
- lag = CSVModelChoiceField(
- queryset=Interface.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Parent LAG interface'
- )
- type = CSVChoiceField(
- choices=InterfaceTypeChoices,
- help_text='Physical medium'
- )
- duplex = CSVChoiceField(
- choices=InterfaceDuplexChoices,
- required=False
- )
- poe_mode = CSVChoiceField(
- choices=InterfacePoEModeChoices,
- required=False,
- help_text='PoE mode'
- )
- poe_type = CSVChoiceField(
- choices=InterfacePoETypeChoices,
- required=False,
- help_text='PoE type'
- )
- mode = CSVChoiceField(
- choices=InterfaceModeChoices,
- required=False,
- help_text='IEEE 802.1Q operational mode (for L2 interfaces)'
- )
- vrf = CSVModelChoiceField(
- queryset=VRF.objects.all(),
- required=False,
- to_field_name='rd',
- help_text='Assigned VRF'
- )
- rf_role = CSVChoiceField(
- choices=WirelessRoleChoices,
- required=False,
- help_text='Wireless role (AP/station)'
- )
- class Meta:
- model = Interface
- fields = (
- 'device', 'name', 'label', 'parent', 'bridge', 'lag', 'type', 'speed', 'duplex', 'enabled',
- 'mark_connected', 'mac_address', 'wwn', 'mtu', 'mgmt_only', 'description', 'poe_mode', 'poe_type', 'mode',
- 'vrf', 'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'tx_power',
- )
- 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 = {
- f"device__{self.fields['device'].to_field_name}": 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)
- 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
- else:
- return self.cleaned_data['enabled']
- class FrontPortCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- rear_port = CSVModelChoiceField(
- queryset=RearPort.objects.all(),
- to_field_name='name',
- help_text='Corresponding rear port'
- )
- type = CSVChoiceField(
- choices=PortTypeChoices,
- help_text='Physical medium classification'
- )
- class Meta:
- model = FrontPort
- fields = (
- 'device', 'name', 'label', 'type', 'color', 'mark_connected', 'rear_port', 'rear_port_position',
- 'description',
- )
- help_texts = {
- 'rear_port_position': 'Mapped position on corresponding rear port',
- }
- 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()
- class RearPortCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- type = CSVChoiceField(
- help_text='Physical medium classification',
- choices=PortTypeChoices,
- )
- class Meta:
- model = RearPort
- fields = ('device', 'name', 'label', 'type', 'color', 'mark_connected', 'positions', 'description')
- help_texts = {
- 'positions': 'Number of front ports which may be mapped'
- }
- class ModuleBayCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- class Meta:
- model = ModuleBay
- fields = ('device', 'name', 'label', 'position', 'description')
- class DeviceBayCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- installed_device = CSVModelChoiceField(
- 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', 'installed_device', 'description')
- 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 = Interface.objects.none()
- class InventoryItemCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name'
- )
- role = CSVModelChoiceField(
- queryset=InventoryItemRole.objects.all(),
- to_field_name='name',
- required=False
- )
- manufacturer = CSVModelChoiceField(
- queryset=Manufacturer.objects.all(),
- to_field_name='name',
- required=False
- )
- parent = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name',
- required=False,
- help_text='Parent inventory item'
- )
- class Meta:
- model = InventoryItem
- fields = (
- 'device', 'name', 'label', 'role', 'manufacturer', 'part_id', 'serial', 'asset_tag', 'discovered',
- 'description',
- )
- 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()
- #
- # Device component roles
- #
- class InventoryItemRoleCSVForm(NetBoxModelCSVForm):
- slug = SlugField()
- class Meta:
- model = InventoryItemRole
- fields = ('name', 'slug', 'color', 'description')
- help_texts = {
- 'color': mark_safe('RGB color in hexadecimal (e.g. <code>00ff00</code>)'),
- }
- #
- # Cables
- #
- class CableCSVForm(NetBoxModelCSVForm):
- # Termination A
- side_a_device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name',
- help_text='Side A device'
- )
- side_a_type = CSVContentTypeField(
- queryset=ContentType.objects.all(),
- limit_choices_to=CABLE_TERMINATION_MODELS,
- help_text='Side A type'
- )
- side_a_name = forms.CharField(
- help_text='Side A component name'
- )
- # Termination B
- side_b_device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name',
- help_text='Side B device'
- )
- side_b_type = CSVContentTypeField(
- queryset=ContentType.objects.all(),
- limit_choices_to=CABLE_TERMINATION_MODELS,
- help_text='Side B type'
- )
- side_b_name = forms.CharField(
- help_text='Side B component name'
- )
- # Cable attributes
- status = CSVChoiceField(
- choices=LinkStatusChoices,
- required=False,
- help_text='Connection status'
- )
- type = CSVChoiceField(
- choices=CableTypeChoices,
- required=False,
- help_text='Physical medium classification'
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned tenant'
- )
- length_unit = CSVChoiceField(
- choices=CableLengthUnitChoices,
- required=False,
- help_text='Length unit'
- )
- class Meta:
- model = Cable
- fields = [
- 'side_a_device', 'side_a_type', 'side_a_name', 'side_b_device', 'side_b_type', 'side_b_name', 'type',
- 'status', 'tenant', 'label', 'color', 'length', 'length_unit',
- ]
- help_texts = {
- 'color': mark_safe('RGB color in hexadecimal (e.g. <code>00ff00</code>)'),
- }
- def _clean_side(self, side):
- """
- Derive a Cable's A/B termination objects.
- :param side: 'a' or 'b'
- """
- assert side in 'ab', f"Invalid side designation: {side}"
- device = self.cleaned_data.get(f'side_{side}_device')
- content_type = self.cleaned_data.get(f'side_{side}_type')
- name = self.cleaned_data.get(f'side_{side}_name')
- if not device or not content_type or not name:
- return None
- model = content_type.model_class()
- try:
- termination_object = model.objects.get(device=device, name=name)
- if termination_object.cable is not None:
- raise forms.ValidationError(f"Side {side.upper()}: {device} {termination_object} is already connected")
- except ObjectDoesNotExist:
- raise forms.ValidationError(f"{side.upper()} side termination not found: {device} {name}")
- setattr(self.instance, f'{side}_terminations', [termination_object])
- return termination_object
- 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 ''
- #
- # Virtual chassis
- #
- class VirtualChassisCSVForm(NetBoxModelCSVForm):
- master = CSVModelChoiceField(
- queryset=Device.objects.all(),
- to_field_name='name',
- required=False,
- help_text='Master device'
- )
- class Meta:
- model = VirtualChassis
- fields = ('name', 'domain', 'master')
- #
- # Power
- #
- class PowerPanelCSVForm(NetBoxModelCSVForm):
- site = CSVModelChoiceField(
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text='Name of parent site'
- )
- location = CSVModelChoiceField(
- queryset=Location.objects.all(),
- required=False,
- to_field_name='name'
- )
- class Meta:
- model = PowerPanel
- fields = ('site', 'location', 'name')
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit group queryset by assigned site
- params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- class PowerFeedCSVForm(NetBoxModelCSVForm):
- site = CSVModelChoiceField(
- queryset=Site.objects.all(),
- to_field_name='name',
- help_text='Assigned site'
- )
- power_panel = CSVModelChoiceField(
- queryset=PowerPanel.objects.all(),
- to_field_name='name',
- help_text='Upstream power panel'
- )
- location = CSVModelChoiceField(
- queryset=Location.objects.all(),
- to_field_name='name',
- required=False,
- help_text="Rack's location (if any)"
- )
- rack = CSVModelChoiceField(
- queryset=Rack.objects.all(),
- to_field_name='name',
- required=False,
- help_text='Rack'
- )
- status = CSVChoiceField(
- choices=PowerFeedStatusChoices,
- help_text='Operational status'
- )
- type = CSVChoiceField(
- choices=PowerFeedTypeChoices,
- help_text='Primary or redundant'
- )
- supply = CSVChoiceField(
- choices=PowerFeedSupplyChoices,
- help_text='Supply type (AC/DC)'
- )
- phase = CSVChoiceField(
- 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', 'comments',
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit power_panel queryset by site
- params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
- self.fields['power_panel'].queryset = self.fields['power_panel'].queryset.filter(**params)
- # Limit location queryset by site
- params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
- self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
- # Limit rack queryset by site and group
- params = {
- f"site__{self.fields['site'].to_field_name}": data.get('site'),
- f"location__{self.fields['location'].to_field_name}": data.get('location'),
- }
- self.fields['rack'].queryset = self.fields['rack'].queryset.filter(**params)
|