bulk_import.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. from django import forms
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.contrib.postgres.forms.array import SimpleArrayField
  4. from django.core.exceptions import ObjectDoesNotExist
  5. from django.utils.safestring import mark_safe
  6. from dcim.choices import *
  7. from dcim.constants import *
  8. from dcim.models import *
  9. from extras.forms import CustomFieldModelCSVForm
  10. from tenancy.models import Tenant
  11. from utilities.forms import CSVChoiceField, CSVContentTypeField, CSVModelChoiceField, CSVTypedChoiceField, SlugField
  12. from virtualization.models import Cluster
  13. __all__ = (
  14. 'CableCSVForm',
  15. 'ChildDeviceCSVForm',
  16. 'ConsolePortCSVForm',
  17. 'ConsoleServerPortCSVForm',
  18. 'DeviceBayCSVForm',
  19. 'DeviceCSVForm',
  20. 'DeviceRoleCSVForm',
  21. 'FrontPortCSVForm',
  22. 'InterfaceCSVForm',
  23. 'InventoryItemCSVForm',
  24. 'LocationCSVForm',
  25. 'ManufacturerCSVForm',
  26. 'PlatformCSVForm',
  27. 'PowerFeedCSVForm',
  28. 'PowerOutletCSVForm',
  29. 'PowerPanelCSVForm',
  30. 'PowerPortCSVForm',
  31. 'RackCSVForm',
  32. 'RackReservationCSVForm',
  33. 'RackRoleCSVForm',
  34. 'RearPortCSVForm',
  35. 'RegionCSVForm',
  36. 'SiteCSVForm',
  37. 'SiteGroupCSVForm',
  38. 'VirtualChassisCSVForm',
  39. )
  40. class RegionCSVForm(CustomFieldModelCSVForm):
  41. parent = CSVModelChoiceField(
  42. queryset=Region.objects.all(),
  43. required=False,
  44. to_field_name='name',
  45. help_text='Name of parent region'
  46. )
  47. class Meta:
  48. model = Region
  49. fields = ('name', 'slug', 'parent', 'description')
  50. class SiteGroupCSVForm(CustomFieldModelCSVForm):
  51. parent = CSVModelChoiceField(
  52. queryset=SiteGroup.objects.all(),
  53. required=False,
  54. to_field_name='name',
  55. help_text='Name of parent site group'
  56. )
  57. class Meta:
  58. model = SiteGroup
  59. fields = ('name', 'slug', 'parent', 'description')
  60. class SiteCSVForm(CustomFieldModelCSVForm):
  61. status = CSVChoiceField(
  62. choices=SiteStatusChoices,
  63. help_text='Operational status'
  64. )
  65. region = CSVModelChoiceField(
  66. queryset=Region.objects.all(),
  67. required=False,
  68. to_field_name='name',
  69. help_text='Assigned region'
  70. )
  71. group = CSVModelChoiceField(
  72. queryset=SiteGroup.objects.all(),
  73. required=False,
  74. to_field_name='name',
  75. help_text='Assigned group'
  76. )
  77. tenant = CSVModelChoiceField(
  78. queryset=Tenant.objects.all(),
  79. required=False,
  80. to_field_name='name',
  81. help_text='Assigned tenant'
  82. )
  83. class Meta:
  84. model = Site
  85. fields = (
  86. 'name', 'slug', 'status', 'region', 'group', 'tenant', 'facility', 'asn', 'time_zone', 'description',
  87. 'physical_address', 'shipping_address', 'latitude', 'longitude', 'contact_name', 'contact_phone',
  88. 'contact_email', 'comments',
  89. )
  90. help_texts = {
  91. 'time_zone': mark_safe(
  92. 'Time zone (<a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones">available options</a>)'
  93. )
  94. }
  95. class LocationCSVForm(CustomFieldModelCSVForm):
  96. site = CSVModelChoiceField(
  97. queryset=Site.objects.all(),
  98. to_field_name='name',
  99. help_text='Assigned site'
  100. )
  101. parent = CSVModelChoiceField(
  102. queryset=Location.objects.all(),
  103. required=False,
  104. to_field_name='name',
  105. help_text='Parent location',
  106. error_messages={
  107. 'invalid_choice': 'Location not found.',
  108. }
  109. )
  110. class Meta:
  111. model = Location
  112. fields = ('site', 'parent', 'name', 'slug', 'description')
  113. class RackRoleCSVForm(CustomFieldModelCSVForm):
  114. slug = SlugField()
  115. class Meta:
  116. model = RackRole
  117. fields = ('name', 'slug', 'color', 'description')
  118. help_texts = {
  119. 'color': mark_safe('RGB color in hexadecimal (e.g. <code>00ff00</code>)'),
  120. }
  121. class RackCSVForm(CustomFieldModelCSVForm):
  122. site = CSVModelChoiceField(
  123. queryset=Site.objects.all(),
  124. to_field_name='name'
  125. )
  126. location = CSVModelChoiceField(
  127. queryset=Location.objects.all(),
  128. required=False,
  129. to_field_name='name'
  130. )
  131. tenant = CSVModelChoiceField(
  132. queryset=Tenant.objects.all(),
  133. required=False,
  134. to_field_name='name',
  135. help_text='Name of assigned tenant'
  136. )
  137. status = CSVChoiceField(
  138. choices=RackStatusChoices,
  139. help_text='Operational status'
  140. )
  141. role = CSVModelChoiceField(
  142. queryset=RackRole.objects.all(),
  143. required=False,
  144. to_field_name='name',
  145. help_text='Name of assigned role'
  146. )
  147. type = CSVChoiceField(
  148. choices=RackTypeChoices,
  149. required=False,
  150. help_text='Rack type'
  151. )
  152. width = forms.ChoiceField(
  153. choices=RackWidthChoices,
  154. help_text='Rail-to-rail width (in inches)'
  155. )
  156. outer_unit = CSVChoiceField(
  157. choices=RackDimensionUnitChoices,
  158. required=False,
  159. help_text='Unit for outer dimensions'
  160. )
  161. class Meta:
  162. model = Rack
  163. fields = (
  164. 'site', 'location', 'name', 'facility_id', 'tenant', 'status', 'role', 'type', 'serial', 'asset_tag',
  165. 'width', 'u_height', 'desc_units', 'outer_width', 'outer_depth', 'outer_unit', 'comments',
  166. )
  167. def __init__(self, data=None, *args, **kwargs):
  168. super().__init__(data, *args, **kwargs)
  169. if data:
  170. # Limit location queryset by assigned site
  171. params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
  172. self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
  173. class RackReservationCSVForm(CustomFieldModelCSVForm):
  174. site = CSVModelChoiceField(
  175. queryset=Site.objects.all(),
  176. to_field_name='name',
  177. help_text='Parent site'
  178. )
  179. location = CSVModelChoiceField(
  180. queryset=Location.objects.all(),
  181. to_field_name='name',
  182. required=False,
  183. help_text="Rack's location (if any)"
  184. )
  185. rack = CSVModelChoiceField(
  186. queryset=Rack.objects.all(),
  187. to_field_name='name',
  188. help_text='Rack'
  189. )
  190. units = SimpleArrayField(
  191. base_field=forms.IntegerField(),
  192. required=True,
  193. help_text='Comma-separated list of individual unit numbers'
  194. )
  195. tenant = CSVModelChoiceField(
  196. queryset=Tenant.objects.all(),
  197. required=False,
  198. to_field_name='name',
  199. help_text='Assigned tenant'
  200. )
  201. class Meta:
  202. model = RackReservation
  203. fields = ('site', 'location', 'rack', 'units', 'tenant', 'description')
  204. def __init__(self, data=None, *args, **kwargs):
  205. super().__init__(data, *args, **kwargs)
  206. if data:
  207. # Limit location queryset by assigned site
  208. params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
  209. self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
  210. # Limit rack queryset by assigned site and group
  211. params = {
  212. f"site__{self.fields['site'].to_field_name}": data.get('site'),
  213. f"location__{self.fields['location'].to_field_name}": data.get('location'),
  214. }
  215. self.fields['rack'].queryset = self.fields['rack'].queryset.filter(**params)
  216. class ManufacturerCSVForm(CustomFieldModelCSVForm):
  217. class Meta:
  218. model = Manufacturer
  219. fields = ('name', 'slug', 'description')
  220. class DeviceRoleCSVForm(CustomFieldModelCSVForm):
  221. slug = SlugField()
  222. class Meta:
  223. model = DeviceRole
  224. fields = ('name', 'slug', 'color', 'vm_role', 'description')
  225. help_texts = {
  226. 'color': mark_safe('RGB color in hexadecimal (e.g. <code>00ff00</code>)'),
  227. }
  228. class PlatformCSVForm(CustomFieldModelCSVForm):
  229. slug = SlugField()
  230. manufacturer = CSVModelChoiceField(
  231. queryset=Manufacturer.objects.all(),
  232. required=False,
  233. to_field_name='name',
  234. help_text='Limit platform assignments to this manufacturer'
  235. )
  236. class Meta:
  237. model = Platform
  238. fields = ('name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description')
  239. class BaseDeviceCSVForm(CustomFieldModelCSVForm):
  240. device_role = CSVModelChoiceField(
  241. queryset=DeviceRole.objects.all(),
  242. to_field_name='name',
  243. help_text='Assigned role'
  244. )
  245. tenant = CSVModelChoiceField(
  246. queryset=Tenant.objects.all(),
  247. required=False,
  248. to_field_name='name',
  249. help_text='Assigned tenant'
  250. )
  251. manufacturer = CSVModelChoiceField(
  252. queryset=Manufacturer.objects.all(),
  253. to_field_name='name',
  254. help_text='Device type manufacturer'
  255. )
  256. device_type = CSVModelChoiceField(
  257. queryset=DeviceType.objects.all(),
  258. to_field_name='model',
  259. help_text='Device type model'
  260. )
  261. platform = CSVModelChoiceField(
  262. queryset=Platform.objects.all(),
  263. required=False,
  264. to_field_name='name',
  265. help_text='Assigned platform'
  266. )
  267. status = CSVChoiceField(
  268. choices=DeviceStatusChoices,
  269. help_text='Operational status'
  270. )
  271. virtual_chassis = CSVModelChoiceField(
  272. queryset=VirtualChassis.objects.all(),
  273. to_field_name='name',
  274. required=False,
  275. help_text='Virtual chassis'
  276. )
  277. cluster = CSVModelChoiceField(
  278. queryset=Cluster.objects.all(),
  279. to_field_name='name',
  280. required=False,
  281. help_text='Virtualization cluster'
  282. )
  283. class Meta:
  284. fields = []
  285. model = Device
  286. help_texts = {
  287. 'vc_position': 'Virtual chassis position',
  288. 'vc_priority': 'Virtual chassis priority',
  289. }
  290. def __init__(self, data=None, *args, **kwargs):
  291. super().__init__(data, *args, **kwargs)
  292. if data:
  293. # Limit device type queryset by manufacturer
  294. params = {f"manufacturer__{self.fields['manufacturer'].to_field_name}": data.get('manufacturer')}
  295. self.fields['device_type'].queryset = self.fields['device_type'].queryset.filter(**params)
  296. class DeviceCSVForm(BaseDeviceCSVForm):
  297. site = CSVModelChoiceField(
  298. queryset=Site.objects.all(),
  299. to_field_name='name',
  300. help_text='Assigned site'
  301. )
  302. location = CSVModelChoiceField(
  303. queryset=Location.objects.all(),
  304. to_field_name='name',
  305. required=False,
  306. help_text="Assigned location (if any)"
  307. )
  308. rack = CSVModelChoiceField(
  309. queryset=Rack.objects.all(),
  310. to_field_name='name',
  311. required=False,
  312. help_text="Assigned rack (if any)"
  313. )
  314. face = CSVChoiceField(
  315. choices=DeviceFaceChoices,
  316. required=False,
  317. help_text='Mounted rack face'
  318. )
  319. class Meta(BaseDeviceCSVForm.Meta):
  320. fields = [
  321. 'name', 'device_role', 'tenant', 'manufacturer', 'device_type', 'platform', 'serial', 'asset_tag', 'status',
  322. 'site', 'location', 'rack', 'position', 'face', 'virtual_chassis', 'vc_position', 'vc_priority', 'cluster',
  323. 'comments',
  324. ]
  325. def __init__(self, data=None, *args, **kwargs):
  326. super().__init__(data, *args, **kwargs)
  327. if data:
  328. # Limit location queryset by assigned site
  329. params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
  330. self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
  331. # Limit rack queryset by assigned site and group
  332. params = {
  333. f"site__{self.fields['site'].to_field_name}": data.get('site'),
  334. f"location__{self.fields['location'].to_field_name}": data.get('location'),
  335. }
  336. self.fields['rack'].queryset = self.fields['rack'].queryset.filter(**params)
  337. class ChildDeviceCSVForm(BaseDeviceCSVForm):
  338. parent = CSVModelChoiceField(
  339. queryset=Device.objects.all(),
  340. to_field_name='name',
  341. help_text='Parent device'
  342. )
  343. device_bay = CSVModelChoiceField(
  344. queryset=DeviceBay.objects.all(),
  345. to_field_name='name',
  346. help_text='Device bay in which this device is installed'
  347. )
  348. class Meta(BaseDeviceCSVForm.Meta):
  349. fields = [
  350. 'name', 'device_role', 'tenant', 'manufacturer', 'device_type', 'platform', 'serial', 'asset_tag', 'status',
  351. 'parent', 'device_bay', 'virtual_chassis', 'vc_position', 'vc_priority', 'cluster', 'comments',
  352. ]
  353. def __init__(self, data=None, *args, **kwargs):
  354. super().__init__(data, *args, **kwargs)
  355. if data:
  356. # Limit device bay queryset by parent device
  357. params = {f"device__{self.fields['parent'].to_field_name}": data.get('parent')}
  358. self.fields['device_bay'].queryset = self.fields['device_bay'].queryset.filter(**params)
  359. def clean(self):
  360. super().clean()
  361. # Set parent_bay reverse relationship
  362. device_bay = self.cleaned_data.get('device_bay')
  363. if device_bay:
  364. self.instance.parent_bay = device_bay
  365. # Inherit site and rack from parent device
  366. parent = self.cleaned_data.get('parent')
  367. if parent:
  368. self.instance.site = parent.site
  369. self.instance.rack = parent.rack
  370. #
  371. # Device components
  372. #
  373. class ConsolePortCSVForm(CustomFieldModelCSVForm):
  374. device = CSVModelChoiceField(
  375. queryset=Device.objects.all(),
  376. to_field_name='name'
  377. )
  378. type = CSVChoiceField(
  379. choices=ConsolePortTypeChoices,
  380. required=False,
  381. help_text='Port type'
  382. )
  383. speed = CSVTypedChoiceField(
  384. choices=ConsolePortSpeedChoices,
  385. coerce=int,
  386. empty_value=None,
  387. required=False,
  388. help_text='Port speed in bps'
  389. )
  390. class Meta:
  391. model = ConsolePort
  392. fields = ('device', 'name', 'label', 'type', 'speed', 'mark_connected', 'description')
  393. class ConsoleServerPortCSVForm(CustomFieldModelCSVForm):
  394. device = CSVModelChoiceField(
  395. queryset=Device.objects.all(),
  396. to_field_name='name'
  397. )
  398. type = CSVChoiceField(
  399. choices=ConsolePortTypeChoices,
  400. required=False,
  401. help_text='Port type'
  402. )
  403. speed = CSVTypedChoiceField(
  404. choices=ConsolePortSpeedChoices,
  405. coerce=int,
  406. empty_value=None,
  407. required=False,
  408. help_text='Port speed in bps'
  409. )
  410. class Meta:
  411. model = ConsoleServerPort
  412. fields = ('device', 'name', 'label', 'type', 'speed', 'mark_connected', 'description')
  413. class PowerPortCSVForm(CustomFieldModelCSVForm):
  414. device = CSVModelChoiceField(
  415. queryset=Device.objects.all(),
  416. to_field_name='name'
  417. )
  418. type = CSVChoiceField(
  419. choices=PowerPortTypeChoices,
  420. required=False,
  421. help_text='Port type'
  422. )
  423. class Meta:
  424. model = PowerPort
  425. fields = (
  426. 'device', 'name', 'label', 'type', 'mark_connected', 'maximum_draw', 'allocated_draw', 'description',
  427. )
  428. class PowerOutletCSVForm(CustomFieldModelCSVForm):
  429. device = CSVModelChoiceField(
  430. queryset=Device.objects.all(),
  431. to_field_name='name'
  432. )
  433. type = CSVChoiceField(
  434. choices=PowerOutletTypeChoices,
  435. required=False,
  436. help_text='Outlet type'
  437. )
  438. power_port = CSVModelChoiceField(
  439. queryset=PowerPort.objects.all(),
  440. required=False,
  441. to_field_name='name',
  442. help_text='Local power port which feeds this outlet'
  443. )
  444. feed_leg = CSVChoiceField(
  445. choices=PowerOutletFeedLegChoices,
  446. required=False,
  447. help_text='Electrical phase (for three-phase circuits)'
  448. )
  449. class Meta:
  450. model = PowerOutlet
  451. fields = ('device', 'name', 'label', 'type', 'mark_connected', 'power_port', 'feed_leg', 'description')
  452. def __init__(self, *args, **kwargs):
  453. super().__init__(*args, **kwargs)
  454. # Limit PowerPort choices to those belonging to this device (or VC master)
  455. if self.is_bound:
  456. try:
  457. device = self.fields['device'].to_python(self.data['device'])
  458. except forms.ValidationError:
  459. device = None
  460. else:
  461. try:
  462. device = self.instance.device
  463. except Device.DoesNotExist:
  464. device = None
  465. if device:
  466. self.fields['power_port'].queryset = PowerPort.objects.filter(
  467. device__in=[device, device.get_vc_master()]
  468. )
  469. else:
  470. self.fields['power_port'].queryset = PowerPort.objects.none()
  471. class InterfaceCSVForm(CustomFieldModelCSVForm):
  472. device = CSVModelChoiceField(
  473. queryset=Device.objects.all(),
  474. to_field_name='name'
  475. )
  476. parent = CSVModelChoiceField(
  477. queryset=Interface.objects.all(),
  478. required=False,
  479. to_field_name='name',
  480. help_text='Parent interface'
  481. )
  482. lag = CSVModelChoiceField(
  483. queryset=Interface.objects.all(),
  484. required=False,
  485. to_field_name='name',
  486. help_text='Parent LAG interface'
  487. )
  488. type = CSVChoiceField(
  489. choices=InterfaceTypeChoices,
  490. help_text='Physical medium'
  491. )
  492. mode = CSVChoiceField(
  493. choices=InterfaceModeChoices,
  494. required=False,
  495. help_text='IEEE 802.1Q operational mode (for L2 interfaces)'
  496. )
  497. class Meta:
  498. model = Interface
  499. fields = (
  500. 'device', 'name', 'label', 'parent', 'lag', 'type', 'enabled', 'mark_connected', 'mac_address', 'mtu',
  501. 'mgmt_only', 'description', 'mode',
  502. )
  503. def __init__(self, *args, **kwargs):
  504. super().__init__(*args, **kwargs)
  505. # Limit LAG choices to interfaces belonging to this device (or virtual chassis)
  506. device = None
  507. if self.is_bound and 'device' in self.data:
  508. try:
  509. device = self.fields['device'].to_python(self.data['device'])
  510. except forms.ValidationError:
  511. pass
  512. if device and device.virtual_chassis:
  513. self.fields['lag'].queryset = Interface.objects.filter(
  514. Q(device=device) | Q(device__virtual_chassis=device.virtual_chassis),
  515. type=InterfaceTypeChoices.TYPE_LAG
  516. )
  517. self.fields['parent'].queryset = Interface.objects.filter(
  518. Q(device=device) | Q(device__virtual_chassis=device.virtual_chassis)
  519. )
  520. elif device:
  521. self.fields['lag'].queryset = Interface.objects.filter(
  522. device=device,
  523. type=InterfaceTypeChoices.TYPE_LAG
  524. )
  525. self.fields['parent'].queryset = Interface.objects.filter(device=device)
  526. else:
  527. self.fields['lag'].queryset = Interface.objects.none()
  528. self.fields['parent'].queryset = Interface.objects.none()
  529. def clean_enabled(self):
  530. # Make sure enabled is True when it's not included in the uploaded data
  531. if 'enabled' not in self.data:
  532. return True
  533. else:
  534. return self.cleaned_data['enabled']
  535. class FrontPortCSVForm(CustomFieldModelCSVForm):
  536. device = CSVModelChoiceField(
  537. queryset=Device.objects.all(),
  538. to_field_name='name'
  539. )
  540. rear_port = CSVModelChoiceField(
  541. queryset=RearPort.objects.all(),
  542. to_field_name='name',
  543. help_text='Corresponding rear port'
  544. )
  545. type = CSVChoiceField(
  546. choices=PortTypeChoices,
  547. help_text='Physical medium classification'
  548. )
  549. class Meta:
  550. model = FrontPort
  551. fields = (
  552. 'device', 'name', 'label', 'type', 'color', 'mark_connected', 'rear_port', 'rear_port_position',
  553. 'description',
  554. )
  555. help_texts = {
  556. 'rear_port_position': 'Mapped position on corresponding rear port',
  557. }
  558. def __init__(self, *args, **kwargs):
  559. super().__init__(*args, **kwargs)
  560. # Limit RearPort choices to those belonging to this device (or VC master)
  561. if self.is_bound:
  562. try:
  563. device = self.fields['device'].to_python(self.data['device'])
  564. except forms.ValidationError:
  565. device = None
  566. else:
  567. try:
  568. device = self.instance.device
  569. except Device.DoesNotExist:
  570. device = None
  571. if device:
  572. self.fields['rear_port'].queryset = RearPort.objects.filter(
  573. device__in=[device, device.get_vc_master()]
  574. )
  575. else:
  576. self.fields['rear_port'].queryset = RearPort.objects.none()
  577. class RearPortCSVForm(CustomFieldModelCSVForm):
  578. device = CSVModelChoiceField(
  579. queryset=Device.objects.all(),
  580. to_field_name='name'
  581. )
  582. type = CSVChoiceField(
  583. help_text='Physical medium classification',
  584. choices=PortTypeChoices,
  585. )
  586. class Meta:
  587. model = RearPort
  588. fields = ('device', 'name', 'label', 'type', 'color', 'mark_connected', 'positions', 'description')
  589. help_texts = {
  590. 'positions': 'Number of front ports which may be mapped'
  591. }
  592. class DeviceBayCSVForm(CustomFieldModelCSVForm):
  593. device = CSVModelChoiceField(
  594. queryset=Device.objects.all(),
  595. to_field_name='name'
  596. )
  597. installed_device = CSVModelChoiceField(
  598. queryset=Device.objects.all(),
  599. required=False,
  600. to_field_name='name',
  601. help_text='Child device installed within this bay',
  602. error_messages={
  603. 'invalid_choice': 'Child device not found.',
  604. }
  605. )
  606. class Meta:
  607. model = DeviceBay
  608. fields = ('device', 'name', 'label', 'installed_device', 'description')
  609. def __init__(self, *args, **kwargs):
  610. super().__init__(*args, **kwargs)
  611. # Limit installed device choices to devices of the correct type and location
  612. if self.is_bound:
  613. try:
  614. device = self.fields['device'].to_python(self.data['device'])
  615. except forms.ValidationError:
  616. device = None
  617. else:
  618. try:
  619. device = self.instance.device
  620. except Device.DoesNotExist:
  621. device = None
  622. if device:
  623. self.fields['installed_device'].queryset = Device.objects.filter(
  624. site=device.site,
  625. rack=device.rack,
  626. parent_bay__isnull=True,
  627. device_type__u_height=0,
  628. device_type__subdevice_role=SubdeviceRoleChoices.ROLE_CHILD
  629. ).exclude(pk=device.pk)
  630. else:
  631. self.fields['installed_device'].queryset = Interface.objects.none()
  632. class InventoryItemCSVForm(CustomFieldModelCSVForm):
  633. device = CSVModelChoiceField(
  634. queryset=Device.objects.all(),
  635. to_field_name='name'
  636. )
  637. manufacturer = CSVModelChoiceField(
  638. queryset=Manufacturer.objects.all(),
  639. to_field_name='name',
  640. required=False
  641. )
  642. parent = CSVModelChoiceField(
  643. queryset=Device.objects.all(),
  644. to_field_name='name',
  645. required=False,
  646. help_text='Parent inventory item'
  647. )
  648. class Meta:
  649. model = InventoryItem
  650. fields = (
  651. 'device', 'name', 'label', 'manufacturer', 'part_id', 'serial', 'asset_tag', 'discovered', 'description',
  652. )
  653. def __init__(self, *args, **kwargs):
  654. super().__init__(*args, **kwargs)
  655. # Limit parent choices to inventory items belonging to this device
  656. device = None
  657. if self.is_bound and 'device' in self.data:
  658. try:
  659. device = self.fields['device'].to_python(self.data['device'])
  660. except forms.ValidationError:
  661. pass
  662. if device:
  663. self.fields['parent'].queryset = InventoryItem.objects.filter(device=device)
  664. else:
  665. self.fields['parent'].queryset = InventoryItem.objects.none()
  666. class CableCSVForm(CustomFieldModelCSVForm):
  667. # Termination A
  668. side_a_device = CSVModelChoiceField(
  669. queryset=Device.objects.all(),
  670. to_field_name='name',
  671. help_text='Side A device'
  672. )
  673. side_a_type = CSVContentTypeField(
  674. queryset=ContentType.objects.all(),
  675. limit_choices_to=CABLE_TERMINATION_MODELS,
  676. help_text='Side A type'
  677. )
  678. side_a_name = forms.CharField(
  679. help_text='Side A component name'
  680. )
  681. # Termination B
  682. side_b_device = CSVModelChoiceField(
  683. queryset=Device.objects.all(),
  684. to_field_name='name',
  685. help_text='Side B device'
  686. )
  687. side_b_type = CSVContentTypeField(
  688. queryset=ContentType.objects.all(),
  689. limit_choices_to=CABLE_TERMINATION_MODELS,
  690. help_text='Side B type'
  691. )
  692. side_b_name = forms.CharField(
  693. help_text='Side B component name'
  694. )
  695. # Cable attributes
  696. status = CSVChoiceField(
  697. choices=CableStatusChoices,
  698. required=False,
  699. help_text='Connection status'
  700. )
  701. type = CSVChoiceField(
  702. choices=CableTypeChoices,
  703. required=False,
  704. help_text='Physical medium classification'
  705. )
  706. length_unit = CSVChoiceField(
  707. choices=CableLengthUnitChoices,
  708. required=False,
  709. help_text='Length unit'
  710. )
  711. class Meta:
  712. model = Cable
  713. fields = [
  714. 'side_a_device', 'side_a_type', 'side_a_name', 'side_b_device', 'side_b_type', 'side_b_name', 'type',
  715. 'status', 'label', 'color', 'length', 'length_unit',
  716. ]
  717. help_texts = {
  718. 'color': mark_safe('RGB color in hexadecimal (e.g. <code>00ff00</code>)'),
  719. }
  720. def _clean_side(self, side):
  721. """
  722. Derive a Cable's A/B termination objects.
  723. :param side: 'a' or 'b'
  724. """
  725. assert side in 'ab', f"Invalid side designation: {side}"
  726. device = self.cleaned_data.get(f'side_{side}_device')
  727. content_type = self.cleaned_data.get(f'side_{side}_type')
  728. name = self.cleaned_data.get(f'side_{side}_name')
  729. if not device or not content_type or not name:
  730. return None
  731. model = content_type.model_class()
  732. try:
  733. termination_object = model.objects.get(device=device, name=name)
  734. if termination_object.cable is not None:
  735. raise forms.ValidationError(f"Side {side.upper()}: {device} {termination_object} is already connected")
  736. except ObjectDoesNotExist:
  737. raise forms.ValidationError(f"{side.upper()} side termination not found: {device} {name}")
  738. setattr(self.instance, f'termination_{side}', termination_object)
  739. return termination_object
  740. def clean_side_a_name(self):
  741. return self._clean_side('a')
  742. def clean_side_b_name(self):
  743. return self._clean_side('b')
  744. def clean_length_unit(self):
  745. # Avoid trying to save as NULL
  746. length_unit = self.cleaned_data.get('length_unit', None)
  747. return length_unit if length_unit is not None else ''
  748. class VirtualChassisCSVForm(CustomFieldModelCSVForm):
  749. master = CSVModelChoiceField(
  750. queryset=Device.objects.all(),
  751. to_field_name='name',
  752. required=False,
  753. help_text='Master device'
  754. )
  755. class Meta:
  756. model = VirtualChassis
  757. fields = ('name', 'domain', 'master')
  758. class PowerPanelCSVForm(CustomFieldModelCSVForm):
  759. site = CSVModelChoiceField(
  760. queryset=Site.objects.all(),
  761. to_field_name='name',
  762. help_text='Name of parent site'
  763. )
  764. location = CSVModelChoiceField(
  765. queryset=Location.objects.all(),
  766. required=False,
  767. to_field_name='name'
  768. )
  769. class Meta:
  770. model = PowerPanel
  771. fields = ('site', 'location', 'name')
  772. def __init__(self, data=None, *args, **kwargs):
  773. super().__init__(data, *args, **kwargs)
  774. if data:
  775. # Limit group queryset by assigned site
  776. params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
  777. self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
  778. class PowerFeedCSVForm(CustomFieldModelCSVForm):
  779. site = CSVModelChoiceField(
  780. queryset=Site.objects.all(),
  781. to_field_name='name',
  782. help_text='Assigned site'
  783. )
  784. power_panel = CSVModelChoiceField(
  785. queryset=PowerPanel.objects.all(),
  786. to_field_name='name',
  787. help_text='Upstream power panel'
  788. )
  789. location = CSVModelChoiceField(
  790. queryset=Location.objects.all(),
  791. to_field_name='name',
  792. required=False,
  793. help_text="Rack's location (if any)"
  794. )
  795. rack = CSVModelChoiceField(
  796. queryset=Rack.objects.all(),
  797. to_field_name='name',
  798. required=False,
  799. help_text='Rack'
  800. )
  801. status = CSVChoiceField(
  802. choices=PowerFeedStatusChoices,
  803. help_text='Operational status'
  804. )
  805. type = CSVChoiceField(
  806. choices=PowerFeedTypeChoices,
  807. help_text='Primary or redundant'
  808. )
  809. supply = CSVChoiceField(
  810. choices=PowerFeedSupplyChoices,
  811. help_text='Supply type (AC/DC)'
  812. )
  813. phase = CSVChoiceField(
  814. choices=PowerFeedPhaseChoices,
  815. help_text='Single or three-phase'
  816. )
  817. class Meta:
  818. model = PowerFeed
  819. fields = (
  820. 'site', 'power_panel', 'location', 'rack', 'name', 'status', 'type', 'mark_connected', 'supply', 'phase',
  821. 'voltage', 'amperage', 'max_utilization', 'comments',
  822. )
  823. def __init__(self, data=None, *args, **kwargs):
  824. super().__init__(data, *args, **kwargs)
  825. if data:
  826. # Limit power_panel queryset by site
  827. params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
  828. self.fields['power_panel'].queryset = self.fields['power_panel'].queryset.filter(**params)
  829. # Limit location queryset by site
  830. params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
  831. self.fields['location'].queryset = self.fields['location'].queryset.filter(**params)
  832. # Limit rack queryset by site and group
  833. params = {
  834. f"site__{self.fields['site'].to_field_name}": data.get('site'),
  835. f"location__{self.fields['location'].to_field_name}": data.get('location'),
  836. }
  837. self.fields['rack'].queryset = self.fields['rack'].queryset.filter(**params)