bulk_edit.py 54 KB

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