bulk_edit.py 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  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', 'speed', 'mark_connected', 'description'])
  1596. ):
  1597. type = ChoiceField(
  1598. label=_('Type'),
  1599. choices=add_blank_choice(ConsolePortTypeChoices),
  1600. required=False,
  1601. help_text=_('Physical port type')
  1602. )
  1603. mark_connected = forms.NullBooleanField(
  1604. label=_('Mark connected'),
  1605. required=False,
  1606. widget=BulkEditNullBooleanSelect
  1607. )
  1608. model = ConsolePort
  1609. fieldsets = (
  1610. FieldSet('module', 'type', 'label', 'speed', 'description', 'mark_connected'),
  1611. )
  1612. nullable_fields = ('module', 'label', 'description')
  1613. class ConsoleServerPortBulkEditForm(
  1614. ComponentBulkEditForm,
  1615. form_from_model(ConsoleServerPort, ['label', 'speed', 'mark_connected', 'description'])
  1616. ):
  1617. type = ChoiceField(
  1618. label=_('Type'),
  1619. choices=add_blank_choice(ConsolePortTypeChoices),
  1620. required=False,
  1621. help_text=_('Physical port type')
  1622. )
  1623. mark_connected = forms.NullBooleanField(
  1624. label=_('Mark connected'),
  1625. required=False,
  1626. widget=BulkEditNullBooleanSelect
  1627. )
  1628. model = ConsoleServerPort
  1629. fieldsets = (
  1630. FieldSet('module', 'type', 'label', 'speed', 'description', 'mark_connected'),
  1631. )
  1632. nullable_fields = ('module', 'label', 'description')
  1633. class PowerPortBulkEditForm(
  1634. ComponentBulkEditForm,
  1635. form_from_model(PowerPort, ['label', 'maximum_draw', 'allocated_draw', 'mark_connected', 'description'])
  1636. ):
  1637. type = ChoiceField(
  1638. label=_('Type'),
  1639. choices=add_blank_choice(PowerPortTypeChoices),
  1640. required=False,
  1641. help_text=_('Physical port type')
  1642. )
  1643. mark_connected = forms.NullBooleanField(
  1644. label=_('Mark connected'),
  1645. required=False,
  1646. widget=BulkEditNullBooleanSelect
  1647. )
  1648. model = PowerPort
  1649. fieldsets = (
  1650. FieldSet('module', 'type', 'label', 'description', 'mark_connected'),
  1651. FieldSet('maximum_draw', 'allocated_draw', name=_('Power')),
  1652. )
  1653. nullable_fields = ('module', 'label', 'description', 'maximum_draw', 'allocated_draw')
  1654. class PowerOutletBulkEditForm(
  1655. ComponentBulkEditForm,
  1656. form_from_model(
  1657. PowerOutlet,
  1658. ['label', 'status', 'color', 'feed_leg', 'power_port', 'mark_connected', 'description']
  1659. )
  1660. ):
  1661. type = ChoiceField(
  1662. label=_('Type'),
  1663. choices=add_blank_choice(PowerOutletTypeChoices),
  1664. required=False,
  1665. help_text=_('Physical port type')
  1666. )
  1667. mark_connected = forms.NullBooleanField(
  1668. label=_('Mark connected'),
  1669. required=False,
  1670. widget=BulkEditNullBooleanSelect
  1671. )
  1672. model = PowerOutlet
  1673. fieldsets = (
  1674. FieldSet('module', 'type', 'label', 'status', 'description', 'mark_connected', 'color'),
  1675. FieldSet('feed_leg', 'power_port', name=_('Power')),
  1676. )
  1677. nullable_fields = ('module', 'label', 'type', 'feed_leg', 'power_port', 'description')
  1678. def __init__(self, *args, **kwargs):
  1679. super().__init__(*args, **kwargs)
  1680. # Limit power_port queryset to PowerPorts which belong to the parent Device
  1681. if self.device_id:
  1682. device = Device.objects.filter(pk=self.device_id).first()
  1683. self.fields['power_port'].queryset = PowerPort.objects.filter(device=device)
  1684. else:
  1685. self.fields['power_port'].choices = ()
  1686. self.fields['power_port'].widget.attrs['disabled'] = True
  1687. class CoolingIntakeBulkEditForm(
  1688. ComponentBulkEditForm,
  1689. form_from_model(CoolingIntake, [
  1690. 'label', 'type', 'diameter', 'diameter_unit', 'max_flow', 'max_flow_unit',
  1691. 'description'
  1692. ])
  1693. ):
  1694. cooling_outflow = DynamicModelChoiceField(
  1695. label=_('Cooling outflow'),
  1696. queryset=CoolingOutflow.objects.all(),
  1697. required=False
  1698. )
  1699. model = CoolingIntake
  1700. fieldsets = (
  1701. FieldSet(
  1702. 'module', 'type',
  1703. InlineFields('diameter', 'diameter_unit', label=_('Diameter')),
  1704. 'label', 'cooling_outflow', 'description',
  1705. ),
  1706. FieldSet(
  1707. InlineFields('max_flow', 'max_flow_unit', label=_('Max flow')),
  1708. name=_('Characteristics')
  1709. ),
  1710. )
  1711. nullable_fields = (
  1712. 'module', 'label', 'type', 'diameter', 'diameter_unit', 'max_flow', 'max_flow_unit',
  1713. 'cooling_outflow', 'description',
  1714. )
  1715. class CoolingOutflowBulkEditForm(
  1716. ComponentBulkEditForm,
  1717. form_from_model(
  1718. CoolingOutflow,
  1719. [
  1720. 'label', 'type', 'diameter', 'diameter_unit', 'cooling_intake',
  1721. 'description'
  1722. ]
  1723. )
  1724. ):
  1725. model = CoolingOutflow
  1726. fieldsets = (
  1727. FieldSet(
  1728. 'module', 'type',
  1729. InlineFields('diameter', 'diameter_unit', label=_('Diameter')),
  1730. 'label', 'description', 'cooling_intake',
  1731. ),
  1732. )
  1733. nullable_fields = (
  1734. 'module', 'label', 'type', 'diameter', 'diameter_unit', 'cooling_intake', 'description',
  1735. )
  1736. def __init__(self, *args, **kwargs):
  1737. super().__init__(*args, **kwargs)
  1738. # Limit cooling_intake queryset to CoolingIntakes which belong to the parent Device
  1739. if self.device_id:
  1740. device = Device.objects.filter(pk=self.device_id).first()
  1741. self.fields['cooling_intake'].queryset = CoolingIntake.objects.filter(device=device)
  1742. else:
  1743. self.fields['cooling_intake'].choices = ()
  1744. self.fields['cooling_intake'].widget.attrs['disabled'] = True
  1745. class InterfaceBulkEditForm(
  1746. ComponentBulkEditForm,
  1747. form_from_model(Interface, [
  1748. 'label', 'type', 'channels', 'channel_id', 'parent', 'bridge', 'lag', 'speed', 'duplex', 'wwn', 'mtu',
  1749. 'mgmt_only', 'mark_connected', 'description', 'mode', 'rf_role', 'rf_channel', 'rf_channel_frequency',
  1750. 'rf_channel_width', 'tx_power', 'wireless_lans', 'vlan_translation_policy'
  1751. ])
  1752. ):
  1753. enabled = forms.NullBooleanField(
  1754. label=_('Enabled'),
  1755. required=False,
  1756. widget=BulkEditNullBooleanSelect
  1757. )
  1758. parent = DynamicModelChoiceField(
  1759. label=_('Parent'),
  1760. queryset=Interface.objects.all(),
  1761. required=False,
  1762. query_params={
  1763. 'virtual_chassis_member_id': '$device',
  1764. }
  1765. )
  1766. bridge = DynamicModelChoiceField(
  1767. label=_('Bridge'),
  1768. queryset=Interface.objects.all(),
  1769. required=False,
  1770. query_params={
  1771. 'virtual_chassis_member_id': '$device',
  1772. }
  1773. )
  1774. lag = DynamicModelChoiceField(
  1775. queryset=Interface.objects.all(),
  1776. required=False,
  1777. query_params={
  1778. 'type': 'lag',
  1779. 'virtual_chassis_member_id': '$device',
  1780. },
  1781. label=_('LAG')
  1782. )
  1783. vdcs = DynamicModelMultipleChoiceField(
  1784. queryset=VirtualDeviceContext.objects.all(),
  1785. required=False,
  1786. label=_('Virtual device contexts'),
  1787. query_params={
  1788. 'device_id': '$device',
  1789. }
  1790. )
  1791. speed = PositiveBigIntegerField(
  1792. label=_('Speed'),
  1793. required=False,
  1794. widget=NumberWithOptions(
  1795. options=InterfaceSpeedChoices
  1796. )
  1797. )
  1798. mgmt_only = forms.NullBooleanField(
  1799. required=False,
  1800. widget=BulkEditNullBooleanSelect,
  1801. label=_('Management only')
  1802. )
  1803. poe_mode = ChoiceField(
  1804. choices=add_blank_choice(InterfacePoEModeChoices),
  1805. required=False,
  1806. initial='',
  1807. label=_('PoE mode')
  1808. )
  1809. poe_type = ChoiceField(
  1810. choices=add_blank_choice(InterfacePoETypeChoices),
  1811. required=False,
  1812. initial='',
  1813. label=_('PoE type')
  1814. )
  1815. mark_connected = forms.NullBooleanField(
  1816. label=_('Mark connected'),
  1817. required=False,
  1818. widget=BulkEditNullBooleanSelect
  1819. )
  1820. mode = ChoiceField(
  1821. label=_('Mode'),
  1822. choices=add_blank_choice(InterfaceModeChoices),
  1823. required=False,
  1824. initial=''
  1825. )
  1826. vlan_group = DynamicModelChoiceField(
  1827. queryset=VLANGroup.objects.all(),
  1828. required=False,
  1829. label=_('VLAN group')
  1830. )
  1831. untagged_vlan = DynamicModelChoiceField(
  1832. queryset=VLAN.objects.all(),
  1833. required=False,
  1834. query_params={
  1835. 'group_id': '$vlan_group',
  1836. 'available_on_device': '$device',
  1837. },
  1838. label=_('Untagged VLAN')
  1839. )
  1840. tagged_vlans = DynamicModelMultipleChoiceField(
  1841. queryset=VLAN.objects.all(),
  1842. required=False,
  1843. query_params={
  1844. 'group_id': '$vlan_group',
  1845. 'available_on_device': '$device',
  1846. },
  1847. label=_('Tagged VLANs')
  1848. )
  1849. add_tagged_vlans = DynamicModelMultipleChoiceField(
  1850. label=_('Add tagged VLANs'),
  1851. queryset=VLAN.objects.all(),
  1852. required=False,
  1853. query_params={
  1854. 'group_id': '$vlan_group',
  1855. 'available_on_device': '$device',
  1856. },
  1857. )
  1858. remove_tagged_vlans = DynamicModelMultipleChoiceField(
  1859. label=_('Remove tagged VLANs'),
  1860. queryset=VLAN.objects.all(),
  1861. required=False,
  1862. query_params={
  1863. 'group_id': '$vlan_group',
  1864. 'available_on_device': '$device',
  1865. }
  1866. )
  1867. qinq_svlan = DynamicModelChoiceField(
  1868. queryset=VLAN.objects.all(),
  1869. required=False,
  1870. label=_('Q-in-Q Service VLAN'),
  1871. query_params={
  1872. 'group_id': '$vlan_group',
  1873. 'available_on_device': '$device',
  1874. 'qinq_role': VLANQinQRoleChoices.ROLE_SERVICE,
  1875. }
  1876. )
  1877. vrf = DynamicModelChoiceField(
  1878. queryset=VRF.objects.all(),
  1879. required=False,
  1880. label=_('VRF')
  1881. )
  1882. wireless_lan_group = DynamicModelChoiceField(
  1883. queryset=WirelessLANGroup.objects.all(),
  1884. required=False,
  1885. label=_('Wireless LAN group')
  1886. )
  1887. wireless_lans = DynamicModelMultipleChoiceField(
  1888. queryset=WirelessLAN.objects.all(),
  1889. required=False,
  1890. label=_('Wireless LANs'),
  1891. query_params={
  1892. 'group_id': '$wireless_lan_group',
  1893. }
  1894. )
  1895. model = Interface
  1896. fieldsets = (
  1897. FieldSet('module', 'type', 'channels', 'label', 'speed', 'duplex', 'description'),
  1898. FieldSet('vrf', 'wwn', name=_('Addressing')),
  1899. FieldSet('vdcs', 'mtu', 'tx_power', 'enabled', 'mgmt_only', 'mark_connected', name=_('Operation')),
  1900. FieldSet('poe_mode', 'poe_type', name=_('PoE')),
  1901. FieldSet('parent', 'channel_id', 'bridge', 'lag', name=_('Related Interfaces')),
  1902. FieldSet(
  1903. 'mode', 'vlan_group', 'untagged_vlan', 'qinq_svlan', 'vlan_translation_policy', name=_('802.1Q Switching')
  1904. ),
  1905. FieldSet(
  1906. TabbedGroups(
  1907. FieldSet('tagged_vlans', name=_('Assignment')),
  1908. FieldSet('add_tagged_vlans', 'remove_tagged_vlans', name=_('Add/Remove')),
  1909. ),
  1910. ),
  1911. FieldSet(
  1912. 'rf_role', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width', 'wireless_lan_group', 'wireless_lans',
  1913. name=_('Wireless')
  1914. ),
  1915. )
  1916. nullable_fields = (
  1917. 'module', 'label', 'channels', 'channel_id', 'parent', 'bridge', 'lag', 'speed', 'duplex', 'wwn', 'vdcs',
  1918. 'mtu', 'description', 'poe_mode', 'poe_type', 'mode', 'rf_channel', 'rf_channel_frequency', 'rf_channel_width',
  1919. 'tx_power', 'untagged_vlan', 'tagged_vlans', 'qinq_svlan', 'vrf', 'wireless_lans', 'vlan_translation_policy',
  1920. )
  1921. def __init__(self, *args, **kwargs):
  1922. super().__init__(*args, **kwargs)
  1923. if not self.device_id:
  1924. # See #4523
  1925. if 'pk' in self.initial:
  1926. site = None
  1927. interfaces = Interface.objects.filter(pk__in=self.initial['pk']).prefetch_related('device__site')
  1928. # Check interface sites. First interface should set site, further interfaces will either continue the
  1929. # loop or reset back to no site and break the loop.
  1930. for interface in interfaces:
  1931. if site is None:
  1932. site = interface.device.site
  1933. elif interface.device.site is not site:
  1934. site = None
  1935. break
  1936. if site is not None:
  1937. # Query for VLANs assigned to the same site and VLANs with no site assigned (null).
  1938. self.fields['untagged_vlan'].widget.add_query_param(
  1939. 'site_id', [site.pk, settings.FILTERS_NULL_CHOICE_VALUE]
  1940. )
  1941. self.fields['tagged_vlans'].widget.add_query_param(
  1942. 'site_id', [site.pk, settings.FILTERS_NULL_CHOICE_VALUE]
  1943. )
  1944. self.fields['add_tagged_vlans'].widget.add_query_param(
  1945. 'site_id', [site.pk, settings.FILTERS_NULL_CHOICE_VALUE]
  1946. )
  1947. self.fields['remove_tagged_vlans'].widget.add_query_param(
  1948. 'site_id', [site.pk, settings.FILTERS_NULL_CHOICE_VALUE]
  1949. )
  1950. self.fields['parent'].choices = ()
  1951. self.fields['parent'].widget.attrs['disabled'] = True
  1952. self.fields['bridge'].choices = ()
  1953. self.fields['bridge'].widget.attrs['disabled'] = True
  1954. self.fields['lag'].choices = ()
  1955. self.fields['lag'].widget.attrs['disabled'] = True
  1956. def clean(self):
  1957. super().clean()
  1958. if not self.cleaned_data['mode']:
  1959. if self.cleaned_data['untagged_vlan']:
  1960. raise forms.ValidationError({'untagged_vlan': _("Interface mode must be specified to assign VLANs")})
  1961. if self.cleaned_data['tagged_vlans']:
  1962. raise forms.ValidationError({'tagged_vlans': _("Interface mode must be specified to assign VLANs")})
  1963. # Untagged interfaces cannot be assigned tagged VLANs
  1964. elif self.cleaned_data['mode'] == InterfaceModeChoices.MODE_ACCESS and self.cleaned_data['tagged_vlans']:
  1965. raise forms.ValidationError({
  1966. 'mode': _("An access interface cannot have tagged VLANs assigned.")
  1967. })
  1968. # Remove all tagged VLAN assignments from "tagged all" interfaces
  1969. elif self.cleaned_data['mode'] == InterfaceModeChoices.MODE_TAGGED_ALL:
  1970. self.cleaned_data['tagged_vlans'] = []
  1971. class FrontPortBulkEditForm(
  1972. ComponentBulkEditForm,
  1973. form_from_model(FrontPort, ['label', 'color', 'mark_connected', 'description'])
  1974. ):
  1975. type = ChoiceField(
  1976. label=_('Type'),
  1977. choices=add_blank_choice(PortTypeChoices),
  1978. required=False
  1979. )
  1980. mark_connected = forms.NullBooleanField(
  1981. label=_('Mark connected'),
  1982. required=False,
  1983. widget=BulkEditNullBooleanSelect
  1984. )
  1985. model = FrontPort
  1986. fieldsets = (
  1987. FieldSet('module', 'type', 'label', 'color', 'description', 'mark_connected'),
  1988. )
  1989. nullable_fields = ('module', 'label', 'description', 'color')
  1990. class RearPortBulkEditForm(
  1991. ComponentBulkEditForm,
  1992. form_from_model(RearPort, ['label', 'color', 'mark_connected', 'description'])
  1993. ):
  1994. type = ChoiceField(
  1995. label=_('Type'),
  1996. choices=add_blank_choice(PortTypeChoices),
  1997. required=False
  1998. )
  1999. mark_connected = forms.NullBooleanField(
  2000. label=_('Mark connected'),
  2001. required=False,
  2002. widget=BulkEditNullBooleanSelect
  2003. )
  2004. model = RearPort
  2005. fieldsets = (
  2006. FieldSet('module', 'type', 'label', 'color', 'description', 'mark_connected'),
  2007. )
  2008. nullable_fields = ('module', 'label', 'description', 'color')
  2009. class ModuleBayBulkEditForm(
  2010. form_from_model(ModuleBay, ['label', 'position', 'description']),
  2011. NetBoxModelBulkEditForm
  2012. ):
  2013. enabled = forms.NullBooleanField(
  2014. label=_('Enabled'),
  2015. required=False,
  2016. widget=BulkEditNullBooleanSelect,
  2017. )
  2018. add_module_bay_types = DynamicModelMultipleChoiceField(
  2019. label=_('Add bay types'),
  2020. queryset=ModuleBayType.objects.all(),
  2021. required=False,
  2022. )
  2023. remove_module_bay_types = DynamicModelMultipleChoiceField(
  2024. label=_('Remove bay types'),
  2025. queryset=ModuleBayType.objects.all(),
  2026. required=False,
  2027. )
  2028. model = ModuleBay
  2029. fieldsets = (
  2030. FieldSet('label', 'position', 'enabled', 'description'),
  2031. FieldSet('add_module_bay_types', 'remove_module_bay_types', name=_('Bay Types')),
  2032. )
  2033. nullable_fields = ('label', 'position', 'description')
  2034. class DeviceBayBulkEditForm(
  2035. form_from_model(DeviceBay, ['label', 'description']),
  2036. NetBoxModelBulkEditForm
  2037. ):
  2038. enabled = forms.NullBooleanField(
  2039. label=_('Enabled'),
  2040. required=False,
  2041. widget=BulkEditNullBooleanSelect,
  2042. )
  2043. model = DeviceBay
  2044. fieldsets = (
  2045. FieldSet('label', 'enabled', 'description'),
  2046. )
  2047. nullable_fields = ('label', 'description')
  2048. class InventoryItemBulkEditForm(
  2049. form_from_model(InventoryItem, ['label', 'role', 'manufacturer', 'part_id', 'description']),
  2050. NetBoxModelBulkEditForm
  2051. ):
  2052. device = DynamicModelChoiceField(
  2053. label=_('Device'),
  2054. queryset=Device.objects.all(),
  2055. required=False
  2056. )
  2057. role = DynamicModelChoiceField(
  2058. label=_('Role'),
  2059. queryset=InventoryItemRole.objects.all(),
  2060. required=False
  2061. )
  2062. manufacturer = DynamicModelChoiceField(
  2063. label=_('Manufacturer'),
  2064. queryset=Manufacturer.objects.all(),
  2065. required=False
  2066. )
  2067. status = ChoiceField(
  2068. label=_('Status'),
  2069. choices=add_blank_choice(InventoryItemStatusChoices),
  2070. required=False,
  2071. initial=''
  2072. )
  2073. model = InventoryItem
  2074. fieldsets = (
  2075. FieldSet('device', 'label', 'role', 'manufacturer', 'part_id', 'status', 'description'),
  2076. )
  2077. nullable_fields = ('label', 'role', 'manufacturer', 'part_id', 'description')
  2078. def __init__(self, *args, **kwargs):
  2079. super().__init__(*args, **kwargs)
  2080. # Remove parent device passed as context to avoid conflicts with the actual device field
  2081. # on this form (see bug #19464)
  2082. self.initial.pop('device', None)
  2083. #
  2084. # Device component roles
  2085. #
  2086. class InventoryItemRoleBulkEditForm(OrganizationalModelBulkEditForm):
  2087. color = ColorField(
  2088. label=_('Color'),
  2089. required=False
  2090. )
  2091. model = InventoryItemRole
  2092. fieldsets = (
  2093. FieldSet('color', 'description'),
  2094. )
  2095. nullable_fields = ('color', 'description', 'comments')
  2096. class VirtualDeviceContextBulkEditForm(PrimaryModelBulkEditForm):
  2097. device = DynamicModelChoiceField(
  2098. label=_('Device'),
  2099. queryset=Device.objects.all(),
  2100. required=False
  2101. )
  2102. status = ChoiceField(
  2103. label=_('Status'),
  2104. required=False,
  2105. choices=add_blank_choice(VirtualDeviceContextStatusChoices)
  2106. )
  2107. tenant = DynamicModelChoiceField(
  2108. label=_('Tenant'),
  2109. queryset=Tenant.objects.all(),
  2110. required=False
  2111. )
  2112. model = VirtualDeviceContext
  2113. fieldsets = (
  2114. FieldSet('device', 'status', 'tenant'),
  2115. )
  2116. nullable_fields = ('device', 'tenant', )
  2117. def __init__(self, *args, **kwargs):
  2118. super().__init__(*args, **kwargs)
  2119. # The ?device=<id> GET param is navigation context (filter), not an intent to change the
  2120. # device field — drop it from initial so Django's changed_data doesn't treat it as an edit.
  2121. self.initial.pop('device', None)
  2122. #
  2123. # Addressing
  2124. #
  2125. class MACAddressBulkEditForm(PrimaryModelBulkEditForm):
  2126. model = MACAddress
  2127. fieldsets = (
  2128. FieldSet('description'),
  2129. )
  2130. nullable_fields = ('description', 'comments')