bulk_edit.py 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315
  1. from decimal import Decimal
  2. from django import forms
  3. from django.conf import settings
  4. from django.utils.translation import gettext_lazy as _
  5. from timezone_field import TimeZoneFormField
  6. from dcim.choices import *
  7. from dcim.constants import *
  8. from dcim.models import *
  9. from extras.models import ConfigTemplate
  10. from ipam.choices import VLANQinQRoleChoices
  11. from ipam.models import ASN, VLAN, VRF, VLANGroup
  12. from netbox.choices import *
  13. from netbox.forms import (
  14. NestedGroupModelBulkEditForm,
  15. NetBoxModelBulkEditForm,
  16. OrganizationalModelBulkEditForm,
  17. PrimaryModelBulkEditForm,
  18. )
  19. from netbox.forms.mixins import ChangelogMessageMixin, OwnerMixin
  20. from tenancy.models import Tenant
  21. from users.models import User
  22. from utilities.forms import BulkEditForm, add_blank_choice, form_from_model
  23. from utilities.forms.fields import (
  24. ChoiceField,
  25. ColorField,
  26. DynamicModelChoiceField,
  27. DynamicModelMultipleChoiceField,
  28. JSONField,
  29. PositiveBigIntegerField,
  30. )
  31. from utilities.forms.rendering import FieldSet, InlineFields, TabbedGroups
  32. from utilities.forms.widgets import BulkEditNullBooleanSelect, DatePicker, NumberWithOptions
  33. from virtualization.models import Cluster
  34. from wireless.choices import WirelessRoleChoices
  35. from wireless.models import WirelessLAN, WirelessLANGroup
  36. __all__ = (
  37. 'CableBulkEditForm',
  38. 'CableBundleBulkEditForm',
  39. 'ConsolePortBulkEditForm',
  40. 'ConsolePortTemplateBulkEditForm',
  41. 'ConsoleServerPortBulkEditForm',
  42. 'ConsoleServerPortTemplateBulkEditForm',
  43. 'CoolingFeedBulkEditForm',
  44. 'CoolingIntakeBulkEditForm',
  45. 'CoolingIntakeTemplateBulkEditForm',
  46. 'CoolingOutflowBulkEditForm',
  47. 'CoolingOutflowTemplateBulkEditForm',
  48. 'CoolingSourceBulkEditForm',
  49. 'DeviceBayBulkEditForm',
  50. 'DeviceBayTemplateBulkEditForm',
  51. 'DeviceBulkEditForm',
  52. 'DeviceRoleBulkEditForm',
  53. 'DeviceTypeBulkEditForm',
  54. 'FrontPortBulkEditForm',
  55. 'FrontPortTemplateBulkEditForm',
  56. 'InterfaceBulkEditForm',
  57. 'InterfaceTemplateBulkEditForm',
  58. 'InventoryItemBulkEditForm',
  59. 'InventoryItemRoleBulkEditForm',
  60. 'InventoryItemTemplateBulkEditForm',
  61. 'LocationBulkEditForm',
  62. 'MACAddressBulkEditForm',
  63. 'ManufacturerBulkEditForm',
  64. 'ModuleBayBulkEditForm',
  65. 'ModuleBayTemplateBulkEditForm',
  66. 'ModuleBayTypeBulkEditForm',
  67. 'ModuleBulkEditForm',
  68. 'ModuleTypeBulkEditForm',
  69. 'ModuleTypeProfileBulkEditForm',
  70. 'PlatformBulkEditForm',
  71. 'PowerFeedBulkEditForm',
  72. 'PowerOutletBulkEditForm',
  73. 'PowerOutletTemplateBulkEditForm',
  74. 'PowerPanelBulkEditForm',
  75. 'PowerPortBulkEditForm',
  76. 'PowerPortTemplateBulkEditForm',
  77. 'RackBulkEditForm',
  78. 'RackGroupBulkEditForm',
  79. 'RackReservationBulkEditForm',
  80. 'RackRoleBulkEditForm',
  81. 'RackTypeBulkEditForm',
  82. 'RearPortBulkEditForm',
  83. 'RearPortTemplateBulkEditForm',
  84. 'RegionBulkEditForm',
  85. 'SiteBulkEditForm',
  86. 'SiteGroupBulkEditForm',
  87. 'VirtualChassisBulkEditForm',
  88. 'VirtualDeviceContextBulkEditForm'
  89. )
  90. class RegionBulkEditForm(NestedGroupModelBulkEditForm):
  91. parent = DynamicModelChoiceField(
  92. label=_('Parent'),
  93. queryset=Region.objects.all(),
  94. required=False
  95. )
  96. model = Region
  97. fieldsets = (
  98. FieldSet('parent', 'description'),
  99. )
  100. nullable_fields = ('parent', 'description', 'comments')
  101. class SiteGroupBulkEditForm(NestedGroupModelBulkEditForm):
  102. parent = DynamicModelChoiceField(
  103. label=_('Parent'),
  104. queryset=SiteGroup.objects.all(),
  105. required=False
  106. )
  107. model = SiteGroup
  108. fieldsets = (
  109. FieldSet('parent', 'description'),
  110. )
  111. nullable_fields = ('parent', 'description', 'comments')
  112. class SiteBulkEditForm(PrimaryModelBulkEditForm):
  113. status = ChoiceField(
  114. label=_('Status'),
  115. choices=add_blank_choice(SiteStatusChoices),
  116. required=False,
  117. initial=''
  118. )
  119. region = DynamicModelChoiceField(
  120. label=_('Region'),
  121. queryset=Region.objects.all(),
  122. required=False
  123. )
  124. group = DynamicModelChoiceField(
  125. label=_('Group'),
  126. queryset=SiteGroup.objects.all(),
  127. required=False
  128. )
  129. tenant = DynamicModelChoiceField(
  130. label=_('Tenant'),
  131. queryset=Tenant.objects.all(),
  132. required=False
  133. )
  134. facility = forms.CharField(
  135. label=_('Facility'),
  136. max_length=50,
  137. required=False
  138. )
  139. asns = DynamicModelMultipleChoiceField(
  140. queryset=ASN.objects.all(),
  141. label=_('ASNs'),
  142. required=False
  143. )
  144. contact_name = forms.CharField(
  145. label=_('Contact name'),
  146. max_length=50,
  147. required=False
  148. )
  149. contact_phone = forms.CharField(
  150. label=_('Contact phone'),
  151. max_length=20,
  152. required=False
  153. )
  154. contact_email = forms.EmailField(
  155. required=False,
  156. label=_('Contact E-mail')
  157. )
  158. time_zone = TimeZoneFormField(
  159. label=_('Time zone'),
  160. choices=add_blank_choice(TimeZoneFormField().choices),
  161. required=False
  162. )
  163. model = Site
  164. fieldsets = (
  165. FieldSet('status', 'region', 'group', 'tenant', 'facility', 'asns', 'time_zone', 'description'),
  166. )
  167. nullable_fields = (
  168. 'region', 'group', 'tenant', 'facility', 'asns', 'time_zone', 'description', 'comments',
  169. )
  170. class LocationBulkEditForm(NestedGroupModelBulkEditForm):
  171. site = DynamicModelChoiceField(
  172. label=_('Site'),
  173. queryset=Site.objects.all(),
  174. required=False
  175. )
  176. parent = DynamicModelChoiceField(
  177. label=_('Parent'),
  178. queryset=Location.objects.all(),
  179. required=False,
  180. query_params={
  181. 'site_id': '$site'
  182. }
  183. )
  184. status = ChoiceField(
  185. label=_('Status'),
  186. choices=add_blank_choice(LocationStatusChoices),
  187. required=False,
  188. initial=''
  189. )
  190. tenant = DynamicModelChoiceField(
  191. label=_('Tenant'),
  192. queryset=Tenant.objects.all(),
  193. required=False
  194. )
  195. facility = forms.CharField(
  196. label=_('Facility'),
  197. max_length=50,
  198. required=False
  199. )
  200. model = Location
  201. fieldsets = (
  202. FieldSet('site', 'parent', 'status', 'tenant', 'facility', 'description'),
  203. )
  204. nullable_fields = ('parent', 'tenant', 'facility', 'description', 'comments')
  205. class RackGroupBulkEditForm(OrganizationalModelBulkEditForm):
  206. model = RackGroup
  207. fieldsets = (
  208. FieldSet('description'),
  209. )
  210. nullable_fields = ('description', 'comments')
  211. class RackRoleBulkEditForm(OrganizationalModelBulkEditForm):
  212. color = ColorField(
  213. label=_('Color'),
  214. required=False
  215. )
  216. model = RackRole
  217. fieldsets = (
  218. FieldSet('color', 'description'),
  219. )
  220. nullable_fields = ('color', 'description', 'comments')
  221. class RackTypeBulkEditForm(PrimaryModelBulkEditForm):
  222. manufacturer = DynamicModelChoiceField(
  223. label=_('Manufacturer'),
  224. queryset=Manufacturer.objects.all(),
  225. required=False
  226. )
  227. form_factor = ChoiceField(
  228. label=_('Form factor'),
  229. choices=add_blank_choice(RackFormFactorChoices),
  230. required=False
  231. )
  232. width = ChoiceField(
  233. label=_('Width'),
  234. choices=add_blank_choice(RackWidthChoices),
  235. required=False
  236. )
  237. u_height = forms.IntegerField(
  238. required=False,
  239. label=_('Height (U)')
  240. )
  241. starting_unit = forms.IntegerField(
  242. required=False,
  243. min_value=1
  244. )
  245. desc_units = forms.NullBooleanField(
  246. required=False,
  247. widget=BulkEditNullBooleanSelect,
  248. label=_('Descending units')
  249. )
  250. outer_width = forms.IntegerField(
  251. label=_('Outer width'),
  252. required=False,
  253. min_value=1
  254. )
  255. outer_height = forms.IntegerField(
  256. label=_('Outer height'),
  257. required=False,
  258. min_value=1
  259. )
  260. outer_depth = forms.IntegerField(
  261. label=_('Outer depth'),
  262. required=False,
  263. min_value=1
  264. )
  265. outer_unit = ChoiceField(
  266. label=_('Outer unit'),
  267. choices=add_blank_choice(RackDimensionUnitChoices),
  268. required=False
  269. )
  270. mounting_depth = forms.IntegerField(
  271. label=_('Mounting depth'),
  272. required=False,
  273. min_value=1
  274. )
  275. weight = forms.DecimalField(
  276. label=_('Weight'),
  277. min_value=0,
  278. required=False
  279. )
  280. max_weight = forms.IntegerField(
  281. label=_('Max weight'),
  282. min_value=0,
  283. required=False
  284. )
  285. weight_unit = ChoiceField(
  286. label=_('Weight unit'),
  287. choices=add_blank_choice(WeightUnitChoices),
  288. required=False,
  289. initial=''
  290. )
  291. cooling_capability = forms.ChoiceField(
  292. label=_('Cooling capability'),
  293. choices=add_blank_choice(RackCoolingCapabilityChoices),
  294. required=False
  295. )
  296. cooling_capacity = forms.DecimalField(
  297. label=_('Cooling capacity'),
  298. min_value=0,
  299. required=False
  300. )
  301. model = RackType
  302. fieldsets = (
  303. FieldSet('manufacturer', 'description', 'form_factor', 'width', 'u_height', name=_('Rack Type')),
  304. FieldSet(
  305. InlineFields('outer_width', 'outer_height', 'outer_depth', 'outer_unit', label=_('Outer Dimensions')),
  306. InlineFields('weight', 'max_weight', 'weight_unit', label=_('Weight')),
  307. 'mounting_depth',
  308. name=_('Dimensions')
  309. ),
  310. FieldSet('starting_unit', 'desc_units', name=_('Numbering')),
  311. FieldSet('cooling_capability', 'cooling_capacity', name=_('Cooling')),
  312. )
  313. nullable_fields = (
  314. 'outer_width', 'outer_height', 'outer_depth', 'outer_unit', 'weight',
  315. 'max_weight', 'weight_unit', 'cooling_capability', 'cooling_capacity', 'description', 'comments',
  316. )
  317. class RackBulkEditForm(PrimaryModelBulkEditForm):
  318. region = DynamicModelChoiceField(
  319. label=_('Region'),
  320. queryset=Region.objects.all(),
  321. required=False,
  322. initial_params={
  323. 'sites': '$site'
  324. }
  325. )
  326. site_group = DynamicModelChoiceField(
  327. label=_('Site group'),
  328. queryset=SiteGroup.objects.all(),
  329. required=False,
  330. initial_params={
  331. 'sites': '$site'
  332. }
  333. )
  334. site = DynamicModelChoiceField(
  335. label=_('Site'),
  336. queryset=Site.objects.all(),
  337. required=False,
  338. query_params={
  339. 'region_id': '$region',
  340. 'group_id': '$site_group',
  341. }
  342. )
  343. location = DynamicModelChoiceField(
  344. label=_('Location'),
  345. queryset=Location.objects.all(),
  346. required=False,
  347. query_params={
  348. 'site_id': '$site'
  349. }
  350. )
  351. group = DynamicModelChoiceField(
  352. label=_('Group'),
  353. queryset=RackGroup.objects.all(),
  354. required=False
  355. )
  356. tenant = DynamicModelChoiceField(
  357. label=_('Tenant'),
  358. queryset=Tenant.objects.all(),
  359. required=False
  360. )
  361. status = ChoiceField(
  362. label=_('Status'),
  363. choices=add_blank_choice(RackStatusChoices),
  364. required=False,
  365. initial=''
  366. )
  367. role = DynamicModelChoiceField(
  368. label=_('Role'),
  369. queryset=RackRole.objects.all(),
  370. required=False
  371. )
  372. rack_type = DynamicModelChoiceField(
  373. label=_('Rack type'),
  374. queryset=RackType.objects.all(),
  375. required=False,
  376. )
  377. serial = forms.CharField(
  378. max_length=50,
  379. required=False,
  380. label=_('Serial Number')
  381. )
  382. asset_tag = forms.CharField(
  383. label=_('Asset tag'),
  384. max_length=50,
  385. required=False
  386. )
  387. form_factor = ChoiceField(
  388. label=_('Form factor'),
  389. choices=add_blank_choice(RackFormFactorChoices),
  390. required=False
  391. )
  392. width = ChoiceField(
  393. label=_('Width'),
  394. choices=add_blank_choice(RackWidthChoices),
  395. required=False
  396. )
  397. u_height = forms.IntegerField(
  398. required=False,
  399. label=_('Height (U)')
  400. )
  401. desc_units = forms.NullBooleanField(
  402. required=False,
  403. widget=BulkEditNullBooleanSelect,
  404. label=_('Descending units')
  405. )
  406. outer_width = forms.IntegerField(
  407. label=_('Outer width'),
  408. required=False,
  409. min_value=1
  410. )
  411. outer_height = forms.IntegerField(
  412. label=_('Outer height'),
  413. required=False,
  414. min_value=1
  415. )
  416. outer_depth = forms.IntegerField(
  417. label=_('Outer depth'),
  418. required=False,
  419. min_value=1
  420. )
  421. outer_unit = ChoiceField(
  422. label=_('Outer unit'),
  423. choices=add_blank_choice(RackDimensionUnitChoices),
  424. required=False
  425. )
  426. mounting_depth = forms.IntegerField(
  427. label=_('Mounting depth'),
  428. required=False,
  429. min_value=1
  430. )
  431. airflow = ChoiceField(
  432. label=_('Airflow'),
  433. choices=add_blank_choice(RackAirflowChoices),
  434. required=False
  435. )
  436. cooling_capability = forms.ChoiceField(
  437. label=_('Cooling capability'),
  438. choices=add_blank_choice(RackCoolingCapabilityChoices),
  439. required=False
  440. )
  441. cooling_capacity = forms.DecimalField(
  442. label=_('Cooling capacity'),
  443. min_value=0,
  444. required=False
  445. )
  446. weight = forms.DecimalField(
  447. label=_('Weight'),
  448. min_value=0,
  449. required=False
  450. )
  451. max_weight = forms.IntegerField(
  452. label=_('Max weight'),
  453. min_value=0,
  454. required=False
  455. )
  456. weight_unit = ChoiceField(
  457. label=_('Weight unit'),
  458. choices=add_blank_choice(WeightUnitChoices),
  459. required=False,
  460. initial=''
  461. )
  462. model = Rack
  463. fieldsets = (
  464. FieldSet(
  465. 'status', 'group', 'role', 'tenant', 'serial', 'asset_tag', 'rack_type', 'description', name=_('Rack')
  466. ),
  467. FieldSet('region', 'site_group', 'site', 'location', name=_('Location')),
  468. FieldSet('outer_width', 'outer_height', 'outer_depth', 'outer_unit', name=_('Outer Dimensions')),
  469. FieldSet('form_factor', 'width', 'u_height', 'desc_units', 'airflow', 'mounting_depth', name=_('Hardware')),
  470. FieldSet('cooling_capability', 'cooling_capacity', name=_('Cooling')),
  471. FieldSet('weight', 'max_weight', 'weight_unit', name=_('Weight')),
  472. )
  473. nullable_fields = (
  474. 'location', 'group', 'tenant', 'role', 'serial', 'asset_tag', 'outer_width', 'outer_height', 'outer_depth',
  475. 'outer_unit', 'cooling_capability', 'cooling_capacity', 'weight', 'max_weight', 'weight_unit', 'description',
  476. 'comments',
  477. )
  478. class RackReservationBulkEditForm(PrimaryModelBulkEditForm):
  479. status = ChoiceField(
  480. label=_('Status'),
  481. choices=add_blank_choice(RackReservationStatusChoices),
  482. required=False,
  483. initial=''
  484. )
  485. user = forms.ModelChoiceField(
  486. label=_('User'),
  487. queryset=User.objects.order_by('username'),
  488. required=False
  489. )
  490. tenant = DynamicModelChoiceField(
  491. label=_('Tenant'),
  492. queryset=Tenant.objects.all(),
  493. required=False
  494. )
  495. model = RackReservation
  496. fieldsets = (
  497. FieldSet('status', 'user', 'tenant', 'description'),
  498. )
  499. nullable_fields = ('comments',)
  500. class ManufacturerBulkEditForm(OrganizationalModelBulkEditForm):
  501. model = Manufacturer
  502. fieldsets = (
  503. FieldSet('description'),
  504. )
  505. nullable_fields = ('description', 'comments')
  506. class DeviceTypeBulkEditForm(PrimaryModelBulkEditForm):
  507. manufacturer = DynamicModelChoiceField(
  508. label=_('Manufacturer'),
  509. queryset=Manufacturer.objects.all(),
  510. required=False
  511. )
  512. default_platform = DynamicModelChoiceField(
  513. label=_('Default platform'),
  514. queryset=Platform.objects.all(),
  515. required=False
  516. )
  517. part_number = forms.CharField(
  518. label=_('Part number'),
  519. required=False
  520. )
  521. u_height = forms.IntegerField(
  522. label=_('U height'),
  523. min_value=0,
  524. required=False
  525. )
  526. is_full_depth = forms.NullBooleanField(
  527. required=False,
  528. widget=BulkEditNullBooleanSelect(),
  529. label=_('Is full depth')
  530. )
  531. exclude_from_utilization = forms.NullBooleanField(
  532. required=False,
  533. widget=BulkEditNullBooleanSelect(),
  534. label=_('Exclude from utilization')
  535. )
  536. airflow = ChoiceField(
  537. label=_('Airflow'),
  538. choices=add_blank_choice(DeviceAirflowChoices),
  539. required=False
  540. )
  541. cooling_method = forms.ChoiceField(
  542. label=_('Cooling method'),
  543. choices=add_blank_choice(CoolingMethodChoices),
  544. required=False
  545. )
  546. weight = forms.DecimalField(
  547. label=_('Weight'),
  548. min_value=0,
  549. required=False
  550. )
  551. weight_unit = ChoiceField(
  552. label=_('Weight unit'),
  553. choices=add_blank_choice(WeightUnitChoices),
  554. required=False,
  555. initial=''
  556. )
  557. end_of_life = forms.DateField(
  558. label=_('End of life'),
  559. required=False,
  560. widget=DatePicker()
  561. )
  562. model = DeviceType
  563. fieldsets = (
  564. FieldSet(
  565. 'manufacturer', 'default_platform', 'part_number', 'u_height', 'exclude_from_utilization', 'is_full_depth',
  566. 'airflow', 'cooling_method', 'description', name=_('Device Type')
  567. ),
  568. FieldSet('weight', 'weight_unit', name=_('Weight')),
  569. FieldSet('end_of_life', name=_('Lifecycle')),
  570. )
  571. nullable_fields = (
  572. 'part_number', 'airflow', 'cooling_method', 'weight', 'weight_unit', 'end_of_life', 'description', 'comments',
  573. )
  574. class ModuleBayTypeBulkEditForm(PrimaryModelBulkEditForm):
  575. manufacturer = DynamicModelChoiceField(
  576. label=_('Manufacturer'),
  577. queryset=Manufacturer.objects.all(),
  578. required=False,
  579. )
  580. color = ColorField(
  581. label=_('Color'),
  582. required=False,
  583. )
  584. model = ModuleBayType
  585. fieldsets = (
  586. FieldSet('manufacturer', 'color', 'description', name=_('Module Bay Type')),
  587. )
  588. nullable_fields = ('manufacturer', 'color', 'description', 'comments')
  589. class ModuleTypeProfileBulkEditForm(PrimaryModelBulkEditForm):
  590. schema = JSONField(
  591. label=_('Schema'),
  592. required=False
  593. )
  594. model = ModuleTypeProfile
  595. fieldsets = (
  596. FieldSet('name', 'description', 'schema', name=_('Profile')),
  597. )
  598. nullable_fields = ('description', 'comments')
  599. class ModuleTypeBulkEditForm(PrimaryModelBulkEditForm):
  600. profile = DynamicModelChoiceField(
  601. label=_('Profile'),
  602. queryset=ModuleTypeProfile.objects.all(),
  603. required=False
  604. )
  605. manufacturer = DynamicModelChoiceField(
  606. label=_('Manufacturer'),
  607. queryset=Manufacturer.objects.all(),
  608. required=False
  609. )
  610. part_number = forms.CharField(
  611. label=_('Part number'),
  612. required=False
  613. )
  614. airflow = ChoiceField(
  615. label=_('Airflow'),
  616. choices=add_blank_choice(ModuleAirflowChoices),
  617. required=False
  618. )
  619. cooling_method = forms.ChoiceField(
  620. label=_('Cooling method'),
  621. choices=add_blank_choice(CoolingMethodChoices),
  622. required=False
  623. )
  624. weight = forms.DecimalField(
  625. label=_('Weight'),
  626. min_value=0,
  627. required=False
  628. )
  629. weight_unit = ChoiceField(
  630. label=_('Weight unit'),
  631. choices=add_blank_choice(WeightUnitChoices),
  632. required=False,
  633. initial=''
  634. )
  635. end_of_life = forms.DateField(
  636. label=_('End of life'),
  637. required=False,
  638. widget=DatePicker()
  639. )
  640. add_module_bay_types = DynamicModelMultipleChoiceField(
  641. label=_('Add bay types'),
  642. queryset=ModuleBayType.objects.all(),
  643. required=False,
  644. )
  645. remove_module_bay_types = DynamicModelMultipleChoiceField(
  646. label=_('Remove bay types'),
  647. queryset=ModuleBayType.objects.all(),
  648. required=False,
  649. )
  650. model = ModuleType
  651. fieldsets = (
  652. FieldSet('profile', 'manufacturer', 'part_number', 'description', name=_('Module Type')),
  653. FieldSet(
  654. InlineFields('weight', 'max_weight', 'weight_unit', label=_('Weight')),
  655. name=_('Chassis')
  656. ),
  657. FieldSet('cooling_method', 'airflow', name=_('Cooling')),
  658. FieldSet('add_module_bay_types', 'remove_module_bay_types', name=_('Bay Types')),
  659. FieldSet('end_of_life', name=_('Lifecycle')),
  660. )
  661. nullable_fields = (
  662. 'part_number', 'weight', 'weight_unit', 'profile', 'end_of_life', 'description', 'comments', 'cooling_method',
  663. )
  664. class DeviceRoleBulkEditForm(NestedGroupModelBulkEditForm):
  665. parent = DynamicModelChoiceField(
  666. label=_('Parent'),
  667. queryset=DeviceRole.objects.all(),
  668. required=False,
  669. )
  670. color = ColorField(
  671. label=_('Color'),
  672. required=False
  673. )
  674. vm_role = forms.NullBooleanField(
  675. required=False,
  676. widget=BulkEditNullBooleanSelect,
  677. label=_('VM role')
  678. )
  679. config_template = DynamicModelChoiceField(
  680. label=_('Config template'),
  681. queryset=ConfigTemplate.objects.all(),
  682. required=False
  683. )
  684. model = DeviceRole
  685. fieldsets = (
  686. FieldSet('parent', 'color', 'vm_role', 'config_template', 'description'),
  687. )
  688. nullable_fields = ('parent', 'color', 'config_template', 'description', 'comments')
  689. class PlatformBulkEditForm(NestedGroupModelBulkEditForm):
  690. parent = DynamicModelChoiceField(
  691. label=_('Parent'),
  692. queryset=Platform.objects.all(),
  693. required=False,
  694. )
  695. manufacturer = DynamicModelChoiceField(
  696. label=_('Manufacturer'),
  697. queryset=Manufacturer.objects.all(),
  698. required=False
  699. )
  700. config_template = DynamicModelChoiceField(
  701. label=_('Config template'),
  702. queryset=ConfigTemplate.objects.all(),
  703. required=False
  704. )
  705. model = Platform
  706. fieldsets = (
  707. FieldSet('parent', 'manufacturer', 'config_template', 'description'),
  708. )
  709. nullable_fields = ('parent', 'manufacturer', 'config_template', 'description', 'comments')
  710. class DeviceBulkEditForm(PrimaryModelBulkEditForm):
  711. manufacturer = DynamicModelChoiceField(
  712. label=_('Manufacturer'),
  713. queryset=Manufacturer.objects.all(),
  714. required=False
  715. )
  716. device_type = DynamicModelChoiceField(
  717. label=_('Device type'),
  718. queryset=DeviceType.objects.all(),
  719. required=False,
  720. context={
  721. 'parent': 'manufacturer',
  722. },
  723. query_params={
  724. 'manufacturer_id': '$manufacturer'
  725. }
  726. )
  727. role = DynamicModelChoiceField(
  728. label=_('Device role'),
  729. queryset=DeviceRole.objects.all(),
  730. required=False
  731. )
  732. site = DynamicModelChoiceField(
  733. label=_('Site'),
  734. queryset=Site.objects.all(),
  735. required=False
  736. )
  737. location = DynamicModelChoiceField(
  738. label=_('Location'),
  739. queryset=Location.objects.all(),
  740. required=False,
  741. query_params={
  742. 'site_id': '$site'
  743. }
  744. )
  745. tenant = DynamicModelChoiceField(
  746. label=_('Tenant'),
  747. queryset=Tenant.objects.all(),
  748. required=False
  749. )
  750. platform = DynamicModelChoiceField(
  751. label=_('Platform'),
  752. queryset=Platform.objects.all(),
  753. required=False
  754. )
  755. status = ChoiceField(
  756. label=_('Status'),
  757. choices=add_blank_choice(DeviceStatusChoices),
  758. required=False
  759. )
  760. airflow = ChoiceField(
  761. label=_('Airflow'),
  762. choices=add_blank_choice(DeviceAirflowChoices),
  763. required=False
  764. )
  765. cooling_method = forms.ChoiceField(
  766. label=_('Cooling method'),
  767. choices=add_blank_choice(CoolingMethodChoices),
  768. required=False
  769. )
  770. serial = forms.CharField(
  771. max_length=50,
  772. required=False,
  773. label=_('Serial Number')
  774. )
  775. config_template = DynamicModelChoiceField(
  776. label=_('Config template'),
  777. queryset=ConfigTemplate.objects.all(),
  778. required=False
  779. )
  780. cluster = DynamicModelChoiceField(
  781. label=_('Cluster'),
  782. queryset=Cluster.objects.all(),
  783. required=False,
  784. query_params={
  785. 'site_id': ['$site', 'null']
  786. },
  787. )
  788. model = Device
  789. fieldsets = (
  790. FieldSet('role', 'status', 'tenant', 'platform', 'description', name=_('Device')),
  791. FieldSet('site', 'location', name=_('Location')),
  792. FieldSet('manufacturer', 'device_type', 'airflow', 'cooling_method', 'serial', name=_('Hardware')),
  793. FieldSet('config_template', name=_('Configuration')),
  794. FieldSet('cluster', name=_('Virtualization')),
  795. )
  796. nullable_fields = (
  797. 'location', 'tenant', 'platform', 'serial', 'airflow', 'cooling_method', 'description', 'cluster', 'comments',
  798. )
  799. class ModuleBulkEditForm(PrimaryModelBulkEditForm):
  800. manufacturer = DynamicModelChoiceField(
  801. label=_('Manufacturer'),
  802. queryset=Manufacturer.objects.all(),
  803. required=False
  804. )
  805. module_type = DynamicModelChoiceField(
  806. label=_('Module type'),
  807. queryset=ModuleType.objects.all(),
  808. required=False,
  809. query_params={
  810. 'manufacturer_id': '$manufacturer'
  811. },
  812. context={
  813. 'parent': 'manufacturer',
  814. }
  815. )
  816. status = ChoiceField(
  817. label=_('Status'),
  818. choices=add_blank_choice(ModuleStatusChoices),
  819. required=False,
  820. initial=''
  821. )
  822. serial = forms.CharField(
  823. max_length=50,
  824. required=False,
  825. label=_('Serial Number')
  826. )
  827. model = Module
  828. fieldsets = (
  829. FieldSet('manufacturer', 'module_type', 'status', 'serial', 'description'),
  830. )
  831. nullable_fields = ('serial', 'description', 'comments')
  832. class CableBundleBulkEditForm(PrimaryModelBulkEditForm):
  833. pk = forms.ModelMultipleChoiceField(
  834. queryset=CableBundle.objects.all(),
  835. widget=forms.MultipleHiddenInput
  836. )
  837. description = forms.CharField(
  838. label=_('Description'),
  839. max_length=200,
  840. required=False,
  841. )
  842. model = CableBundle
  843. fieldsets = (
  844. FieldSet('description',),
  845. )
  846. nullable_fields = ('description', 'comments')
  847. class CableBulkEditForm(PrimaryModelBulkEditForm):
  848. type = ChoiceField(
  849. label=_('Type'),
  850. choices=add_blank_choice(CableTypeChoices),
  851. required=False,
  852. initial=''
  853. )
  854. status = ChoiceField(
  855. label=_('Status'),
  856. choices=add_blank_choice(LinkStatusChoices),
  857. required=False,
  858. initial=''
  859. )
  860. profile = ChoiceField(
  861. label=_('Profile'),
  862. choices=add_blank_choice(CableProfileChoices),
  863. required=False,
  864. initial=''
  865. )
  866. tenant = DynamicModelChoiceField(
  867. label=_('Tenant'),
  868. queryset=Tenant.objects.all(),
  869. required=False
  870. )
  871. bundle = DynamicModelChoiceField(
  872. label=_('Bundle'),
  873. queryset=CableBundle.objects.all(),
  874. required=False,
  875. )
  876. label = forms.CharField(
  877. label=_('Label'),
  878. max_length=100,
  879. required=False
  880. )
  881. color = ColorField(
  882. label=_('Color'),
  883. required=False
  884. )
  885. length = forms.DecimalField(
  886. label=_('Length'),
  887. min_value=0,
  888. required=False
  889. )
  890. length_unit = ChoiceField(
  891. label=_('Length unit'),
  892. choices=add_blank_choice(CableLengthUnitChoices),
  893. required=False,
  894. initial=''
  895. )
  896. model = Cable
  897. fieldsets = (
  898. FieldSet('type', 'status', 'profile', 'tenant', 'bundle', 'label', 'description'),
  899. FieldSet('color', 'length', 'length_unit', name=_('Attributes')),
  900. )
  901. nullable_fields = (
  902. 'type', 'status', 'profile', 'tenant', 'bundle', 'label', 'color', 'length', 'description', 'comments',
  903. )
  904. class VirtualChassisBulkEditForm(PrimaryModelBulkEditForm):
  905. domain = forms.CharField(
  906. label=_('Domain'),
  907. max_length=30,
  908. required=False
  909. )
  910. model = VirtualChassis
  911. fieldsets = (
  912. FieldSet('domain', 'description'),
  913. )
  914. nullable_fields = ('domain', 'description', 'comments')
  915. class PowerPanelBulkEditForm(PrimaryModelBulkEditForm):
  916. region = DynamicModelChoiceField(
  917. label=_('Region'),
  918. queryset=Region.objects.all(),
  919. required=False,
  920. initial_params={
  921. 'sites': '$site'
  922. }
  923. )
  924. site_group = DynamicModelChoiceField(
  925. label=_('Site group'),
  926. queryset=SiteGroup.objects.all(),
  927. required=False,
  928. initial_params={
  929. 'sites': '$site'
  930. }
  931. )
  932. site = DynamicModelChoiceField(
  933. label=_('Site'),
  934. queryset=Site.objects.all(),
  935. required=False,
  936. query_params={
  937. 'region_id': '$region',
  938. 'group_id': '$site_group',
  939. }
  940. )
  941. location = DynamicModelChoiceField(
  942. label=_('Location'),
  943. queryset=Location.objects.all(),
  944. required=False,
  945. query_params={
  946. 'site_id': '$site'
  947. }
  948. )
  949. model = PowerPanel
  950. fieldsets = (
  951. FieldSet('region', 'site_group', 'site', 'location', 'description'),
  952. )
  953. nullable_fields = ('location', 'description', 'comments')
  954. class PowerFeedBulkEditForm(PrimaryModelBulkEditForm):
  955. power_panel = DynamicModelChoiceField(
  956. label=_('Power panel'),
  957. queryset=PowerPanel.objects.all(),
  958. required=False
  959. )
  960. rack = DynamicModelChoiceField(
  961. label=_('Rack'),
  962. queryset=Rack.objects.all(),
  963. required=False,
  964. )
  965. status = ChoiceField(
  966. label=_('Status'),
  967. choices=add_blank_choice(PowerFeedStatusChoices),
  968. required=False,
  969. initial=''
  970. )
  971. type = ChoiceField(
  972. label=_('Type'),
  973. choices=add_blank_choice(PowerFeedTypeChoices),
  974. required=False,
  975. initial=''
  976. )
  977. supply = ChoiceField(
  978. label=_('Supply'),
  979. choices=add_blank_choice(PowerFeedSupplyChoices),
  980. required=False,
  981. initial=''
  982. )
  983. phase = ChoiceField(
  984. label=_('Phase'),
  985. choices=add_blank_choice(PowerFeedPhaseChoices),
  986. required=False,
  987. initial=''
  988. )
  989. voltage = forms.IntegerField(
  990. label=_('Voltage'),
  991. required=False
  992. )
  993. amperage = forms.IntegerField(
  994. label=_('Amperage'),
  995. required=False
  996. )
  997. max_utilization = forms.IntegerField(
  998. label=_('Max utilization'),
  999. required=False
  1000. )
  1001. mark_connected = forms.NullBooleanField(
  1002. label=_('Mark connected'),
  1003. required=False,
  1004. widget=BulkEditNullBooleanSelect
  1005. )
  1006. tenant = DynamicModelChoiceField(
  1007. queryset=Tenant.objects.all(),
  1008. required=False
  1009. )
  1010. model = PowerFeed
  1011. fieldsets = (
  1012. FieldSet('power_panel', 'rack', 'status', 'type', 'mark_connected', 'description', 'tenant'),
  1013. FieldSet('supply', 'phase', 'voltage', 'amperage', 'max_utilization', name=_('Power'))
  1014. )
  1015. nullable_fields = ('rack', 'tenant', 'description', 'comments')
  1016. #
  1017. # Cooling
  1018. #
  1019. class CoolingSourceBulkEditForm(PrimaryModelBulkEditForm):
  1020. region = DynamicModelChoiceField(
  1021. label=_('Region'),
  1022. queryset=Region.objects.all(),
  1023. required=False,
  1024. initial_params={
  1025. 'sites': '$site'
  1026. }
  1027. )
  1028. site_group = DynamicModelChoiceField(
  1029. label=_('Site group'),
  1030. queryset=SiteGroup.objects.all(),
  1031. required=False,
  1032. initial_params={
  1033. 'sites': '$site'
  1034. }
  1035. )
  1036. site = DynamicModelChoiceField(
  1037. label=_('Site'),
  1038. queryset=Site.objects.all(),
  1039. required=False,
  1040. query_params={
  1041. 'region_id': '$region',
  1042. 'group_id': '$site_group',
  1043. }
  1044. )
  1045. location = DynamicModelChoiceField(
  1046. label=_('Location'),
  1047. queryset=Location.objects.all(),
  1048. required=False,
  1049. query_params={
  1050. 'site_id': '$site'
  1051. }
  1052. )
  1053. type = forms.ChoiceField(
  1054. label=_('Type'),
  1055. choices=add_blank_choice(CoolingSourceTypeChoices),
  1056. required=False,
  1057. initial=''
  1058. )
  1059. status = forms.ChoiceField(
  1060. label=_('Status'),
  1061. choices=add_blank_choice(CoolingSourceStatusChoices),
  1062. required=False,
  1063. initial=''
  1064. )
  1065. fluid_type = forms.ChoiceField(
  1066. label=_('Fluid type'),
  1067. choices=add_blank_choice(FluidTypeChoices),
  1068. required=False,
  1069. initial=''
  1070. )
  1071. cooling_capacity = forms.DecimalField(
  1072. label=_('Cooling capacity'),
  1073. min_value=0,
  1074. required=False
  1075. )
  1076. model = CoolingSource
  1077. fieldsets = (
  1078. FieldSet('region', 'site_group', 'site', 'location', 'type', 'status', 'description'),
  1079. FieldSet('fluid_type', 'cooling_capacity', name=_('Characteristics')),
  1080. )
  1081. nullable_fields = (
  1082. 'location', 'fluid_type', 'cooling_capacity', 'description', 'comments',
  1083. )
  1084. class CoolingFeedBulkEditForm(PrimaryModelBulkEditForm):
  1085. cooling_source = DynamicModelChoiceField(
  1086. label=_('Cooling source'),
  1087. queryset=CoolingSource.objects.all(),
  1088. required=False
  1089. )
  1090. rack = DynamicModelChoiceField(
  1091. label=_('Rack'),
  1092. queryset=Rack.objects.all(),
  1093. required=False,
  1094. )
  1095. status = forms.ChoiceField(
  1096. label=_('Status'),
  1097. choices=add_blank_choice(CoolingFeedStatusChoices),
  1098. required=False,
  1099. initial=''
  1100. )
  1101. cooling_capacity = forms.DecimalField(
  1102. label=_('Cooling capacity'),
  1103. min_value=0,
  1104. required=False
  1105. )
  1106. max_flow = forms.DecimalField(
  1107. label=_('Max flow'),
  1108. min_value=Decimal('0.01'),
  1109. required=False
  1110. )
  1111. max_flow_unit = forms.ChoiceField(
  1112. label=_('Max flow unit'),
  1113. choices=add_blank_choice(FlowRateUnitChoices),
  1114. required=False,
  1115. initial=''
  1116. )
  1117. tenant = DynamicModelChoiceField(
  1118. queryset=Tenant.objects.all(),
  1119. required=False
  1120. )
  1121. model = CoolingFeed
  1122. fieldsets = (
  1123. FieldSet('cooling_source', 'rack', 'status', 'description', 'tenant'),
  1124. FieldSet(
  1125. 'cooling_capacity', 'max_flow', 'max_flow_unit',
  1126. name=_('Characteristics')
  1127. ),
  1128. )
  1129. nullable_fields = (
  1130. 'rack', 'cooling_capacity', 'max_flow', 'max_flow_unit',
  1131. 'tenant', 'description', 'comments',
  1132. )
  1133. #
  1134. # Device component templates
  1135. #
  1136. class ComponentTemplateBulkEditForm(ChangelogMessageMixin, BulkEditForm):
  1137. pass
  1138. class ConsolePortTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1139. pk = forms.ModelMultipleChoiceField(
  1140. queryset=ConsolePortTemplate.objects.all(),
  1141. widget=forms.MultipleHiddenInput()
  1142. )
  1143. label = forms.CharField(
  1144. label=_('Label'),
  1145. max_length=64,
  1146. required=False
  1147. )
  1148. type = ChoiceField(
  1149. label=_('Type'),
  1150. choices=add_blank_choice(ConsolePortTypeChoices),
  1151. required=False
  1152. )
  1153. description = forms.CharField(
  1154. label=_('Description'),
  1155. required=False
  1156. )
  1157. nullable_fields = ('label', 'type', 'description')
  1158. class ConsoleServerPortTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1159. pk = forms.ModelMultipleChoiceField(
  1160. queryset=ConsoleServerPortTemplate.objects.all(),
  1161. widget=forms.MultipleHiddenInput()
  1162. )
  1163. label = forms.CharField(
  1164. label=_('Label'),
  1165. max_length=64,
  1166. required=False
  1167. )
  1168. type = ChoiceField(
  1169. label=_('Type'),
  1170. choices=add_blank_choice(ConsolePortTypeChoices),
  1171. required=False
  1172. )
  1173. description = forms.CharField(
  1174. label=_('Description'),
  1175. required=False
  1176. )
  1177. nullable_fields = ('label', 'type', 'description')
  1178. class PowerPortTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1179. pk = forms.ModelMultipleChoiceField(
  1180. queryset=PowerPortTemplate.objects.all(),
  1181. widget=forms.MultipleHiddenInput()
  1182. )
  1183. label = forms.CharField(
  1184. label=_('Label'),
  1185. max_length=64,
  1186. required=False
  1187. )
  1188. type = ChoiceField(
  1189. label=_('Type'),
  1190. choices=add_blank_choice(PowerPortTypeChoices),
  1191. required=False
  1192. )
  1193. maximum_draw = forms.IntegerField(
  1194. label=_('Maximum draw'),
  1195. min_value=1,
  1196. required=False,
  1197. help_text=_("Maximum power draw (watts)")
  1198. )
  1199. allocated_draw = forms.IntegerField(
  1200. label=_('Allocated draw'),
  1201. min_value=1,
  1202. required=False,
  1203. help_text=_("Allocated power draw (watts)")
  1204. )
  1205. description = forms.CharField(
  1206. label=_('Description'),
  1207. required=False
  1208. )
  1209. nullable_fields = ('label', 'type', 'maximum_draw', 'allocated_draw', 'description')
  1210. class PowerOutletTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1211. pk = forms.ModelMultipleChoiceField(
  1212. queryset=PowerOutletTemplate.objects.all(),
  1213. widget=forms.MultipleHiddenInput()
  1214. )
  1215. device_type = forms.ModelChoiceField(
  1216. label=_('Device type'),
  1217. queryset=DeviceType.objects.all(),
  1218. required=False,
  1219. disabled=True,
  1220. widget=forms.HiddenInput()
  1221. )
  1222. label = forms.CharField(
  1223. label=_('Label'),
  1224. max_length=64,
  1225. required=False
  1226. )
  1227. type = ChoiceField(
  1228. label=_('Type'),
  1229. choices=add_blank_choice(PowerOutletTypeChoices),
  1230. required=False
  1231. )
  1232. color = ColorField(
  1233. label=_('Color'),
  1234. required=False
  1235. )
  1236. power_port = forms.ModelChoiceField(
  1237. label=_('Power port'),
  1238. queryset=PowerPortTemplate.objects.all(),
  1239. required=False
  1240. )
  1241. feed_leg = ChoiceField(
  1242. label=_('Feed leg'),
  1243. choices=add_blank_choice(PowerOutletFeedLegChoices),
  1244. required=False
  1245. )
  1246. description = forms.CharField(
  1247. label=_('Description'),
  1248. required=False
  1249. )
  1250. nullable_fields = ('label', 'type', 'power_port', 'feed_leg', 'description')
  1251. def __init__(self, *args, **kwargs):
  1252. super().__init__(*args, **kwargs)
  1253. # Limit power_port queryset to PowerPortTemplates which belong to the parent DeviceType
  1254. if 'device_type' in self.initial:
  1255. device_type = DeviceType.objects.filter(pk=self.initial['device_type']).first()
  1256. self.fields['power_port'].queryset = PowerPortTemplate.objects.filter(device_type=device_type)
  1257. else:
  1258. self.fields['power_port'].choices = ()
  1259. self.fields['power_port'].widget.attrs['disabled'] = True
  1260. class CoolingIntakeTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1261. pk = forms.ModelMultipleChoiceField(
  1262. queryset=CoolingIntakeTemplate.objects.all(),
  1263. widget=forms.MultipleHiddenInput()
  1264. )
  1265. label = forms.CharField(
  1266. label=_('Label'),
  1267. max_length=64,
  1268. required=False
  1269. )
  1270. type = forms.ChoiceField(
  1271. label=_('Type'),
  1272. choices=add_blank_choice(CoolingConnectorTypeChoices),
  1273. required=False
  1274. )
  1275. diameter = forms.DecimalField(
  1276. label=_('Diameter'),
  1277. min_value=Decimal('0.01'),
  1278. required=False
  1279. )
  1280. diameter_unit = forms.ChoiceField(
  1281. label=_('Diameter unit'),
  1282. choices=add_blank_choice(DiameterUnitChoices),
  1283. required=False
  1284. )
  1285. max_flow = forms.DecimalField(
  1286. label=_('Max flow'),
  1287. min_value=Decimal('0.01'),
  1288. required=False
  1289. )
  1290. max_flow_unit = forms.ChoiceField(
  1291. label=_('Max flow unit'),
  1292. choices=add_blank_choice(FlowRateUnitChoices),
  1293. required=False
  1294. )
  1295. description = forms.CharField(
  1296. label=_('Description'),
  1297. required=False
  1298. )
  1299. fieldsets = (
  1300. FieldSet(
  1301. 'label', 'type',
  1302. InlineFields('diameter', 'diameter_unit', label=_('Diameter')),
  1303. InlineFields('max_flow', 'max_flow_unit', label=_('Max flow')),
  1304. 'description',
  1305. ),
  1306. )
  1307. nullable_fields = (
  1308. 'label', 'type', 'diameter', 'diameter_unit', 'max_flow', 'max_flow_unit',
  1309. 'description',
  1310. )
  1311. class CoolingOutflowTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1312. pk = forms.ModelMultipleChoiceField(
  1313. queryset=CoolingOutflowTemplate.objects.all(),
  1314. widget=forms.MultipleHiddenInput()
  1315. )
  1316. device_type = forms.ModelChoiceField(
  1317. label=_('Device type'),
  1318. queryset=DeviceType.objects.all(),
  1319. required=False,
  1320. disabled=True,
  1321. widget=forms.HiddenInput()
  1322. )
  1323. label = forms.CharField(
  1324. label=_('Label'),
  1325. max_length=64,
  1326. required=False
  1327. )
  1328. type = forms.ChoiceField(
  1329. label=_('Type'),
  1330. choices=add_blank_choice(CoolingConnectorTypeChoices),
  1331. required=False
  1332. )
  1333. diameter = forms.DecimalField(
  1334. label=_('Diameter'),
  1335. min_value=Decimal('0.01'),
  1336. required=False
  1337. )
  1338. diameter_unit = forms.ChoiceField(
  1339. label=_('Diameter unit'),
  1340. choices=add_blank_choice(DiameterUnitChoices),
  1341. required=False
  1342. )
  1343. cooling_intake = forms.ModelChoiceField(
  1344. label=_('Cooling intake'),
  1345. queryset=CoolingIntakeTemplate.objects.all(),
  1346. required=False
  1347. )
  1348. description = forms.CharField(
  1349. label=_('Description'),
  1350. required=False
  1351. )
  1352. fieldsets = (
  1353. FieldSet(
  1354. 'label', 'type',
  1355. InlineFields('diameter', 'diameter_unit', label=_('Diameter')),
  1356. 'cooling_intake', 'description',
  1357. ),
  1358. )
  1359. nullable_fields = (
  1360. 'label', 'type', 'diameter', 'diameter_unit', 'cooling_intake', 'description',
  1361. )
  1362. def __init__(self, *args, **kwargs):
  1363. super().__init__(*args, **kwargs)
  1364. # Limit cooling_intake queryset to CoolingIntakeTemplates which belong to the parent DeviceType
  1365. if 'device_type' in self.initial:
  1366. device_type = DeviceType.objects.filter(pk=self.initial['device_type']).first()
  1367. self.fields['cooling_intake'].queryset = CoolingIntakeTemplate.objects.filter(device_type=device_type)
  1368. else:
  1369. self.fields['cooling_intake'].choices = ()
  1370. self.fields['cooling_intake'].widget.attrs['disabled'] = True
  1371. class InterfaceTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1372. pk = forms.ModelMultipleChoiceField(
  1373. queryset=InterfaceTemplate.objects.all(),
  1374. widget=forms.MultipleHiddenInput()
  1375. )
  1376. label = forms.CharField(
  1377. label=_('Label'),
  1378. max_length=64,
  1379. required=False
  1380. )
  1381. type = ChoiceField(
  1382. label=_('Type'),
  1383. choices=add_blank_choice(InterfaceTypeChoices),
  1384. required=False
  1385. )
  1386. channels = forms.IntegerField(
  1387. label=_('Channels'),
  1388. required=False
  1389. )
  1390. channel_id = forms.IntegerField(
  1391. label=_('Channel ID'),
  1392. required=False
  1393. )
  1394. enabled = forms.NullBooleanField(
  1395. label=_('Enabled'),
  1396. required=False,
  1397. widget=BulkEditNullBooleanSelect
  1398. )
  1399. mgmt_only = forms.NullBooleanField(
  1400. required=False,
  1401. widget=BulkEditNullBooleanSelect,
  1402. label=_('Management only')
  1403. )
  1404. description = forms.CharField(
  1405. label=_('Description'),
  1406. required=False
  1407. )
  1408. poe_mode = ChoiceField(
  1409. choices=add_blank_choice(InterfacePoEModeChoices),
  1410. required=False,
  1411. initial='',
  1412. label=_('PoE mode')
  1413. )
  1414. poe_type = ChoiceField(
  1415. choices=add_blank_choice(InterfacePoETypeChoices),
  1416. required=False,
  1417. initial='',
  1418. label=_('PoE type')
  1419. )
  1420. rf_role = ChoiceField(
  1421. choices=add_blank_choice(WirelessRoleChoices),
  1422. required=False,
  1423. initial='',
  1424. label=_('Wireless role')
  1425. )
  1426. nullable_fields = ('label', 'channels', 'channel_id', 'description', 'poe_mode', 'poe_type', 'rf_role')
  1427. class FrontPortTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1428. pk = forms.ModelMultipleChoiceField(
  1429. queryset=FrontPortTemplate.objects.all(),
  1430. widget=forms.MultipleHiddenInput()
  1431. )
  1432. label = forms.CharField(
  1433. label=_('Label'),
  1434. max_length=64,
  1435. required=False
  1436. )
  1437. type = ChoiceField(
  1438. label=_('Type'),
  1439. choices=add_blank_choice(PortTypeChoices),
  1440. required=False
  1441. )
  1442. color = ColorField(
  1443. label=_('Color'),
  1444. required=False
  1445. )
  1446. description = forms.CharField(
  1447. label=_('Description'),
  1448. required=False
  1449. )
  1450. nullable_fields = ('description',)
  1451. class RearPortTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1452. pk = forms.ModelMultipleChoiceField(
  1453. queryset=RearPortTemplate.objects.all(),
  1454. widget=forms.MultipleHiddenInput()
  1455. )
  1456. label = forms.CharField(
  1457. label=_('Label'),
  1458. max_length=64,
  1459. required=False
  1460. )
  1461. type = ChoiceField(
  1462. label=_('Type'),
  1463. choices=add_blank_choice(PortTypeChoices),
  1464. required=False
  1465. )
  1466. color = ColorField(
  1467. label=_('Color'),
  1468. required=False
  1469. )
  1470. description = forms.CharField(
  1471. label=_('Description'),
  1472. required=False
  1473. )
  1474. nullable_fields = ('description',)
  1475. class ModuleBayTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1476. pk = forms.ModelMultipleChoiceField(
  1477. queryset=ModuleBayTemplate.objects.all(),
  1478. widget=forms.MultipleHiddenInput()
  1479. )
  1480. label = forms.CharField(
  1481. label=_('Label'),
  1482. max_length=64,
  1483. required=False
  1484. )
  1485. position = forms.CharField(
  1486. label=_('Position'),
  1487. max_length=30,
  1488. required=False
  1489. )
  1490. description = forms.CharField(
  1491. label=_('Description'),
  1492. required=False
  1493. )
  1494. enabled = forms.NullBooleanField(
  1495. label=_('Enabled'),
  1496. required=False,
  1497. widget=BulkEditNullBooleanSelect,
  1498. )
  1499. add_module_bay_types = DynamicModelMultipleChoiceField(
  1500. label=_('Add bay types'),
  1501. queryset=ModuleBayType.objects.all(),
  1502. required=False,
  1503. )
  1504. remove_module_bay_types = DynamicModelMultipleChoiceField(
  1505. label=_('Remove bay types'),
  1506. queryset=ModuleBayType.objects.all(),
  1507. required=False,
  1508. )
  1509. fieldsets = (
  1510. FieldSet('label', 'enabled', 'description'),
  1511. FieldSet('add_module_bay_types', 'remove_module_bay_types', name=_('Bay Types')),
  1512. )
  1513. nullable_fields = ('label', 'position', 'description')
  1514. class DeviceBayTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1515. pk = forms.ModelMultipleChoiceField(
  1516. queryset=DeviceBayTemplate.objects.all(),
  1517. widget=forms.MultipleHiddenInput()
  1518. )
  1519. label = forms.CharField(
  1520. label=_('Label'),
  1521. max_length=64,
  1522. required=False
  1523. )
  1524. description = forms.CharField(
  1525. label=_('Description'),
  1526. required=False
  1527. )
  1528. enabled = forms.NullBooleanField(
  1529. label=_('Enabled'),
  1530. required=False,
  1531. widget=BulkEditNullBooleanSelect,
  1532. )
  1533. nullable_fields = ('label', 'description')
  1534. class InventoryItemTemplateBulkEditForm(ComponentTemplateBulkEditForm):
  1535. pk = forms.ModelMultipleChoiceField(
  1536. queryset=InventoryItemTemplate.objects.all(),
  1537. widget=forms.MultipleHiddenInput()
  1538. )
  1539. label = forms.CharField(
  1540. label=_('Label'),
  1541. max_length=64,
  1542. required=False
  1543. )
  1544. description = forms.CharField(
  1545. label=_('Description'),
  1546. required=False
  1547. )
  1548. role = DynamicModelChoiceField(
  1549. label=_('Role'),
  1550. queryset=InventoryItemRole.objects.all(),
  1551. required=False
  1552. )
  1553. manufacturer = DynamicModelChoiceField(
  1554. label=_('Manufacturer'),
  1555. queryset=Manufacturer.objects.all(),
  1556. required=False
  1557. )
  1558. part_id = forms.CharField(
  1559. label=_('Part ID'),
  1560. max_length=50,
  1561. required=False
  1562. )
  1563. nullable_fields = ('label', 'role', 'manufacturer', 'part_id', 'description')
  1564. #
  1565. # Device components
  1566. #
  1567. class ComponentBulkEditForm(OwnerMixin, NetBoxModelBulkEditForm):
  1568. device = forms.ModelChoiceField(
  1569. label=_('Device'),
  1570. queryset=Device.objects.all(),
  1571. required=False,
  1572. disabled=True,
  1573. widget=forms.HiddenInput()
  1574. )
  1575. module = forms.ModelChoiceField(
  1576. label=_('Module'),
  1577. queryset=Module.objects.all(),
  1578. required=False
  1579. )
  1580. def __init__(self, *args, initial=None, **kwargs):
  1581. try:
  1582. self.device_id = int(initial.get('device'))
  1583. except (TypeError, ValueError):
  1584. self.device_id = None
  1585. super().__init__(*args, initial=initial, **kwargs)
  1586. # Limit module queryset to Modules which belong to the parent Device
  1587. if self.device_id:
  1588. device = Device.objects.filter(pk=self.device_id).first()
  1589. self.fields['module'].queryset = Module.objects.filter(device=device)
  1590. else:
  1591. self.fields['module'].choices = ()
  1592. self.fields['module'].widget.attrs['disabled'] = True
  1593. class ConsolePortBulkEditForm(
  1594. ComponentBulkEditForm,
  1595. form_from_model(ConsolePort, ['label', 'type', 'speed', 'mark_connected', 'description'])
  1596. ):
  1597. type = ChoiceField(
  1598. label=_('Type'),
  1599. choices=add_blank_choice(ConsolePortTypeChoices),
  1600. required=False
  1601. )
  1602. mark_connected = forms.NullBooleanField(
  1603. label=_('Mark connected'),
  1604. required=False,
  1605. widget=BulkEditNullBooleanSelect
  1606. )
  1607. model = ConsolePort
  1608. fieldsets = (
  1609. FieldSet('module', 'type', 'label', 'speed', 'description', 'mark_connected'),
  1610. )
  1611. nullable_fields = ('module', 'label', 'description')
  1612. class ConsoleServerPortBulkEditForm(
  1613. ComponentBulkEditForm,
  1614. form_from_model(ConsoleServerPort, ['label', 'type', 'speed', 'mark_connected', 'description'])
  1615. ):
  1616. type = ChoiceField(
  1617. label=_('Type'),
  1618. choices=add_blank_choice(ConsolePortTypeChoices),
  1619. required=False
  1620. )
  1621. mark_connected = forms.NullBooleanField(
  1622. label=_('Mark connected'),
  1623. required=False,
  1624. widget=BulkEditNullBooleanSelect
  1625. )
  1626. model = ConsoleServerPort
  1627. fieldsets = (
  1628. FieldSet('module', 'type', 'label', 'speed', 'description', 'mark_connected'),
  1629. )
  1630. nullable_fields = ('module', 'label', 'description')
  1631. class PowerPortBulkEditForm(
  1632. ComponentBulkEditForm,
  1633. form_from_model(PowerPort, ['label', 'type', 'maximum_draw', 'allocated_draw', 'mark_connected', 'description'])
  1634. ):
  1635. type = ChoiceField(
  1636. label=_('Type'),
  1637. choices=add_blank_choice(PowerPortTypeChoices),
  1638. required=False
  1639. )
  1640. mark_connected = forms.NullBooleanField(
  1641. label=_('Mark connected'),
  1642. required=False,
  1643. widget=BulkEditNullBooleanSelect
  1644. )
  1645. model = PowerPort
  1646. fieldsets = (
  1647. FieldSet('module', 'type', 'label', 'description', 'mark_connected'),
  1648. FieldSet('maximum_draw', 'allocated_draw', name=_('Power')),
  1649. )
  1650. nullable_fields = ('module', 'label', 'description', 'maximum_draw', 'allocated_draw')
  1651. class PowerOutletBulkEditForm(
  1652. ComponentBulkEditForm,
  1653. form_from_model(
  1654. PowerOutlet,
  1655. ['label', 'type', 'status', 'color', 'feed_leg', 'power_port', 'mark_connected', 'description']
  1656. )
  1657. ):
  1658. type = ChoiceField(
  1659. label=_('Type'),
  1660. choices=add_blank_choice(PowerOutletTypeChoices),
  1661. required=False
  1662. )
  1663. mark_connected = forms.NullBooleanField(
  1664. label=_('Mark connected'),
  1665. required=False,
  1666. widget=BulkEditNullBooleanSelect
  1667. )
  1668. model = PowerOutlet
  1669. fieldsets = (
  1670. FieldSet('module', 'type', 'label', 'status', 'description', 'mark_connected', 'color'),
  1671. FieldSet('feed_leg', 'power_port', name=_('Power')),
  1672. )
  1673. nullable_fields = ('module', 'label', 'type', 'feed_leg', 'power_port', 'description')
  1674. def __init__(self, *args, **kwargs):
  1675. super().__init__(*args, **kwargs)
  1676. # Limit power_port queryset to PowerPorts which belong to the parent Device
  1677. if self.device_id:
  1678. device = Device.objects.filter(pk=self.device_id).first()
  1679. self.fields['power_port'].queryset = PowerPort.objects.filter(device=device)
  1680. else:
  1681. self.fields['power_port'].choices = ()
  1682. self.fields['power_port'].widget.attrs['disabled'] = True
  1683. class CoolingIntakeBulkEditForm(
  1684. ComponentBulkEditForm,
  1685. form_from_model(CoolingIntake, [
  1686. 'label', 'type', 'diameter', 'diameter_unit', 'max_flow', 'max_flow_unit',
  1687. 'description'
  1688. ])
  1689. ):
  1690. cooling_outflow = DynamicModelChoiceField(
  1691. label=_('Cooling outflow'),
  1692. queryset=CoolingOutflow.objects.all(),
  1693. required=False
  1694. )
  1695. model = CoolingIntake
  1696. fieldsets = (
  1697. FieldSet(
  1698. 'module', 'type',
  1699. InlineFields('diameter', 'diameter_unit', label=_('Diameter')),
  1700. 'label', 'cooling_outflow', 'description',
  1701. ),
  1702. FieldSet(
  1703. InlineFields('max_flow', 'max_flow_unit', label=_('Max flow')),
  1704. name=_('Characteristics')
  1705. ),
  1706. )
  1707. nullable_fields = (
  1708. 'module', 'label', 'type', 'diameter', 'diameter_unit', 'max_flow', 'max_flow_unit',
  1709. 'cooling_outflow', 'description',
  1710. )
  1711. class CoolingOutflowBulkEditForm(
  1712. ComponentBulkEditForm,
  1713. form_from_model(
  1714. CoolingOutflow,
  1715. [
  1716. 'label', 'type', 'diameter', 'diameter_unit', 'cooling_intake',
  1717. 'description'
  1718. ]
  1719. )
  1720. ):
  1721. model = CoolingOutflow
  1722. fieldsets = (
  1723. FieldSet(
  1724. 'module', 'type',
  1725. InlineFields('diameter', 'diameter_unit', label=_('Diameter')),
  1726. 'label', 'description', 'cooling_intake',
  1727. ),
  1728. )
  1729. nullable_fields = (
  1730. 'module', 'label', 'type', 'diameter', 'diameter_unit', 'cooling_intake', 'description',
  1731. )
  1732. def __init__(self, *args, **kwargs):
  1733. super().__init__(*args, **kwargs)
  1734. # Limit cooling_intake queryset to CoolingIntakes which belong to the parent Device
  1735. if self.device_id:
  1736. device = Device.objects.filter(pk=self.device_id).first()
  1737. self.fields['cooling_intake'].queryset = CoolingIntake.objects.filter(device=device)
  1738. else:
  1739. self.fields['cooling_intake'].choices = ()
  1740. self.fields['cooling_intake'].widget.attrs['disabled'] = True
  1741. class InterfaceBulkEditForm(
  1742. ComponentBulkEditForm,
  1743. form_from_model(Interface, [
  1744. 'label', 'type', 'channels', 'channel_id', 'parent', 'bridge', 'lag', 'speed', 'duplex', 'wwn', 'mtu',
  1745. 'mgmt_only', 'mark_connected', 'description', 'mode', 'rf_role', 'rf_channel', 'rf_channel_frequency',
  1746. 'rf_channel_width', 'tx_power', 'wireless_lans', 'vlan_translation_policy'
  1747. ])
  1748. ):
  1749. enabled = forms.NullBooleanField(
  1750. label=_('Enabled'),
  1751. required=False,
  1752. widget=BulkEditNullBooleanSelect
  1753. )
  1754. parent = DynamicModelChoiceField(
  1755. label=_('Parent'),
  1756. queryset=Interface.objects.all(),
  1757. required=False,
  1758. query_params={
  1759. 'virtual_chassis_member_id': '$device',
  1760. }
  1761. )
  1762. bridge = DynamicModelChoiceField(
  1763. label=_('Bridge'),
  1764. queryset=Interface.objects.all(),
  1765. required=False,
  1766. query_params={
  1767. 'virtual_chassis_member_id': '$device',
  1768. }
  1769. )
  1770. lag = DynamicModelChoiceField(
  1771. queryset=Interface.objects.all(),
  1772. required=False,
  1773. query_params={
  1774. 'type': 'lag',
  1775. 'virtual_chassis_member_id': '$device',
  1776. },
  1777. label=_('LAG')
  1778. )
  1779. vdcs = DynamicModelMultipleChoiceField(
  1780. queryset=VirtualDeviceContext.objects.all(),
  1781. required=False,
  1782. label=_('Virtual device contexts'),
  1783. query_params={
  1784. 'device_id': '$device',
  1785. }
  1786. )
  1787. speed = PositiveBigIntegerField(
  1788. label=_('Speed'),
  1789. required=False,
  1790. widget=NumberWithOptions(
  1791. options=InterfaceSpeedChoices
  1792. )
  1793. )
  1794. mgmt_only = forms.NullBooleanField(
  1795. required=False,
  1796. widget=BulkEditNullBooleanSelect,
  1797. label=_('Management only')
  1798. )
  1799. poe_mode = ChoiceField(
  1800. choices=add_blank_choice(InterfacePoEModeChoices),
  1801. required=False,
  1802. initial='',
  1803. label=_('PoE mode')
  1804. )
  1805. poe_type = ChoiceField(
  1806. choices=add_blank_choice(InterfacePoETypeChoices),
  1807. required=False,
  1808. initial='',
  1809. label=_('PoE type')
  1810. )
  1811. mark_connected = forms.NullBooleanField(
  1812. label=_('Mark connected'),
  1813. required=False,
  1814. widget=BulkEditNullBooleanSelect
  1815. )
  1816. mode = ChoiceField(
  1817. label=_('Mode'),
  1818. choices=add_blank_choice(InterfaceModeChoices),
  1819. required=False,
  1820. initial=''
  1821. )
  1822. vlan_group = DynamicModelChoiceField(
  1823. queryset=VLANGroup.objects.all(),
  1824. required=False,
  1825. label=_('VLAN group')
  1826. )
  1827. untagged_vlan = DynamicModelChoiceField(
  1828. queryset=VLAN.objects.all(),
  1829. required=False,
  1830. query_params={
  1831. 'group_id': '$vlan_group',
  1832. 'available_on_device': '$device',
  1833. },
  1834. label=_('Untagged VLAN')
  1835. )
  1836. tagged_vlans = DynamicModelMultipleChoiceField(
  1837. queryset=VLAN.objects.all(),
  1838. required=False,
  1839. query_params={
  1840. 'group_id': '$vlan_group',
  1841. 'available_on_device': '$device',
  1842. },
  1843. label=_('Tagged VLANs')
  1844. )
  1845. add_tagged_vlans = DynamicModelMultipleChoiceField(
  1846. label=_('Add tagged VLANs'),
  1847. queryset=VLAN.objects.all(),
  1848. required=False,
  1849. query_params={
  1850. 'group_id': '$vlan_group',
  1851. 'available_on_device': '$device',
  1852. },
  1853. )
  1854. remove_tagged_vlans = DynamicModelMultipleChoiceField(
  1855. label=_('Remove tagged VLANs'),
  1856. queryset=VLAN.objects.all(),
  1857. required=False,
  1858. query_params={
  1859. 'group_id': '$vlan_group',
  1860. 'available_on_device': '$device',
  1861. }
  1862. )
  1863. qinq_svlan = DynamicModelChoiceField(
  1864. queryset=VLAN.objects.all(),
  1865. required=False,
  1866. label=_('Q-in-Q Service VLAN'),
  1867. query_params={
  1868. 'group_id': '$vlan_group',
  1869. 'available_on_device': '$device',
  1870. 'qinq_role': VLANQinQRoleChoices.ROLE_SERVICE,
  1871. }
  1872. )
  1873. vrf = DynamicModelChoiceField(
  1874. queryset=VRF.objects.all(),
  1875. required=False,
  1876. label=_('VRF')
  1877. )
  1878. wireless_lan_group = DynamicModelChoiceField(
  1879. queryset=WirelessLANGroup.objects.all(),
  1880. required=False,
  1881. label=_('Wireless LAN group')
  1882. )
  1883. wireless_lans = DynamicModelMultipleChoiceField(
  1884. queryset=WirelessLAN.objects.all(),
  1885. required=False,
  1886. label=_('Wireless LANs'),
  1887. query_params={
  1888. 'group_id': '$wireless_lan_group',
  1889. }
  1890. )
  1891. model = Interface
  1892. fieldsets = (
  1893. FieldSet('module', 'type', 'channels', 'label', 'speed', 'duplex', 'description'),
  1894. FieldSet('vrf', 'wwn', name=_('Addressing')),
  1895. FieldSet('vdcs', 'mtu', 'tx_power', 'enabled', 'mgmt_only', 'mark_connected', name=_('Operation')),
  1896. FieldSet('poe_mode', 'poe_type', name=_('PoE')),
  1897. FieldSet('parent', 'channel_id', 'bridge', 'lag', name=_('Related Interfaces')),
  1898. FieldSet(
  1899. 'mode', 'vlan_group', 'untagged_vlan', 'qinq_svlan', 'vlan_translation_policy', name=_('802.1Q Switching')
  1900. ),
  1901. FieldSet(
  1902. TabbedGroups(
  1903. FieldSet('tagged_vlans', name=_('Assignment')),
  1904. FieldSet('add_tagged_vlans', 'remove_tagged_vlans', name=_('Add/Remove')),
  1905. ),
  1906. ),
  1907. FieldSet(
  1908. 'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'wireless_lan_group', 'wireless_lans',
  1909. name=_('Wireless')
  1910. ),
  1911. )
  1912. nullable_fields = (
  1913. 'module', 'label', 'channels', 'channel_id', 'parent', 'bridge', 'lag', 'speed', 'duplex', 'wwn', 'vdcs',
  1914. 'mtu', 'description', 'poe_mode', 'poe_type', 'mode', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width',
  1915. 'tx_power', 'untagged_vlan', 'tagged_vlans', 'qinq_svlan', 'vrf', 'wireless_lans', 'vlan_translation_policy',
  1916. )
  1917. def __init__(self, *args, **kwargs):
  1918. super().__init__(*args, **kwargs)
  1919. if not self.device_id:
  1920. # See #4523
  1921. if 'pk' in self.initial:
  1922. site = None
  1923. interfaces = Interface.objects.filter(pk__in=self.initial['pk']).prefetch_related('device__site')
  1924. # Check interface sites. First interface should set site, further interfaces will either continue the
  1925. # loop or reset back to no site and break the loop.
  1926. for interface in interfaces:
  1927. if site is None:
  1928. site = interface.device.site
  1929. elif interface.device.site is not site:
  1930. site = None
  1931. break
  1932. if site is not None:
  1933. # Query for VLANs assigned to the same site and VLANs with no site assigned (null).
  1934. self.fields['untagged_vlan'].widget.add_query_param(
  1935. 'site_id', [site.pk, settings.FILTERS_NULL_CHOICE_VALUE]
  1936. )
  1937. self.fields['tagged_vlans'].widget.add_query_param(
  1938. 'site_id', [site.pk, settings.FILTERS_NULL_CHOICE_VALUE]
  1939. )
  1940. self.fields['add_tagged_vlans'].widget.add_query_param(
  1941. 'site_id', [site.pk, settings.FILTERS_NULL_CHOICE_VALUE]
  1942. )
  1943. self.fields['remove_tagged_vlans'].widget.add_query_param(
  1944. 'site_id', [site.pk, settings.FILTERS_NULL_CHOICE_VALUE]
  1945. )
  1946. self.fields['parent'].choices = ()
  1947. self.fields['parent'].widget.attrs['disabled'] = True
  1948. self.fields['bridge'].choices = ()
  1949. self.fields['bridge'].widget.attrs['disabled'] = True
  1950. self.fields['lag'].choices = ()
  1951. self.fields['lag'].widget.attrs['disabled'] = True
  1952. def clean(self):
  1953. super().clean()
  1954. if not self.cleaned_data['mode']:
  1955. if self.cleaned_data['untagged_vlan']:
  1956. raise forms.ValidationError({'untagged_vlan': _("Interface mode must be specified to assign VLANs")})
  1957. if self.cleaned_data['tagged_vlans']:
  1958. raise forms.ValidationError({'tagged_vlans': _("Interface mode must be specified to assign VLANs")})
  1959. # Untagged interfaces cannot be assigned tagged VLANs
  1960. elif self.cleaned_data['mode'] == InterfaceModeChoices.MODE_ACCESS and self.cleaned_data['tagged_vlans']:
  1961. raise forms.ValidationError({
  1962. 'mode': _("An access interface cannot have tagged VLANs assigned.")
  1963. })
  1964. # Remove all tagged VLAN assignments from "tagged all" interfaces
  1965. elif self.cleaned_data['mode'] == InterfaceModeChoices.MODE_TAGGED_ALL:
  1966. self.cleaned_data['tagged_vlans'] = []
  1967. class FrontPortBulkEditForm(
  1968. ComponentBulkEditForm,
  1969. form_from_model(FrontPort, ['label', 'type', 'color', 'mark_connected', 'description'])
  1970. ):
  1971. type = ChoiceField(
  1972. label=_('Type'),
  1973. choices=add_blank_choice(PortTypeChoices),
  1974. required=False
  1975. )
  1976. mark_connected = forms.NullBooleanField(
  1977. label=_('Mark connected'),
  1978. required=False,
  1979. widget=BulkEditNullBooleanSelect
  1980. )
  1981. model = FrontPort
  1982. fieldsets = (
  1983. FieldSet('module', 'type', 'label', 'color', 'description', 'mark_connected'),
  1984. )
  1985. nullable_fields = ('module', 'label', 'description', 'color')
  1986. class RearPortBulkEditForm(
  1987. ComponentBulkEditForm,
  1988. form_from_model(RearPort, ['label', 'type', 'color', 'mark_connected', 'description'])
  1989. ):
  1990. type = ChoiceField(
  1991. label=_('Type'),
  1992. choices=add_blank_choice(PortTypeChoices),
  1993. required=False
  1994. )
  1995. mark_connected = forms.NullBooleanField(
  1996. label=_('Mark connected'),
  1997. required=False,
  1998. widget=BulkEditNullBooleanSelect
  1999. )
  2000. model = RearPort
  2001. fieldsets = (
  2002. FieldSet('module', 'type', 'label', 'color', 'description', 'mark_connected'),
  2003. )
  2004. nullable_fields = ('module', 'label', 'description', 'color')
  2005. class ModuleBayBulkEditForm(
  2006. form_from_model(ModuleBay, ['label', 'position', 'description']),
  2007. NetBoxModelBulkEditForm
  2008. ):
  2009. enabled = forms.NullBooleanField(
  2010. label=_('Enabled'),
  2011. required=False,
  2012. widget=BulkEditNullBooleanSelect,
  2013. )
  2014. add_module_bay_types = DynamicModelMultipleChoiceField(
  2015. label=_('Add bay types'),
  2016. queryset=ModuleBayType.objects.all(),
  2017. required=False,
  2018. )
  2019. remove_module_bay_types = DynamicModelMultipleChoiceField(
  2020. label=_('Remove bay types'),
  2021. queryset=ModuleBayType.objects.all(),
  2022. required=False,
  2023. )
  2024. model = ModuleBay
  2025. fieldsets = (
  2026. FieldSet('label', 'position', 'enabled', 'description'),
  2027. FieldSet('add_module_bay_types', 'remove_module_bay_types', name=_('Bay Types')),
  2028. )
  2029. nullable_fields = ('label', 'position', 'description')
  2030. class DeviceBayBulkEditForm(
  2031. form_from_model(DeviceBay, ['label', 'description']),
  2032. NetBoxModelBulkEditForm
  2033. ):
  2034. enabled = forms.NullBooleanField(
  2035. label=_('Enabled'),
  2036. required=False,
  2037. widget=BulkEditNullBooleanSelect,
  2038. )
  2039. model = DeviceBay
  2040. fieldsets = (
  2041. FieldSet('label', 'enabled', 'description'),
  2042. )
  2043. nullable_fields = ('label', 'description')
  2044. class InventoryItemBulkEditForm(
  2045. form_from_model(InventoryItem, ['label', 'role', 'manufacturer', 'part_id', 'description']),
  2046. NetBoxModelBulkEditForm
  2047. ):
  2048. device = DynamicModelChoiceField(
  2049. label=_('Device'),
  2050. queryset=Device.objects.all(),
  2051. required=False
  2052. )
  2053. role = DynamicModelChoiceField(
  2054. label=_('Role'),
  2055. queryset=InventoryItemRole.objects.all(),
  2056. required=False
  2057. )
  2058. manufacturer = DynamicModelChoiceField(
  2059. label=_('Manufacturer'),
  2060. queryset=Manufacturer.objects.all(),
  2061. required=False
  2062. )
  2063. status = ChoiceField(
  2064. label=_('Status'),
  2065. choices=add_blank_choice(InventoryItemStatusChoices),
  2066. required=False,
  2067. initial=''
  2068. )
  2069. model = InventoryItem
  2070. fieldsets = (
  2071. FieldSet('device', 'label', 'role', 'manufacturer', 'part_id', 'status', 'description'),
  2072. )
  2073. nullable_fields = ('label', 'role', 'manufacturer', 'part_id', 'description')
  2074. def __init__(self, *args, **kwargs):
  2075. super().__init__(*args, **kwargs)
  2076. # Remove parent device passed as context to avoid conflicts with the actual device field
  2077. # on this form (see bug #19464)
  2078. self.initial.pop('device', None)
  2079. #
  2080. # Device component roles
  2081. #
  2082. class InventoryItemRoleBulkEditForm(OrganizationalModelBulkEditForm):
  2083. color = ColorField(
  2084. label=_('Color'),
  2085. required=False
  2086. )
  2087. model = InventoryItemRole
  2088. fieldsets = (
  2089. FieldSet('color', 'description'),
  2090. )
  2091. nullable_fields = ('color', 'description', 'comments')
  2092. class VirtualDeviceContextBulkEditForm(PrimaryModelBulkEditForm):
  2093. device = DynamicModelChoiceField(
  2094. label=_('Device'),
  2095. queryset=Device.objects.all(),
  2096. required=False
  2097. )
  2098. status = ChoiceField(
  2099. label=_('Status'),
  2100. required=False,
  2101. choices=add_blank_choice(VirtualDeviceContextStatusChoices)
  2102. )
  2103. tenant = DynamicModelChoiceField(
  2104. label=_('Tenant'),
  2105. queryset=Tenant.objects.all(),
  2106. required=False
  2107. )
  2108. model = VirtualDeviceContext
  2109. fieldsets = (
  2110. FieldSet('device', 'status', 'tenant'),
  2111. )
  2112. nullable_fields = ('device', 'tenant', )
  2113. def __init__(self, *args, **kwargs):
  2114. super().__init__(*args, **kwargs)
  2115. # The ?device=<id> GET param is navigation context (filter), not an intent to change the
  2116. # device field — drop it from initial so Django's changed_data doesn't treat it as an edit.
  2117. self.initial.pop('device', None)
  2118. #
  2119. # Addressing
  2120. #
  2121. class MACAddressBulkEditForm(PrimaryModelBulkEditForm):
  2122. model = MACAddress
  2123. fieldsets = (
  2124. FieldSet('description'),
  2125. )
  2126. nullable_fields = ('description', 'comments')