| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415 |
- from django import forms
- from django.contrib.auth.models import User
- from django.contrib.contenttypes.models import ContentType
- from django.utils.translation import gettext as _
- from timezone_field import TimeZoneFormField
- from dcim.choices import *
- from dcim.constants import *
- from dcim.models import *
- from extras.models import ConfigTemplate
- from ipam.models import ASN, IPAddress, VLAN, VLANGroup, VRF
- from netbox.forms import NetBoxModelForm
- from tenancy.forms import TenancyForm
- from utilities.forms import BootstrapMixin, add_blank_choice
- from utilities.forms.fields import (
- CommentField, ContentTypeChoiceField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, JSONField,
- NumericArrayField, SlugField,
- )
- from utilities.forms.widgets import APISelect, ClearableFileInput, HTMXSelect, NumberWithOptions, SelectWithPK
- from virtualization.models import Cluster
- from wireless.models import WirelessLAN, WirelessLANGroup
- from .common import InterfaceCommonForm, ModuleCommonForm
- __all__ = (
- 'CableForm',
- 'ConsolePortForm',
- 'ConsolePortTemplateForm',
- 'ConsoleServerPortForm',
- 'ConsoleServerPortTemplateForm',
- 'DeviceBayForm',
- 'DeviceBayTemplateForm',
- 'DeviceForm',
- 'DeviceRoleForm',
- 'DeviceTypeForm',
- 'DeviceVCMembershipForm',
- 'FrontPortForm',
- 'FrontPortTemplateForm',
- 'InterfaceForm',
- 'InterfaceTemplateForm',
- 'InventoryItemForm',
- 'InventoryItemRoleForm',
- 'InventoryItemTemplateForm',
- 'LocationForm',
- 'ManufacturerForm',
- 'ModuleForm',
- 'ModuleBayForm',
- 'ModuleBayTemplateForm',
- 'ModuleTypeForm',
- 'PlatformForm',
- 'PopulateDeviceBayForm',
- 'PowerFeedForm',
- 'PowerOutletForm',
- 'PowerOutletTemplateForm',
- 'PowerPanelForm',
- 'PowerPortForm',
- 'PowerPortTemplateForm',
- 'RackForm',
- 'RackReservationForm',
- 'RackRoleForm',
- 'RearPortForm',
- 'RearPortTemplateForm',
- 'RegionForm',
- 'SiteForm',
- 'SiteGroupForm',
- 'VCMemberSelectForm',
- 'VirtualChassisForm',
- 'VirtualDeviceContextForm'
- )
- class RegionForm(NetBoxModelForm):
- parent = DynamicModelChoiceField(
- queryset=Region.objects.all(),
- required=False
- )
- slug = SlugField()
- fieldsets = (
- ('Region', (
- 'parent', 'name', 'slug', 'description', 'tags',
- )),
- )
- class Meta:
- model = Region
- fields = (
- 'parent', 'name', 'slug', 'description', 'tags',
- )
- class SiteGroupForm(NetBoxModelForm):
- parent = DynamicModelChoiceField(
- queryset=SiteGroup.objects.all(),
- required=False
- )
- slug = SlugField()
- fieldsets = (
- ('Site Group', (
- 'parent', 'name', 'slug', 'description', 'tags',
- )),
- )
- class Meta:
- model = SiteGroup
- fields = (
- 'parent', 'name', 'slug', 'description', 'tags',
- )
- class SiteForm(TenancyForm, NetBoxModelForm):
- region = DynamicModelChoiceField(
- queryset=Region.objects.all(),
- required=False
- )
- group = DynamicModelChoiceField(
- queryset=SiteGroup.objects.all(),
- required=False
- )
- asns = DynamicModelMultipleChoiceField(
- queryset=ASN.objects.all(),
- label=_('ASNs'),
- required=False
- )
- slug = SlugField()
- time_zone = TimeZoneFormField(
- choices=add_blank_choice(TimeZoneFormField().choices),
- required=False
- )
- comments = CommentField()
- fieldsets = (
- ('Site', (
- 'name', 'slug', 'status', 'region', 'group', 'facility', 'asns', 'time_zone', 'description', 'tags',
- )),
- ('Tenancy', ('tenant_group', 'tenant')),
- ('Contact Info', ('physical_address', 'shipping_address', 'latitude', 'longitude')),
- )
- class Meta:
- model = Site
- fields = (
- 'name', 'slug', 'status', 'region', 'group', 'tenant_group', 'tenant', 'facility', 'asns', 'time_zone',
- 'description', 'physical_address', 'shipping_address', 'latitude', 'longitude', 'comments', 'tags',
- )
- widgets = {
- 'physical_address': forms.Textarea(
- attrs={
- 'rows': 3,
- }
- ),
- 'shipping_address': forms.Textarea(
- attrs={
- 'rows': 3,
- }
- ),
- }
- class LocationForm(TenancyForm, NetBoxModelForm):
- site = DynamicModelChoiceField(
- queryset=Site.objects.all(),
- selector=True
- )
- parent = DynamicModelChoiceField(
- queryset=Location.objects.all(),
- required=False,
- query_params={
- 'site_id': '$site'
- }
- )
- slug = SlugField()
- fieldsets = (
- ('Location', ('site', 'parent', 'name', 'slug', 'status', 'description', 'tags')),
- ('Tenancy', ('tenant_group', 'tenant')),
- )
- class Meta:
- model = Location
- fields = (
- 'site', 'parent', 'name', 'slug', 'status', 'description', 'tenant_group', 'tenant', 'tags',
- )
- class RackRoleForm(NetBoxModelForm):
- slug = SlugField()
- fieldsets = (
- ('Rack Role', (
- 'name', 'slug', 'color', 'description', 'tags',
- )),
- )
- class Meta:
- model = RackRole
- fields = [
- 'name', 'slug', 'color', 'description', 'tags',
- ]
- class RackForm(TenancyForm, NetBoxModelForm):
- site = DynamicModelChoiceField(
- queryset=Site.objects.all(),
- selector=True
- )
- location = DynamicModelChoiceField(
- queryset=Location.objects.all(),
- required=False,
- query_params={
- 'site_id': '$site'
- }
- )
- role = DynamicModelChoiceField(
- queryset=RackRole.objects.all(),
- required=False
- )
- comments = CommentField()
- class Meta:
- model = Rack
- fields = [
- 'site', 'location', 'name', 'facility_id', 'tenant_group', 'tenant', 'status', 'role', 'serial',
- 'asset_tag', 'type', 'width', 'u_height', 'desc_units', 'outer_width', 'outer_depth', 'outer_unit',
- 'mounting_depth', 'weight', 'max_weight', 'weight_unit', 'description', 'comments', 'tags',
- ]
- class RackReservationForm(TenancyForm, NetBoxModelForm):
- rack = DynamicModelChoiceField(
- queryset=Rack.objects.all(),
- selector=True
- )
- units = NumericArrayField(
- base_field=forms.IntegerField(),
- help_text=_("Comma-separated list of numeric unit IDs. A range may be specified using a hyphen.")
- )
- user = forms.ModelChoiceField(
- queryset=User.objects.order_by(
- 'username'
- )
- )
- comments = CommentField()
- fieldsets = (
- ('Reservation', ('rack', 'units', 'user', 'description', 'tags')),
- ('Tenancy', ('tenant_group', 'tenant')),
- )
- class Meta:
- model = RackReservation
- fields = [
- 'rack', 'units', 'user', 'tenant_group', 'tenant', 'description', 'comments', 'tags',
- ]
- class ManufacturerForm(NetBoxModelForm):
- slug = SlugField()
- fieldsets = (
- ('Manufacturer', (
- 'name', 'slug', 'description', 'tags',
- )),
- )
- class Meta:
- model = Manufacturer
- fields = [
- 'name', 'slug', 'description', 'tags',
- ]
- class DeviceTypeForm(NetBoxModelForm):
- manufacturer = DynamicModelChoiceField(
- queryset=Manufacturer.objects.all()
- )
- default_platform = DynamicModelChoiceField(
- queryset=Platform.objects.all(),
- required=False
- )
- slug = SlugField(
- slug_source='model'
- )
- comments = CommentField()
- fieldsets = (
- ('Device Type', ('manufacturer', 'model', 'slug', 'default_platform', 'description', 'tags')),
- ('Chassis', (
- 'u_height', 'is_full_depth', 'part_number', 'subdevice_role', 'airflow', 'weight', 'weight_unit',
- )),
- ('Images', ('front_image', 'rear_image')),
- )
- class Meta:
- model = DeviceType
- fields = [
- 'manufacturer', 'model', 'slug', 'default_platform', 'part_number', 'u_height', 'is_full_depth',
- 'subdevice_role', 'airflow', 'weight', 'weight_unit', 'front_image', 'rear_image', 'description',
- 'comments', 'tags',
- ]
- widgets = {
- 'front_image': ClearableFileInput(attrs={
- 'accept': DEVICETYPE_IMAGE_FORMATS
- }),
- 'rear_image': ClearableFileInput(attrs={
- 'accept': DEVICETYPE_IMAGE_FORMATS
- }),
- }
- class ModuleTypeForm(NetBoxModelForm):
- manufacturer = DynamicModelChoiceField(
- queryset=Manufacturer.objects.all()
- )
- comments = CommentField()
- fieldsets = (
- ('Module Type', ('manufacturer', 'model', 'part_number', 'description', 'tags')),
- ('Weight', ('weight', 'weight_unit'))
- )
- class Meta:
- model = ModuleType
- fields = [
- 'manufacturer', 'model', 'part_number', 'weight', 'weight_unit', 'description', 'comments', 'tags',
- ]
- class DeviceRoleForm(NetBoxModelForm):
- config_template = DynamicModelChoiceField(
- queryset=ConfigTemplate.objects.all(),
- required=False
- )
- slug = SlugField()
- fieldsets = (
- ('Device Role', (
- 'name', 'slug', 'color', 'vm_role', 'config_template', 'description', 'tags',
- )),
- )
- class Meta:
- model = DeviceRole
- fields = [
- 'name', 'slug', 'color', 'vm_role', 'config_template', 'description', 'tags',
- ]
- class PlatformForm(NetBoxModelForm):
- manufacturer = DynamicModelChoiceField(
- queryset=Manufacturer.objects.all(),
- required=False
- )
- config_template = DynamicModelChoiceField(
- queryset=ConfigTemplate.objects.all(),
- required=False
- )
- slug = SlugField(
- max_length=64
- )
- fieldsets = (
- ('Platform', ('name', 'slug', 'manufacturer', 'config_template', 'description', 'tags')),
- )
- class Meta:
- model = Platform
- fields = [
- 'name', 'slug', 'manufacturer', 'config_template', 'description', 'tags',
- ]
- class DeviceForm(TenancyForm, NetBoxModelForm):
- site = DynamicModelChoiceField(
- queryset=Site.objects.all(),
- selector=True
- )
- location = DynamicModelChoiceField(
- queryset=Location.objects.all(),
- required=False,
- query_params={
- 'site_id': '$site'
- },
- initial_params={
- 'racks': '$rack'
- }
- )
- rack = DynamicModelChoiceField(
- queryset=Rack.objects.all(),
- required=False,
- query_params={
- 'site_id': '$site',
- 'location_id': '$location',
- }
- )
- position = forms.DecimalField(
- required=False,
- help_text=_("The lowest-numbered unit occupied by the device"),
- widget=APISelect(
- api_url='/api/dcim/racks/{{rack}}/elevation/',
- attrs={
- 'disabled-indicator': 'device',
- 'data-dynamic-params': '[{"fieldName":"face","queryParam":"face"}]'
- }
- )
- )
- device_type = DynamicModelChoiceField(
- queryset=DeviceType.objects.all(),
- selector=True
- )
- device_role = DynamicModelChoiceField(
- queryset=DeviceRole.objects.all()
- )
- platform = DynamicModelChoiceField(
- queryset=Platform.objects.all(),
- required=False
- )
- cluster = DynamicModelChoiceField(
- queryset=Cluster.objects.all(),
- required=False,
- selector=True
- )
- comments = CommentField()
- local_context_data = JSONField(
- required=False,
- label=''
- )
- virtual_chassis = DynamicModelChoiceField(
- queryset=VirtualChassis.objects.all(),
- required=False,
- selector=True
- )
- vc_position = forms.IntegerField(
- required=False,
- label=_('Position'),
- help_text=_("The position in the virtual chassis this device is identified by")
- )
- vc_priority = forms.IntegerField(
- required=False,
- label=_('Priority'),
- help_text=_("The priority of the device in the virtual chassis")
- )
- config_template = DynamicModelChoiceField(
- queryset=ConfigTemplate.objects.all(),
- required=False
- )
- class Meta:
- model = Device
- fields = [
- 'name', 'device_role', 'device_type', 'serial', 'asset_tag', 'site', 'rack', 'location', 'position', 'face',
- 'latitude', 'longitude', 'status', 'airflow', 'platform', 'primary_ip4', 'primary_ip6', 'cluster',
- 'tenant_group', 'tenant', 'virtual_chassis', 'vc_position', 'vc_priority', 'description', 'config_template',
- 'comments', 'tags', 'local_context_data'
- ]
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- if self.instance.pk:
- # Compile list of choices for primary IPv4 and IPv6 addresses
- for family in [4, 6]:
- ip_choices = [(None, '---------')]
- # Gather PKs of all interfaces belonging to this Device or a peer VirtualChassis member
- interface_ids = self.instance.vc_interfaces(if_master=False).values_list('pk', flat=True)
- # Collect interface IPs
- interface_ips = IPAddress.objects.filter(
- address__family=family,
- assigned_object_type=ContentType.objects.get_for_model(Interface),
- assigned_object_id__in=interface_ids
- ).prefetch_related('assigned_object')
- if interface_ips:
- ip_list = [(ip.id, f'{ip.address} ({ip.assigned_object})') for ip in interface_ips]
- ip_choices.append(('Interface IPs', ip_list))
- # Collect NAT IPs
- nat_ips = IPAddress.objects.prefetch_related('nat_inside').filter(
- address__family=family,
- nat_inside__assigned_object_type=ContentType.objects.get_for_model(Interface),
- nat_inside__assigned_object_id__in=interface_ids
- ).prefetch_related('assigned_object')
- if nat_ips:
- ip_list = [(ip.id, f'{ip.address} (NAT)') for ip in nat_ips]
- ip_choices.append(('NAT IPs', ip_list))
- self.fields['primary_ip{}'.format(family)].choices = ip_choices
- # If editing an existing device, exclude it from the list of occupied rack units. This ensures that a device
- # can be flipped from one face to another.
- self.fields['position'].widget.add_query_param('exclude', self.instance.pk)
- # Disable rack assignment if this is a child device installed in a parent device
- if self.instance.device_type.is_child_device and hasattr(self.instance, 'parent_bay'):
- self.fields['site'].disabled = True
- self.fields['rack'].disabled = True
- self.initial['site'] = self.instance.parent_bay.device.site_id
- self.initial['rack'] = self.instance.parent_bay.device.rack_id
- else:
- # An object that doesn't exist yet can't have any IPs assigned to it
- self.fields['primary_ip4'].choices = []
- self.fields['primary_ip4'].widget.attrs['readonly'] = True
- self.fields['primary_ip6'].choices = []
- self.fields['primary_ip6'].widget.attrs['readonly'] = True
- # Rack position
- position = self.data.get('position') or self.initial.get('position')
- if position:
- self.fields['position'].widget.choices = [(position, f'U{position}')]
- class ModuleForm(ModuleCommonForm, NetBoxModelForm):
- device = DynamicModelChoiceField(
- queryset=Device.objects.all(),
- initial_params={
- 'modulebays': '$module_bay'
- }
- )
- module_bay = DynamicModelChoiceField(
- queryset=ModuleBay.objects.all(),
- query_params={
- 'device_id': '$device'
- }
- )
- module_type = DynamicModelChoiceField(
- queryset=ModuleType.objects.all(),
- selector=True
- )
- comments = CommentField()
- replicate_components = forms.BooleanField(
- required=False,
- initial=True,
- help_text=_("Automatically populate components associated with this module type")
- )
- adopt_components = forms.BooleanField(
- required=False,
- initial=False,
- help_text=_("Adopt already existing components")
- )
- fieldsets = (
- ('Module', ('device', 'module_bay', 'module_type', 'status', 'description', 'tags')),
- ('Hardware', (
- 'serial', 'asset_tag', 'replicate_components', 'adopt_components',
- )),
- )
- class Meta:
- model = Module
- fields = [
- 'device', 'module_bay', 'module_type', 'status', 'serial', 'asset_tag', 'tags', 'replicate_components',
- 'adopt_components', 'description', 'comments',
- ]
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- if self.instance.pk:
- self.fields['device'].disabled = True
- self.fields['replicate_components'].initial = False
- self.fields['replicate_components'].disabled = True
- self.fields['adopt_components'].initial = False
- self.fields['adopt_components'].disabled = True
- class CableForm(TenancyForm, NetBoxModelForm):
- comments = CommentField()
- class Meta:
- model = Cable
- fields = [
- 'type', 'status', 'tenant_group', 'tenant', 'label', 'color', 'length', 'length_unit', 'description',
- 'comments', 'tags',
- ]
- error_messages = {
- 'length': {
- 'max_value': 'Maximum length is 32767 (any unit)'
- }
- }
- class PowerPanelForm(NetBoxModelForm):
- site = DynamicModelChoiceField(
- queryset=Site.objects.all(),
- selector=True
- )
- location = DynamicModelChoiceField(
- queryset=Location.objects.all(),
- required=False,
- query_params={
- 'site_id': '$site'
- }
- )
- comments = CommentField()
- fieldsets = (
- ('Power Panel', ('site', 'location', 'name', 'description', 'tags')),
- )
- class Meta:
- model = PowerPanel
- fields = [
- 'site', 'location', 'name', 'description', 'comments', 'tags',
- ]
- class PowerFeedForm(NetBoxModelForm):
- power_panel = DynamicModelChoiceField(
- queryset=PowerPanel.objects.all(),
- selector=True
- )
- rack = DynamicModelChoiceField(
- queryset=Rack.objects.all(),
- required=False,
- selector=True
- )
- comments = CommentField()
- fieldsets = (
- ('Power Feed', ('power_panel', 'rack', 'name', 'status', 'type', 'description', 'mark_connected', 'tags')),
- ('Characteristics', ('supply', 'voltage', 'amperage', 'phase', 'max_utilization')),
- )
- class Meta:
- model = PowerFeed
- fields = [
- 'power_panel', 'rack', 'name', 'status', 'type', 'mark_connected', 'supply', 'phase', 'voltage', 'amperage',
- 'max_utilization', 'description', 'comments', 'tags',
- ]
- #
- # Virtual chassis
- #
- class VirtualChassisForm(NetBoxModelForm):
- master = forms.ModelChoiceField(
- queryset=Device.objects.all(),
- required=False,
- )
- comments = CommentField()
- class Meta:
- model = VirtualChassis
- fields = [
- 'name', 'domain', 'master', 'description', 'comments', 'tags',
- ]
- widgets = {
- 'master': SelectWithPK(),
- }
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.fields['master'].queryset = Device.objects.filter(virtual_chassis=self.instance)
- class DeviceVCMembershipForm(forms.ModelForm):
- class Meta:
- model = Device
- fields = [
- 'vc_position', 'vc_priority',
- ]
- labels = {
- 'vc_position': 'Position',
- 'vc_priority': 'Priority',
- }
- def __init__(self, validate_vc_position=False, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Require VC position (only required when the Device is a VirtualChassis member)
- self.fields['vc_position'].required = True
- # Add bootstrap classes to form elements.
- self.fields['vc_position'].widget.attrs = {'class': 'form-control'}
- self.fields['vc_priority'].widget.attrs = {'class': 'form-control'}
- # Validation of vc_position is optional. This is only required when adding a new member to an existing
- # VirtualChassis. Otherwise, vc_position validation is handled by BaseVCMemberFormSet.
- self.validate_vc_position = validate_vc_position
- def clean_vc_position(self):
- vc_position = self.cleaned_data['vc_position']
- if self.validate_vc_position:
- conflicting_members = Device.objects.filter(
- virtual_chassis=self.instance.virtual_chassis,
- vc_position=vc_position
- )
- if conflicting_members.exists():
- raise forms.ValidationError(
- 'A virtual chassis member already exists in position {}.'.format(vc_position)
- )
- return vc_position
- class VCMemberSelectForm(BootstrapMixin, forms.Form):
- device = DynamicModelChoiceField(
- queryset=Device.objects.all(),
- query_params={
- 'virtual_chassis_id': 'null',
- },
- selector=True
- )
- def clean_device(self):
- device = self.cleaned_data['device']
- if device.virtual_chassis is not None:
- raise forms.ValidationError(
- f"Device {device} is already assigned to a virtual chassis."
- )
- return device
- #
- # Device component templates
- #
- class ComponentTemplateForm(BootstrapMixin, forms.ModelForm):
- device_type = DynamicModelChoiceField(
- queryset=DeviceType.objects.all()
- )
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Disable reassignment of DeviceType when editing an existing instance
- if self.instance.pk:
- self.fields['device_type'].disabled = True
- class ModularComponentTemplateForm(ComponentTemplateForm):
- device_type = DynamicModelChoiceField(
- queryset=DeviceType.objects.all().all(),
- required=False
- )
- module_type = DynamicModelChoiceField(
- queryset=ModuleType.objects.all(),
- required=False
- )
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Disable reassignment of ModuleType when editing an existing instance
- if self.instance.pk:
- self.fields['module_type'].disabled = True
- class ConsolePortTemplateForm(ModularComponentTemplateForm):
- fieldsets = (
- (None, ('device_type', 'module_type', 'name', 'label', 'type', 'description')),
- )
- class Meta:
- model = ConsolePortTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'description',
- ]
- class ConsoleServerPortTemplateForm(ModularComponentTemplateForm):
- fieldsets = (
- (None, ('device_type', 'module_type', 'name', 'label', 'type', 'description')),
- )
- class Meta:
- model = ConsoleServerPortTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'description',
- ]
- class PowerPortTemplateForm(ModularComponentTemplateForm):
- fieldsets = (
- (None, (
- 'device_type', 'module_type', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description',
- )),
- )
- class Meta:
- model = PowerPortTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description',
- ]
- class PowerOutletTemplateForm(ModularComponentTemplateForm):
- power_port = DynamicModelChoiceField(
- queryset=PowerPortTemplate.objects.all(),
- required=False,
- query_params={
- 'devicetype_id': '$device_type',
- }
- )
- fieldsets = (
- (None, ('device_type', 'module_type', 'name', 'label', 'type', 'power_port', 'feed_leg', 'description')),
- )
- class Meta:
- model = PowerOutletTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'power_port', 'feed_leg', 'description',
- ]
- class InterfaceTemplateForm(ModularComponentTemplateForm):
- bridge = DynamicModelChoiceField(
- queryset=InterfaceTemplate.objects.all(),
- required=False,
- query_params={
- 'devicetype_id': '$device_type',
- 'moduletype_id': '$module_type',
- }
- )
- fieldsets = (
- (None, ('device_type', 'module_type', 'name', 'label', 'type', 'enabled', 'mgmt_only', 'description', 'bridge')),
- ('PoE', ('poe_mode', 'poe_type'))
- )
- class Meta:
- model = InterfaceTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'mgmt_only', 'enabled', 'description', 'poe_mode', 'poe_type', 'bridge',
- ]
- class FrontPortTemplateForm(ModularComponentTemplateForm):
- rear_port = DynamicModelChoiceField(
- queryset=RearPortTemplate.objects.all(),
- required=False,
- query_params={
- 'devicetype_id': '$device_type',
- 'moduletype_id': '$module_type',
- }
- )
- fieldsets = (
- (None, (
- 'device_type', 'module_type', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position',
- 'description',
- )),
- )
- class Meta:
- model = FrontPortTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position',
- 'description',
- ]
- class RearPortTemplateForm(ModularComponentTemplateForm):
- fieldsets = (
- (None, ('device_type', 'module_type', 'name', 'label', 'type', 'color', 'positions', 'description')),
- )
- class Meta:
- model = RearPortTemplate
- fields = [
- 'device_type', 'module_type', 'name', 'label', 'type', 'color', 'positions', 'description',
- ]
- class ModuleBayTemplateForm(ComponentTemplateForm):
- fieldsets = (
- (None, ('device_type', 'name', 'label', 'position', 'description')),
- )
- class Meta:
- model = ModuleBayTemplate
- fields = [
- 'device_type', 'name', 'label', 'position', 'description',
- ]
- class DeviceBayTemplateForm(ComponentTemplateForm):
- fieldsets = (
- (None, ('device_type', 'name', 'label', 'description')),
- )
- class Meta:
- model = DeviceBayTemplate
- fields = [
- 'device_type', 'name', 'label', 'description',
- ]
- class InventoryItemTemplateForm(ComponentTemplateForm):
- parent = DynamicModelChoiceField(
- queryset=InventoryItemTemplate.objects.all(),
- required=False,
- query_params={
- 'devicetype_id': '$device_type'
- }
- )
- role = DynamicModelChoiceField(
- queryset=InventoryItemRole.objects.all(),
- required=False
- )
- manufacturer = DynamicModelChoiceField(
- queryset=Manufacturer.objects.all(),
- required=False
- )
- component_type = ContentTypeChoiceField(
- queryset=ContentType.objects.all(),
- limit_choices_to=MODULAR_COMPONENT_TEMPLATE_MODELS,
- required=False,
- widget=forms.HiddenInput
- )
- component_id = forms.IntegerField(
- required=False,
- widget=forms.HiddenInput
- )
- fieldsets = (
- (None, (
- 'device_type', 'parent', 'name', 'label', 'role', 'manufacturer', 'part_id', 'description',
- 'component_type', 'component_id',
- )),
- )
- class Meta:
- model = InventoryItemTemplate
- fields = [
- 'device_type', 'parent', 'name', 'label', 'role', 'manufacturer', 'part_id', 'description',
- 'component_type', 'component_id',
- ]
- #
- # Device components
- #
- class DeviceComponentForm(NetBoxModelForm):
- device = DynamicModelChoiceField(
- queryset=Device.objects.all(),
- selector=True
- )
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Disable reassignment of Device when editing an existing instance
- if self.instance.pk:
- self.fields['device'].disabled = True
- class ModularDeviceComponentForm(DeviceComponentForm):
- module = DynamicModelChoiceField(
- queryset=Module.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device',
- }
- )
- class ConsolePortForm(ModularDeviceComponentForm):
- fieldsets = (
- (None, (
- 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags',
- )),
- )
- class Meta:
- model = ConsolePort
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags',
- ]
- class ConsoleServerPortForm(ModularDeviceComponentForm):
- fieldsets = (
- (None, (
- 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags',
- )),
- )
- class Meta:
- model = ConsoleServerPort
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'speed', 'mark_connected', 'description', 'tags',
- ]
- class PowerPortForm(ModularDeviceComponentForm):
- fieldsets = (
- (None, (
- 'device', 'module', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'mark_connected',
- 'description', 'tags',
- )),
- )
- class Meta:
- model = PowerPort
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'mark_connected',
- 'description', 'tags',
- ]
- class PowerOutletForm(ModularDeviceComponentForm):
- power_port = DynamicModelChoiceField(
- queryset=PowerPort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device',
- }
- )
- fieldsets = (
- (None, (
- 'device', 'module', 'name', 'label', 'type', 'power_port', 'feed_leg', 'mark_connected', 'description',
- 'tags',
- )),
- )
- class Meta:
- model = PowerOutlet
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'power_port', 'feed_leg', 'mark_connected', 'description',
- 'tags',
- ]
- class InterfaceForm(InterfaceCommonForm, ModularDeviceComponentForm):
- vdcs = DynamicModelMultipleChoiceField(
- queryset=VirtualDeviceContext.objects.all(),
- required=False,
- label='Virtual Device Contexts',
- query_params={
- 'device_id': '$device',
- }
- )
- parent = DynamicModelChoiceField(
- queryset=Interface.objects.all(),
- required=False,
- label=_('Parent interface'),
- query_params={
- 'device_id': '$device',
- }
- )
- bridge = DynamicModelChoiceField(
- queryset=Interface.objects.all(),
- required=False,
- label=_('Bridged interface'),
- query_params={
- 'device_id': '$device',
- }
- )
- lag = DynamicModelChoiceField(
- queryset=Interface.objects.all(),
- required=False,
- label=_('LAG interface'),
- query_params={
- 'device_id': '$device',
- 'type': 'lag',
- }
- )
- wireless_lan_group = DynamicModelChoiceField(
- queryset=WirelessLANGroup.objects.all(),
- required=False,
- label=_('Wireless LAN group')
- )
- wireless_lans = DynamicModelMultipleChoiceField(
- queryset=WirelessLAN.objects.all(),
- required=False,
- label=_('Wireless LANs'),
- query_params={
- 'group_id': '$wireless_lan_group',
- }
- )
- vlan_group = DynamicModelChoiceField(
- queryset=VLANGroup.objects.all(),
- required=False,
- label=_('VLAN group')
- )
- untagged_vlan = DynamicModelChoiceField(
- queryset=VLAN.objects.all(),
- required=False,
- label=_('Untagged VLAN'),
- query_params={
- 'group_id': '$vlan_group',
- 'available_on_device': '$device',
- }
- )
- tagged_vlans = DynamicModelMultipleChoiceField(
- queryset=VLAN.objects.all(),
- required=False,
- label=_('Tagged VLANs'),
- query_params={
- 'group_id': '$vlan_group',
- 'available_on_device': '$device',
- }
- )
- vrf = DynamicModelChoiceField(
- queryset=VRF.objects.all(),
- required=False,
- label=_('VRF')
- )
- wwn = forms.CharField(
- empty_value=None,
- required=False,
- label=_('WWN')
- )
- fieldsets = (
- ('Interface', ('device', 'module', 'name', 'label', 'type', 'speed', 'duplex', 'description', 'tags')),
- ('Addressing', ('vrf', 'mac_address', 'wwn')),
- ('Operation', ('vdcs', 'mtu', 'tx_power', 'enabled', 'mgmt_only', 'mark_connected')),
- ('Related Interfaces', ('parent', 'bridge', 'lag')),
- ('PoE', ('poe_mode', 'poe_type')),
- ('802.1Q Switching', ('mode', 'vlan_group', 'untagged_vlan', 'tagged_vlans')),
- ('Wireless', (
- 'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'wireless_lan_group', 'wireless_lans',
- )),
- )
- class Meta:
- model = Interface
- fields = [
- 'device', 'module', 'vdcs', 'name', 'label', 'type', 'speed', 'duplex', 'enabled', 'parent', 'bridge', 'lag',
- 'mac_address', 'wwn', 'mtu', 'mgmt_only', 'mark_connected', 'description', 'poe_mode', 'poe_type', 'mode',
- 'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'tx_power', 'wireless_lans',
- 'untagged_vlan', 'tagged_vlans', 'vrf', 'tags',
- ]
- widgets = {
- 'speed': NumberWithOptions(
- options=InterfaceSpeedChoices
- ),
- 'mode': HTMXSelect(),
- }
- labels = {
- 'mode': '802.1Q Mode',
- }
- class FrontPortForm(ModularDeviceComponentForm):
- rear_port = DynamicModelChoiceField(
- queryset=RearPort.objects.all(),
- query_params={
- 'device_id': '$device',
- }
- )
- fieldsets = (
- (None, (
- 'device', 'module', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position', 'mark_connected',
- 'description', 'tags',
- )),
- )
- class Meta:
- model = FrontPort
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'color', 'rear_port', 'rear_port_position', 'mark_connected',
- 'description', 'tags',
- ]
- class RearPortForm(ModularDeviceComponentForm):
- fieldsets = (
- (None, (
- 'device', 'module', 'name', 'label', 'type', 'color', 'positions', 'mark_connected', 'description', 'tags',
- )),
- )
- class Meta:
- model = RearPort
- fields = [
- 'device', 'module', 'name', 'label', 'type', 'color', 'positions', 'mark_connected', 'description', 'tags',
- ]
- class ModuleBayForm(DeviceComponentForm):
- fieldsets = (
- (None, ('device', 'name', 'label', 'position', 'description', 'tags',)),
- )
- class Meta:
- model = ModuleBay
- fields = [
- 'device', 'name', 'label', 'position', 'description', 'tags',
- ]
- class DeviceBayForm(DeviceComponentForm):
- fieldsets = (
- (None, ('device', 'name', 'label', 'description', 'tags',)),
- )
- class Meta:
- model = DeviceBay
- fields = [
- 'device', 'name', 'label', 'description', 'tags',
- ]
- class PopulateDeviceBayForm(BootstrapMixin, forms.Form):
- installed_device = forms.ModelChoiceField(
- queryset=Device.objects.all(),
- label=_('Child Device'),
- help_text=_("Child devices must first be created and assigned to the site and rack of the parent device.")
- )
- def __init__(self, device_bay, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.fields['installed_device'].queryset = Device.objects.filter(
- site=device_bay.device.site,
- rack=device_bay.device.rack,
- parent_bay__isnull=True,
- device_type__u_height=0,
- device_type__subdevice_role=SubdeviceRoleChoices.ROLE_CHILD
- ).exclude(pk=device_bay.device.pk)
- class InventoryItemForm(DeviceComponentForm):
- parent = DynamicModelChoiceField(
- queryset=InventoryItem.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- }
- )
- role = DynamicModelChoiceField(
- queryset=InventoryItemRole.objects.all(),
- required=False
- )
- manufacturer = DynamicModelChoiceField(
- queryset=Manufacturer.objects.all(),
- required=False
- )
- # Assigned component selectors
- consoleport = DynamicModelChoiceField(
- queryset=ConsolePort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Console port')
- )
- consoleserverport = DynamicModelChoiceField(
- queryset=ConsoleServerPort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Console server port')
- )
- frontport = DynamicModelChoiceField(
- queryset=FrontPort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Front port')
- )
- interface = DynamicModelChoiceField(
- queryset=Interface.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Interface')
- )
- poweroutlet = DynamicModelChoiceField(
- queryset=PowerOutlet.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Power outlet')
- )
- powerport = DynamicModelChoiceField(
- queryset=PowerPort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Power port')
- )
- rearport = DynamicModelChoiceField(
- queryset=RearPort.objects.all(),
- required=False,
- query_params={
- 'device_id': '$device'
- },
- label=_('Rear port')
- )
- fieldsets = (
- ('Inventory Item', ('device', 'parent', 'name', 'label', 'role', 'description', 'tags')),
- ('Hardware', ('manufacturer', 'part_id', 'serial', 'asset_tag')),
- )
- class Meta:
- model = InventoryItem
- fields = [
- 'device', 'parent', 'name', 'label', 'role', 'manufacturer', 'part_id', 'serial', 'asset_tag',
- 'description', 'tags',
- ]
- def __init__(self, *args, **kwargs):
- instance = kwargs.get('instance')
- initial = kwargs.get('initial', {}).copy()
- component_type = initial.get('component_type')
- component_id = initial.get('component_id')
- # Used for picking the default active tab for component selection
- self.no_component = True
- if instance:
- # When editing set the initial value for component selectin
- for component_model in ContentType.objects.filter(MODULAR_COMPONENT_MODELS):
- if type(instance.component) is component_model.model_class():
- initial[component_model.model] = instance.component
- self.no_component = False
- break
- elif component_type and component_id:
- # When adding the InventoryItem from a component page
- if content_type := ContentType.objects.filter(MODULAR_COMPONENT_MODELS).filter(pk=component_type).first():
- if component := content_type.model_class().objects.filter(pk=component_id).first():
- initial[content_type.model] = component
- self.no_component = False
- kwargs['initial'] = initial
- super().__init__(*args, **kwargs)
- # Specifically allow editing the device of IntentoryItems
- if self.instance.pk:
- self.fields['device'].disabled = False
- def clean(self):
- super().clean()
- # Handle object assignment
- selected_objects = [
- field for field in (
- 'consoleport', 'consoleserverport', 'frontport', 'interface', 'poweroutlet', 'powerport', 'rearport'
- ) if self.cleaned_data[field]
- ]
- if len(selected_objects) > 1:
- raise forms.ValidationError("An InventoryItem can only be assigned to a single component.")
- elif selected_objects:
- self.instance.component = self.cleaned_data[selected_objects[0]]
- else:
- self.instance.component = None
- # Device component roles
- #
- class InventoryItemRoleForm(NetBoxModelForm):
- slug = SlugField()
- fieldsets = (
- ('Inventory Item Role', (
- 'name', 'slug', 'color', 'description', 'tags',
- )),
- )
- class Meta:
- model = InventoryItemRole
- fields = [
- 'name', 'slug', 'color', 'description', 'tags',
- ]
- class VirtualDeviceContextForm(TenancyForm, NetBoxModelForm):
- device = DynamicModelChoiceField(
- queryset=Device.objects.all(),
- selector=True
- )
- primary_ip4 = DynamicModelChoiceField(
- queryset=IPAddress.objects.all(),
- label='Primary IPv4',
- required=False,
- query_params={
- 'device_id': '$device',
- 'family': '4',
- }
- )
- primary_ip6 = DynamicModelChoiceField(
- queryset=IPAddress.objects.all(),
- label='Primary IPv6',
- required=False,
- query_params={
- 'device_id': '$device',
- 'family': '6',
- }
- )
- fieldsets = (
- ('Virtual Device Context', ('device', 'name', 'status', 'identifier', 'primary_ip4', 'primary_ip6', 'tags')),
- ('Tenancy', ('tenant_group', 'tenant'))
- )
- class Meta:
- model = VirtualDeviceContext
- fields = [
- 'device', 'name', 'status', 'identifier', 'primary_ip4', 'primary_ip6', 'tenant_group', 'tenant',
- 'comments', 'tags'
- ]
|