devices.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. from urllib.parse import quote
  2. import django_tables2 as tables
  3. from django.middleware.csrf import get_token
  4. from django.urls import reverse
  5. from django.utils.html import format_html
  6. from django.utils.safestring import mark_safe
  7. from django.utils.translation import gettext_lazy as _
  8. from django_tables2.utils import Accessor
  9. from dcim import models
  10. from netbox.tables import NestedGroupModelTable, NetBoxTable, OrganizationalModelTable, PrimaryModelTable, columns
  11. from tenancy.tables import ContactsColumnMixin, TenancyColumnsMixin
  12. from .template_code import *
  13. __all__ = (
  14. 'BaseInterfaceTable',
  15. 'CableTerminationTable',
  16. 'ConsolePortTable',
  17. 'ConsoleServerPortTable',
  18. 'DeviceBayTable',
  19. 'DeviceConsolePortTable',
  20. 'DeviceConsoleServerPortTable',
  21. 'DeviceDeviceBayTable',
  22. 'DeviceFrontPortTable',
  23. 'DeviceInterfaceTable',
  24. 'DeviceInventoryItemTable',
  25. 'DeviceModuleBayTable',
  26. 'DevicePowerOutletTable',
  27. 'DevicePowerPortTable',
  28. 'DeviceRearPortTable',
  29. 'DeviceRoleTable',
  30. 'DeviceTable',
  31. 'FrontPortTable',
  32. 'InterfaceLAGMemberTable',
  33. 'InterfaceTable',
  34. 'InventoryItemRoleTable',
  35. 'InventoryItemTable',
  36. 'MACAddressTable',
  37. 'MACBlockTable',
  38. 'ModuleBayTable',
  39. 'PlatformTable',
  40. 'PowerOutletTable',
  41. 'PowerPortTable',
  42. 'RearPortTable',
  43. 'VirtualChassisTable',
  44. 'VirtualDeviceContextTable'
  45. )
  46. MODULEBAY_STATUS = """
  47. {% badge record.installed_module.get_status_display bg_color=record.installed_module.get_status_color %}
  48. """
  49. MACADDRESS_LINK = """
  50. {% if record.pk %}
  51. <a href="{{ record.get_absolute_url }}" id="macaddress_{{ record.pk }}">{{ record.mac_address }}</a>
  52. {% endif %}
  53. """
  54. MACADDRESS_COPY_BUTTON = """
  55. {% copy_content record.pk prefix="macaddress_" %}
  56. """
  57. #
  58. # Device roles
  59. #
  60. class DeviceRoleTable(NestedGroupModelTable):
  61. device_count = columns.LinkedCountColumn(
  62. viewname='dcim:device_list',
  63. url_params={'role_id': 'pk'},
  64. verbose_name=_('Devices')
  65. )
  66. vm_count = columns.LinkedCountColumn(
  67. viewname='virtualization:virtualmachine_list',
  68. url_params={'role_id': 'pk'},
  69. verbose_name=_('VMs')
  70. )
  71. color = columns.ColorColumn()
  72. vm_role = columns.BooleanColumn(
  73. verbose_name=_('VM role'),
  74. false_mark=None
  75. )
  76. config_template = tables.Column(
  77. linkify=True
  78. )
  79. tags = columns.TagColumn(
  80. url_name='dcim:devicerole_list'
  81. )
  82. class Meta(NestedGroupModelTable.Meta):
  83. model = models.DeviceRole
  84. fields = (
  85. 'pk', 'id', 'name', 'parent', 'device_count', 'vm_count', 'color', 'vm_role', 'config_template',
  86. 'description', 'slug', 'tags', 'actions', 'created', 'last_updated',
  87. )
  88. default_columns = ('pk', 'name', 'device_count', 'vm_count', 'color', 'vm_role', 'description')
  89. #
  90. # Platforms
  91. #
  92. class PlatformTable(NestedGroupModelTable):
  93. manufacturer = tables.Column(
  94. verbose_name=_('Manufacturer'),
  95. linkify=True
  96. )
  97. config_template = tables.Column(
  98. verbose_name=_('Config Template'),
  99. linkify=True
  100. )
  101. device_count = columns.LinkedCountColumn(
  102. viewname='dcim:device_list',
  103. url_params={'platform_id': 'pk'},
  104. verbose_name=_('Devices')
  105. )
  106. vm_count = columns.LinkedCountColumn(
  107. viewname='virtualization:virtualmachine_list',
  108. url_params={'platform_id': 'pk'},
  109. verbose_name=_('VMs')
  110. )
  111. tags = columns.TagColumn(
  112. url_name='dcim:platform_list'
  113. )
  114. class Meta(NestedGroupModelTable.Meta):
  115. model = models.Platform
  116. fields = (
  117. 'pk', 'id', 'name', 'parent', 'manufacturer', 'device_count', 'vm_count', 'slug', 'config_template',
  118. 'description', 'tags', 'actions', 'created', 'last_updated',
  119. )
  120. default_columns = (
  121. 'pk', 'name', 'manufacturer', 'device_count', 'vm_count', 'description',
  122. )
  123. #
  124. # Devices
  125. #
  126. class DeviceTable(TenancyColumnsMixin, ContactsColumnMixin, PrimaryModelTable):
  127. name = tables.TemplateColumn(
  128. verbose_name=_('Name'),
  129. template_code=DEVICE_LINK,
  130. linkify=True,
  131. )
  132. status = columns.ChoiceFieldColumn(
  133. verbose_name=_('Status'),
  134. )
  135. region = tables.Column(
  136. verbose_name=_('Region'),
  137. accessor=Accessor('site__region'),
  138. linkify=True
  139. )
  140. site_group = tables.Column(
  141. accessor=Accessor('site__group'),
  142. linkify=True,
  143. verbose_name=_('Site Group')
  144. )
  145. site = tables.Column(
  146. verbose_name=_('Site'),
  147. linkify=True
  148. )
  149. location = tables.Column(
  150. verbose_name=_('Location'),
  151. linkify=True
  152. )
  153. rack = tables.Column(
  154. verbose_name=_('Rack'),
  155. linkify=True
  156. )
  157. position = columns.TemplateColumn(
  158. verbose_name=_('Position'),
  159. template_code='{{ value|floatformat }}'
  160. )
  161. role = columns.ColoredLabelColumn(
  162. verbose_name=_('Role')
  163. )
  164. manufacturer = tables.Column(
  165. verbose_name=_('Manufacturer'),
  166. accessor=Accessor('device_type__manufacturer'),
  167. linkify=True
  168. )
  169. device_type = tables.Column(
  170. linkify=True,
  171. verbose_name=_('Type')
  172. )
  173. u_height = columns.TemplateColumn(
  174. accessor=tables.A('device_type__u_height'),
  175. verbose_name=_('U Height'),
  176. template_code='{{ value|floatformat }}'
  177. )
  178. platform = tables.Column(
  179. linkify=True,
  180. verbose_name=_('Platform')
  181. )
  182. primary_ip = tables.Column(
  183. linkify=True,
  184. order_by=('primary_ip4', 'primary_ip6'),
  185. verbose_name=_('IP Address')
  186. )
  187. primary_ip4 = tables.Column(
  188. linkify=True,
  189. verbose_name=_('IPv4 Address')
  190. )
  191. primary_ip6 = tables.Column(
  192. linkify=True,
  193. verbose_name=_('IPv6 Address')
  194. )
  195. oob_ip = tables.Column(
  196. linkify=True,
  197. verbose_name='OOB IP'
  198. )
  199. cluster = tables.Column(
  200. verbose_name=_('Cluster'),
  201. linkify=True
  202. )
  203. virtual_chassis = tables.Column(
  204. verbose_name=_('Virtual Chassis'),
  205. linkify=True
  206. )
  207. vc_position = tables.Column(
  208. verbose_name=_('VC Position')
  209. )
  210. vc_priority = tables.Column(
  211. verbose_name=_('VC Priority')
  212. )
  213. config_template = tables.Column(
  214. verbose_name=_('Config Template'),
  215. linkify=True
  216. )
  217. parent_device = tables.Column(
  218. verbose_name=_('Parent Device'),
  219. linkify=True,
  220. accessor='parent_bay__device'
  221. )
  222. device_bay_position = tables.Column(
  223. verbose_name=_('Position (Device Bay)'),
  224. accessor='parent_bay',
  225. linkify=True
  226. )
  227. tags = columns.TagColumn(
  228. url_name='dcim:device_list'
  229. )
  230. console_port_count = tables.Column(
  231. verbose_name=_('Console ports')
  232. )
  233. console_server_port_count = tables.Column(
  234. verbose_name=_('Console server ports')
  235. )
  236. power_port_count = tables.Column(
  237. verbose_name=_('Power ports')
  238. )
  239. power_outlet_count = tables.Column(
  240. verbose_name=_('Power outlets')
  241. )
  242. cooling_intake_count = tables.Column(
  243. verbose_name=_('Cooling intakes')
  244. )
  245. cooling_outflow_count = tables.Column(
  246. verbose_name=_('Cooling outflows')
  247. )
  248. interface_count = tables.Column(
  249. verbose_name=_('Interfaces')
  250. )
  251. front_port_count = tables.Column(
  252. verbose_name=_('Front ports')
  253. )
  254. rear_port_count = tables.Column(
  255. verbose_name=_('Rear ports')
  256. )
  257. device_bay_count = tables.Column(
  258. verbose_name=_('Device bays')
  259. )
  260. module_bay_count = tables.Column(
  261. verbose_name=_('Module bays')
  262. )
  263. inventory_item_count = tables.Column(
  264. verbose_name=_('Inventory items')
  265. )
  266. cooling_method = columns.ChoiceFieldColumn(
  267. verbose_name=_('Cooling Method'),
  268. )
  269. class Meta(PrimaryModelTable.Meta):
  270. model = models.Device
  271. fields = (
  272. 'pk', 'id', 'name', 'status', 'tenant', 'tenant_group', 'role', 'manufacturer', 'device_type',
  273. 'serial', 'asset_tag', 'region', 'site_group', 'site', 'location', 'rack', 'parent_device',
  274. 'device_bay_position', 'position', 'face', 'latitude', 'longitude', 'airflow', 'cooling_method',
  275. 'primary_ip', 'primary_ip4',
  276. 'primary_ip6', 'oob_ip', 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'description',
  277. 'config_template', 'comments', 'contacts', 'tags', 'created', 'last_updated',
  278. )
  279. default_columns = (
  280. 'pk', 'name', 'status', 'tenant', 'site', 'location', 'rack', 'role', 'manufacturer', 'device_type',
  281. 'primary_ip',
  282. )
  283. #
  284. # Device components
  285. #
  286. class DeviceComponentTable(NetBoxTable):
  287. device = tables.Column(
  288. verbose_name=_('Device'),
  289. linkify=True
  290. )
  291. name = tables.Column(
  292. verbose_name=_('Name'),
  293. linkify=True,
  294. )
  295. device_location = tables.Column(
  296. accessor=tables.A('device__location'),
  297. verbose_name=_('Device Location'),
  298. linkify=True,
  299. )
  300. device_site = tables.Column(
  301. accessor=tables.A('device__site'),
  302. verbose_name=_('Device Site'),
  303. linkify=True,
  304. )
  305. device_status = columns.ChoiceFieldColumn(
  306. accessor=tables.A('device__status'),
  307. verbose_name=_('Device Status'),
  308. color=lambda x: x.device.get_status_color(),
  309. )
  310. class Meta(NetBoxTable.Meta):
  311. order_by = ('device', 'name')
  312. class ModularDeviceComponentTable(DeviceComponentTable):
  313. module_bay = tables.Column(
  314. verbose_name=_('Module Bay'),
  315. accessor=Accessor('module__module_bay'),
  316. linkify={
  317. 'viewname': 'dcim:device_modulebays',
  318. 'args': [Accessor('device_id')],
  319. }
  320. )
  321. module = tables.Column(
  322. verbose_name=_('Module'),
  323. linkify=True
  324. )
  325. inventory_items = columns.ManyToManyColumn(
  326. linkify_item=True,
  327. verbose_name=_('Inventory Items'),
  328. )
  329. class Meta(NetBoxTable.Meta):
  330. pass
  331. class CableTerminationTable(NetBoxTable):
  332. cable = tables.Column(
  333. verbose_name=_('Cable'),
  334. linkify=True
  335. )
  336. cable_color = columns.ColorColumn(
  337. accessor='cable__color',
  338. orderable=False,
  339. verbose_name=_('Cable Color')
  340. )
  341. link_peer = columns.TemplateColumn(
  342. accessor='link_peers',
  343. template_code=LINKTERMINATION,
  344. orderable=False,
  345. verbose_name=_('Link Peers')
  346. )
  347. mark_connected = columns.BooleanColumn(
  348. verbose_name=_('Mark Connected'),
  349. false_mark=None
  350. )
  351. class Meta:
  352. row_attrs = {
  353. 'data-name': lambda record: record.name,
  354. 'data-mark-connected': lambda record: "true" if record.mark_connected else "false",
  355. 'data-cable-status': lambda record: record.cable.status if record.cable else "",
  356. 'data-type': lambda record: record.type
  357. }
  358. def value_link_peer(self, value):
  359. return ', '.join([
  360. f"{termination.parent_object} > {termination}" for termination in value
  361. ])
  362. class PathEndpointTable(CableTerminationTable):
  363. connection = columns.TemplateColumn(
  364. accessor='_path__destinations',
  365. template_code=LINKTERMINATION,
  366. verbose_name=_('Connection'),
  367. orderable=False
  368. )
  369. def value_connection(self, value):
  370. if value:
  371. connections = []
  372. for termination in value:
  373. if hasattr(termination, 'parent_object'):
  374. connections.append(f'{termination.parent_object} > {termination}')
  375. else:
  376. connections.append(str(termination))
  377. return ', '.join(connections)
  378. return None
  379. class ConsolePortTable(ModularDeviceComponentTable, PathEndpointTable):
  380. device = tables.Column(
  381. verbose_name=_('Device'),
  382. linkify={
  383. 'viewname': 'dcim:device_consoleports',
  384. 'args': [Accessor('device_id')],
  385. }
  386. )
  387. tags = columns.TagColumn(
  388. url_name='dcim:consoleport_list'
  389. )
  390. class Meta(DeviceComponentTable.Meta):
  391. model = models.ConsolePort
  392. fields = (
  393. 'pk', 'id', 'name', 'device', 'module_bay', 'module', 'label', 'type', 'speed', 'description',
  394. 'mark_connected', 'cable', 'cable_color', 'link_peer', 'connection', 'inventory_items', 'tags', 'created',
  395. 'last_updated',
  396. )
  397. default_columns = ('pk', 'name', 'device', 'label', 'type', 'speed', 'description')
  398. class DeviceConsolePortTable(ConsolePortTable):
  399. name = tables.TemplateColumn(
  400. verbose_name=_('Name'),
  401. template_code='<i class="mdi mdi-console"></i> <a href="{{ record.get_absolute_url }}">{{ value }}</a>',
  402. attrs={'td': {'class': 'text-nowrap'}}
  403. )
  404. actions = columns.ActionsColumn(
  405. extra_buttons=CONSOLEPORT_BUTTONS
  406. )
  407. class Meta(CableTerminationTable.Meta, DeviceComponentTable.Meta):
  408. model = models.ConsolePort
  409. fields = (
  410. 'pk', 'id', 'name', 'module_bay', 'module', 'label', 'type', 'speed', 'description', 'mark_connected',
  411. 'cable', 'cable_color', 'link_peer', 'connection', 'tags', 'actions'
  412. )
  413. default_columns = ('pk', 'name', 'label', 'type', 'speed', 'description', 'cable', 'connection')
  414. class ConsoleServerPortTable(ModularDeviceComponentTable, PathEndpointTable):
  415. device = tables.Column(
  416. verbose_name=_('Device'),
  417. linkify={
  418. 'viewname': 'dcim:device_consoleserverports',
  419. 'args': [Accessor('device_id')],
  420. }
  421. )
  422. tags = columns.TagColumn(
  423. url_name='dcim:consoleserverport_list'
  424. )
  425. class Meta(DeviceComponentTable.Meta):
  426. model = models.ConsoleServerPort
  427. fields = (
  428. 'pk', 'id', 'name', 'device', 'module_bay', 'module', 'label', 'type', 'speed', 'description',
  429. 'mark_connected', 'cable', 'cable_color', 'link_peer', 'connection', 'inventory_items', 'tags', 'created',
  430. 'last_updated',
  431. )
  432. default_columns = ('pk', 'name', 'device', 'label', 'type', 'speed', 'description')
  433. class DeviceConsoleServerPortTable(ConsoleServerPortTable):
  434. name = tables.TemplateColumn(
  435. verbose_name=_('Name'),
  436. template_code='<i class="mdi mdi-console-network-outline"></i> '
  437. '<a href="{{ record.get_absolute_url }}">{{ value }}</a>',
  438. attrs={'td': {'class': 'text-nowrap'}}
  439. )
  440. actions = columns.ActionsColumn(
  441. extra_buttons=CONSOLESERVERPORT_BUTTONS
  442. )
  443. class Meta(CableTerminationTable.Meta, DeviceComponentTable.Meta):
  444. model = models.ConsoleServerPort
  445. fields = (
  446. 'pk', 'id', 'name', 'module_bay', 'module', 'label', 'type', 'speed', 'description', 'mark_connected',
  447. 'cable', 'cable_color', 'link_peer', 'connection', 'tags', 'actions',
  448. )
  449. default_columns = ('pk', 'name', 'label', 'type', 'speed', 'description', 'cable', 'connection')
  450. class PowerPortTable(ModularDeviceComponentTable, PathEndpointTable):
  451. device = tables.Column(
  452. verbose_name=_('Device'),
  453. linkify={
  454. 'viewname': 'dcim:device_powerports',
  455. 'args': [Accessor('device_id')],
  456. }
  457. )
  458. maximum_draw = tables.Column(
  459. verbose_name=_('Maximum draw (W)')
  460. )
  461. allocated_draw = tables.Column(
  462. verbose_name=_('Allocated draw (W)')
  463. )
  464. tags = columns.TagColumn(
  465. url_name='dcim:powerport_list'
  466. )
  467. class Meta(DeviceComponentTable.Meta):
  468. model = models.PowerPort
  469. fields = (
  470. 'pk', 'id', 'name', 'device', 'module_bay', 'module', 'label', 'type', 'description', 'mark_connected',
  471. 'maximum_draw', 'allocated_draw', 'cable', 'cable_color', 'link_peer', 'connection', 'inventory_items',
  472. 'tags', 'created', 'last_updated',
  473. )
  474. default_columns = ('pk', 'name', 'device', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description')
  475. class DevicePowerPortTable(PowerPortTable):
  476. name = tables.TemplateColumn(
  477. verbose_name=_('Name'),
  478. template_code='<i class="mdi mdi-power-plug-outline"></i> <a href="{{ record.get_absolute_url }}">'
  479. '{{ value }}</a>',
  480. attrs={'td': {'class': 'text-nowrap'}}
  481. )
  482. actions = columns.ActionsColumn(
  483. extra_buttons=POWERPORT_BUTTONS
  484. )
  485. class Meta(CableTerminationTable.Meta, DeviceComponentTable.Meta):
  486. model = models.PowerPort
  487. fields = (
  488. 'pk', 'id', 'name', 'module_bay', 'module', 'label', 'type', 'maximum_draw', 'allocated_draw',
  489. 'description', 'mark_connected', 'cable', 'cable_color', 'link_peer', 'connection', 'tags', 'actions',
  490. )
  491. default_columns = (
  492. 'pk', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description', 'cable', 'connection',
  493. )
  494. class PowerOutletTable(ModularDeviceComponentTable, PathEndpointTable):
  495. device = tables.Column(
  496. verbose_name=_('Device'),
  497. linkify={
  498. 'viewname': 'dcim:device_poweroutlets',
  499. 'args': [Accessor('device_id')],
  500. }
  501. )
  502. power_port = tables.Column(
  503. verbose_name=_('Power Port'),
  504. linkify=True
  505. )
  506. status = columns.ChoiceFieldColumn(
  507. verbose_name=_('Status'),
  508. )
  509. color = columns.ColorColumn()
  510. tags = columns.TagColumn(
  511. url_name='dcim:poweroutlet_list'
  512. )
  513. class Meta(DeviceComponentTable.Meta):
  514. model = models.PowerOutlet
  515. fields = (
  516. 'pk', 'id', 'name', 'device', 'module_bay', 'module', 'label', 'type', 'description', 'power_port',
  517. 'color', 'feed_leg', 'mark_connected', 'cable', 'cable_color', 'link_peer', 'connection', 'inventory_items',
  518. 'tags', 'created', 'last_updated', 'status',
  519. )
  520. default_columns = (
  521. 'pk', 'name', 'device', 'label', 'type', 'status', 'color', 'power_port', 'feed_leg', 'description',
  522. )
  523. class DevicePowerOutletTable(PowerOutletTable):
  524. name = tables.TemplateColumn(
  525. verbose_name=_('Name'),
  526. template_code='<i class="mdi mdi-power-socket"></i> <a href="{{ record.get_absolute_url }}">{{ value }}</a>',
  527. attrs={'td': {'class': 'text-nowrap'}}
  528. )
  529. actions = columns.ActionsColumn(
  530. extra_buttons=POWEROUTLET_BUTTONS
  531. )
  532. class Meta(CableTerminationTable.Meta, DeviceComponentTable.Meta):
  533. model = models.PowerOutlet
  534. fields = (
  535. 'pk', 'id', 'name', 'module_bay', 'module', 'label', 'type', 'color', 'power_port', 'feed_leg',
  536. 'description', 'mark_connected', 'cable', 'cable_color', 'link_peer', 'connection', 'tags', 'actions',
  537. 'status',
  538. )
  539. default_columns = (
  540. 'pk', 'name', 'label', 'type', 'status', 'color', 'power_port', 'feed_leg', 'description', 'cable',
  541. 'connection',
  542. )
  543. class BaseInterfaceTable(NetBoxTable):
  544. name = tables.Column(
  545. verbose_name=_('Name'),
  546. linkify=True,
  547. order_by=('_name',)
  548. )
  549. enabled = columns.BooleanColumn(
  550. verbose_name=_('Enabled'),
  551. )
  552. ip_addresses = tables.TemplateColumn(
  553. template_code=INTERFACE_IPADDRESSES,
  554. orderable=False,
  555. verbose_name=_('IP Addresses')
  556. )
  557. primary_mac_address = tables.Column(
  558. verbose_name=_('Primary MAC'),
  559. linkify=True
  560. )
  561. mac_addresses = columns.ManyToManyColumn(
  562. orderable=False,
  563. linkify_item=True,
  564. verbose_name=_('MAC Addresses')
  565. )
  566. fhrp_groups = tables.TemplateColumn(
  567. accessor=Accessor('fhrp_group_assignments'),
  568. template_code=INTERFACE_FHRPGROUPS,
  569. orderable=False,
  570. verbose_name=_('FHRP Groups')
  571. )
  572. l2vpn = tables.Column(
  573. accessor=tables.A('l2vpn_termination__l2vpn'),
  574. linkify=True,
  575. orderable=False,
  576. verbose_name=_('L2VPN')
  577. )
  578. tunnel = tables.Column(
  579. accessor=tables.A('tunnel_termination__tunnel'),
  580. linkify=True,
  581. orderable=False,
  582. verbose_name=_('Tunnel')
  583. )
  584. untagged_vlan = tables.Column(
  585. verbose_name=_('Untagged VLAN'),
  586. linkify=True
  587. )
  588. tagged_vlans = columns.TemplateColumn(
  589. template_code=INTERFACE_TAGGED_VLANS,
  590. orderable=False,
  591. verbose_name=_('Tagged VLANs')
  592. )
  593. qinq_svlan = tables.Column(
  594. verbose_name=_('Q-in-Q SVLAN'),
  595. linkify=True
  596. )
  597. def value_ip_addresses(self, value):
  598. return ",".join([str(obj.address) for obj in value.all()])
  599. def value_tagged_vlans(self, value):
  600. return ",".join([str(obj) for obj in value.all()])
  601. class InterfaceTable(BaseInterfaceTable, ModularDeviceComponentTable, PathEndpointTable):
  602. device = tables.Column(
  603. verbose_name=_('Device'),
  604. linkify={
  605. 'viewname': 'dcim:device_interfaces',
  606. 'args': [Accessor('device_id')],
  607. }
  608. )
  609. mgmt_only = columns.BooleanColumn(
  610. verbose_name=_('Management Only'),
  611. false_mark=None
  612. )
  613. speed_formatted = columns.TemplateColumn(
  614. template_code='{% load helpers %}{{ value|humanize_speed }}',
  615. accessor=Accessor('speed'),
  616. verbose_name=_('Speed')
  617. )
  618. wireless_link = tables.Column(
  619. verbose_name=_('Wireless link'),
  620. linkify=True
  621. )
  622. wireless_lans = columns.TemplateColumn(
  623. template_code=INTERFACE_WIRELESS_LANS,
  624. orderable=False,
  625. verbose_name=_('Wireless LANs')
  626. )
  627. vdcs = columns.ManyToManyColumn(
  628. linkify_item=True,
  629. verbose_name=_('VDCs')
  630. )
  631. vrf = tables.Column(
  632. verbose_name=_('VRF'),
  633. linkify=True
  634. )
  635. virtual_circuit_termination = tables.Column(
  636. verbose_name=_('Virtual Circuit'),
  637. linkify=True
  638. )
  639. tags = columns.TagColumn(
  640. url_name='dcim:interface_list'
  641. )
  642. # Override PathEndpointTable.connection to accommodate virtual circuits
  643. connection = columns.TemplateColumn(
  644. accessor='_path__destinations',
  645. template_code=INTERFACE_LINKTERMINATION,
  646. verbose_name=_('Connection'),
  647. orderable=False
  648. )
  649. def value_connection(self, record, value):
  650. if record.is_virtual and hasattr(record, 'virtual_circuit_termination') and record.virtual_circuit_termination:
  651. connections = [
  652. f"{t.interface.parent_object} > {t.interface} via {t.parent_object}"
  653. for t in record.connected_endpoints
  654. ]
  655. return ', '.join(connections)
  656. return super().value_connection(value)
  657. class Meta(DeviceComponentTable.Meta):
  658. model = models.Interface
  659. fields = (
  660. 'pk', 'id', 'name', 'device', 'module_bay', 'module', 'label', 'enabled', 'type', 'channels', 'channel_id',
  661. 'mgmt_only', 'mtu', 'speed', 'speed_formatted', 'duplex', 'mode', 'mac_addresses', 'primary_mac_address',
  662. 'wwn', 'poe_mode', 'poe_type', 'lag_mode', 'lag_hash_policy', 'rf_role', 'rf_channel',
  663. 'rf_channel_frequency', 'rf_channel_width', 'tx_power', 'description', 'mark_connected', 'cable',
  664. 'cable_color', 'wireless_link', 'wireless_lans', 'link_peer', 'connection', 'tags', 'vdcs', 'vrf', 'l2vpn',
  665. 'tunnel', 'ip_addresses', 'fhrp_groups', 'untagged_vlan', 'tagged_vlans', 'qinq_svlan', 'inventory_items',
  666. 'created', 'last_updated', 'vlan_translation_policy',
  667. )
  668. default_columns = ('pk', 'name', 'device', 'label', 'enabled', 'type', 'description')
  669. class InterfaceLAGMemberTable(PathEndpointTable, NetBoxTable):
  670. parent = tables.Column(
  671. verbose_name=_('Parent'),
  672. accessor=Accessor('device'),
  673. linkify=True,
  674. )
  675. name = tables.Column(
  676. verbose_name=_('Name'),
  677. linkify=True,
  678. order_by=('_name',),
  679. )
  680. connection = columns.TemplateColumn(
  681. accessor='connected_endpoints',
  682. template_code=INTERFACE_LAG_MEMBERS_LINKTERMINATION,
  683. verbose_name=_('Peer'),
  684. orderable=False,
  685. )
  686. tags = columns.TagColumn(
  687. url_name='dcim:interface_list'
  688. )
  689. class Meta(NetBoxTable.Meta):
  690. model = models.Interface
  691. fields = ('pk', 'parent', 'name', 'type', 'connection')
  692. default_columns = ('pk', 'parent', 'name', 'type', 'connection')
  693. class DeviceInterfaceTable(InterfaceTable):
  694. name = tables.TemplateColumn(
  695. verbose_name=_('Name'),
  696. template_code='<i class="mdi mdi-{% if record.mgmt_only %}wrench{% elif record.is_lag %}reorder-horizontal'
  697. '{% elif record.is_virtual %}circle{% elif record.is_wireless %}wifi{% else %}ethernet'
  698. '{% endif %}"></i> <a href="{{ record.get_absolute_url }}">{{ value }}</a>',
  699. order_by=Accessor('_name'),
  700. attrs={'td': {'class': 'text-nowrap'}}
  701. )
  702. parent = tables.Column(
  703. verbose_name=_('Parent'),
  704. linkify=True
  705. )
  706. bridge = tables.Column(
  707. verbose_name=_('Bridge'),
  708. linkify=True
  709. )
  710. lag = tables.Column(
  711. linkify=True,
  712. verbose_name=_('LAG')
  713. )
  714. actions = columns.ActionsColumn(
  715. extra_buttons=INTERFACE_BUTTONS
  716. )
  717. class Meta(DeviceComponentTable.Meta):
  718. model = models.Interface
  719. fields = (
  720. 'pk', 'id', 'name', 'module_bay', 'module', 'label', 'enabled', 'type', 'parent', 'bridge', 'lag',
  721. 'mgmt_only', 'mtu', 'mode', 'mac_addresses', 'primary_mac_address', 'wwn', 'rf_role', 'rf_channel',
  722. 'rf_channel_frequency', 'rf_channel_width', 'tx_power', 'description', 'mark_connected', 'cable',
  723. 'cable_color', 'wireless_link', 'wireless_lans', 'link_peer', 'connection', 'tags', 'vdcs', 'vrf',
  724. 'l2vpn', 'tunnel', 'ip_addresses', 'fhrp_groups', 'untagged_vlan', 'tagged_vlans', 'qinq_svlan',
  725. 'actions',
  726. )
  727. default_columns = (
  728. 'pk', 'name', 'label', 'enabled', 'type', 'parent', 'lag', 'mtu', 'mode', 'description', 'ip_addresses',
  729. 'cable', 'connection',
  730. )
  731. row_attrs = {
  732. 'data-name': lambda record: record.name,
  733. 'data-enabled': lambda record: "enabled" if record.enabled else "disabled",
  734. 'data-virtual': lambda record: "true" if record.is_virtual else "false",
  735. 'data-mark-connected': lambda record: "true" if record.mark_connected else "false",
  736. 'data-cable-status': lambda record: record.cable.status if record.cable else "",
  737. }
  738. class FrontPortTable(ModularDeviceComponentTable, CableTerminationTable):
  739. device = tables.Column(
  740. verbose_name=_('Device'),
  741. linkify={
  742. 'viewname': 'dcim:device_frontports',
  743. 'args': [Accessor('device_id')],
  744. }
  745. )
  746. color = columns.ColorColumn(
  747. verbose_name=_('Color'),
  748. )
  749. mappings = columns.ManyToManyColumn(
  750. verbose_name=_('Mappings'),
  751. transform=lambda obj: f'{obj.rear_port}:{obj.rear_port_position}'
  752. )
  753. tags = columns.TagColumn(
  754. url_name='dcim:frontport_list'
  755. )
  756. class Meta(DeviceComponentTable.Meta):
  757. model = models.FrontPort
  758. fields = (
  759. 'pk', 'id', 'name', 'device', 'module_bay', 'module', 'label', 'type', 'color', 'positions', 'mappings',
  760. 'description', 'mark_connected', 'cable', 'cable_color', 'link_peer', 'inventory_items', 'tags', 'created',
  761. 'last_updated',
  762. )
  763. default_columns = (
  764. 'pk', 'name', 'device', 'label', 'type', 'color', 'positions', 'mappings', 'description',
  765. )
  766. class DeviceFrontPortTable(FrontPortTable):
  767. name = tables.TemplateColumn(
  768. verbose_name=_('Name'),
  769. template_code='<i class="mdi mdi-square-rounded{% if not record.cable %}-outline{% endif %}"></i> '
  770. '<a href="{{ record.get_absolute_url }}">{{ value }}</a>',
  771. attrs={'td': {'class': 'text-nowrap'}}
  772. )
  773. actions = columns.ActionsColumn(
  774. extra_buttons=FRONTPORT_BUTTONS
  775. )
  776. class Meta(CableTerminationTable.Meta, DeviceComponentTable.Meta):
  777. model = models.FrontPort
  778. fields = (
  779. 'pk', 'id', 'name', 'module_bay', 'module', 'label', 'type', 'color', 'positions', 'mappings',
  780. 'description', 'mark_connected', 'cable', 'cable_color', 'link_peer', 'tags', 'actions',
  781. )
  782. default_columns = (
  783. 'pk', 'name', 'label', 'type', 'color', 'positions', 'mappings', 'description', 'cable', 'link_peer',
  784. )
  785. class RearPortTable(ModularDeviceComponentTable, CableTerminationTable):
  786. device = tables.Column(
  787. verbose_name=_('Device'),
  788. linkify={
  789. 'viewname': 'dcim:device_rearports',
  790. 'args': [Accessor('device_id')],
  791. }
  792. )
  793. color = columns.ColorColumn(
  794. verbose_name=_('Color'),
  795. )
  796. mappings = columns.ManyToManyColumn(
  797. verbose_name=_('Mappings'),
  798. transform=lambda obj: f'{obj.front_port}:{obj.front_port_position}'
  799. )
  800. tags = columns.TagColumn(
  801. url_name='dcim:rearport_list'
  802. )
  803. class Meta(DeviceComponentTable.Meta):
  804. model = models.RearPort
  805. fields = (
  806. 'pk', 'id', 'name', 'device', 'module_bay', 'module', 'label', 'type', 'color', 'positions', 'mappings',
  807. 'description', 'mark_connected', 'cable', 'cable_color', 'link_peer', 'inventory_items', 'tags', 'created',
  808. 'last_updated',
  809. )
  810. default_columns = (
  811. 'pk', 'name', 'device', 'label', 'type', 'color', 'positions', 'mappings', 'description',
  812. )
  813. class DeviceRearPortTable(RearPortTable):
  814. name = tables.TemplateColumn(
  815. verbose_name=_('Name'),
  816. template_code='<i class="mdi mdi-square-rounded{% if not record.cable %}-outline{% endif %}"></i> '
  817. '<a href="{{ record.get_absolute_url }}">{{ value }}</a>',
  818. attrs={'td': {'class': 'text-nowrap'}}
  819. )
  820. actions = columns.ActionsColumn(
  821. extra_buttons=REARPORT_BUTTONS
  822. )
  823. class Meta(CableTerminationTable.Meta, DeviceComponentTable.Meta):
  824. model = models.RearPort
  825. fields = (
  826. 'pk', 'id', 'name', 'module_bay', 'module', 'label', 'type', 'color', 'positions', 'mappings',
  827. 'description', 'mark_connected', 'cable', 'cable_color', 'link_peer', 'tags', 'actions',
  828. )
  829. default_columns = (
  830. 'pk', 'name', 'label', 'type', 'color', 'positions', 'mappings', 'description', 'cable', 'link_peer',
  831. )
  832. class DeviceBayTable(DeviceComponentTable):
  833. device = tables.Column(
  834. verbose_name=_('Device'),
  835. linkify={
  836. 'viewname': 'dcim:device_devicebays',
  837. 'args': [Accessor('device_id')],
  838. }
  839. )
  840. enabled = columns.BooleanColumn(
  841. verbose_name=_('Enabled'),
  842. )
  843. status = tables.TemplateColumn(
  844. verbose_name=_('Status'),
  845. template_code=DEVICEBAY_STATUS,
  846. order_by=Accessor('installed_device__status')
  847. )
  848. installed_device = tables.Column(
  849. verbose_name=_('Installed Device'),
  850. linkify=True
  851. )
  852. installed_role = columns.ColoredLabelColumn(
  853. accessor=Accessor('installed_device__role'),
  854. verbose_name=_('Installed Role')
  855. )
  856. installed_device_type = tables.Column(
  857. accessor=Accessor('installed_device__device_type'),
  858. linkify=True,
  859. verbose_name=_('Installed Type')
  860. )
  861. installed_description = tables.Column(
  862. accessor=Accessor('installed_device__description'),
  863. verbose_name=_('Installed Description')
  864. )
  865. installed_serial = tables.Column(
  866. accessor=Accessor('installed_device__serial'),
  867. verbose_name=_('Installed Serial')
  868. )
  869. installed_asset_tag = tables.Column(
  870. accessor=Accessor('installed_device__asset_tag'),
  871. verbose_name=_('Installed Asset Tag')
  872. )
  873. tags = columns.TagColumn(
  874. url_name='dcim:devicebay_list'
  875. )
  876. class Meta(DeviceComponentTable.Meta):
  877. model = models.DeviceBay
  878. fields = (
  879. 'pk', 'id', 'name', 'device', 'label', 'enabled', 'status', 'description', 'installed_device',
  880. 'installed_role', 'installed_device_type', 'installed_description', 'installed_serial',
  881. 'installed_asset_tag', 'tags', 'created', 'last_updated',
  882. )
  883. default_columns = ('pk', 'name', 'device', 'label', 'enabled', 'status', 'installed_device', 'description')
  884. class DeviceDeviceBayTable(DeviceBayTable):
  885. name = tables.TemplateColumn(
  886. verbose_name=_('Name'),
  887. template_code='<i class="mdi mdi-circle{% if record.installed_device %}slice-8{% else %}outline{% endif %}'
  888. '"></i> <a href="{{ record.get_absolute_url }}">{{ value }}</a>',
  889. attrs={'td': {'class': 'text-nowrap'}}
  890. )
  891. enabled = columns.BooleanColumn(
  892. verbose_name=_('Enabled'),
  893. )
  894. actions = columns.ActionsColumn(
  895. extra_buttons=DEVICEBAY_BUTTONS
  896. )
  897. class Meta(DeviceComponentTable.Meta):
  898. model = models.DeviceBay
  899. fields = (
  900. 'pk', 'id', 'name', 'label', 'enabled', 'status', 'installed_device', 'description', 'tags', 'actions',
  901. )
  902. default_columns = ('pk', 'name', 'label', 'enabled', 'status', 'installed_device', 'description')
  903. class ModuleBayTable(ModularDeviceComponentTable):
  904. device = tables.Column(
  905. verbose_name=_('Device'),
  906. linkify={
  907. 'viewname': 'dcim:device_modulebays',
  908. 'args': [Accessor('device_id')],
  909. }
  910. )
  911. enabled = columns.BooleanColumn(
  912. verbose_name=_('Enabled'),
  913. )
  914. parent = tables.Column(
  915. linkify=True,
  916. verbose_name=_('Parent'),
  917. )
  918. installed_module = tables.Column(
  919. linkify=True,
  920. verbose_name=_('Installed Module')
  921. )
  922. module_serial = tables.Column(
  923. verbose_name=_('Module Serial'),
  924. accessor=tables.A('installed_module__serial')
  925. )
  926. module_asset_tag = tables.Column(
  927. verbose_name=_('Module Asset Tag'),
  928. accessor=tables.A('installed_module__asset_tag')
  929. )
  930. tags = columns.TagColumn(
  931. url_name='dcim:modulebay_list'
  932. )
  933. module_status = columns.TemplateColumn(
  934. accessor=tables.A('installed_module__status'),
  935. template_code=MODULEBAY_STATUS,
  936. verbose_name=_('Module Status')
  937. )
  938. module_bay_types = columns.ManyToManyColumn(
  939. verbose_name=_('Bay Types'),
  940. linkify_item=True,
  941. )
  942. class Meta(ModularDeviceComponentTable.Meta):
  943. model = models.ModuleBay
  944. fields = (
  945. 'pk', 'id', 'name', 'device', 'enabled', 'parent', 'label', 'position', 'module_bay_types',
  946. 'installed_module', 'module_status', 'module_serial', 'module_asset_tag', 'description', 'tags',
  947. )
  948. default_columns = (
  949. 'pk', 'name', 'device', 'enabled', 'parent', 'label', 'installed_module', 'module_status', 'description',
  950. )
  951. def render_parent_bay(self, value):
  952. return value.name if value else ''
  953. def render_installed_module(self, value):
  954. return value.module_type if value else ''
  955. class DeviceModuleBayTable(ModuleBayTable):
  956. name = columns.MPTTColumn(
  957. verbose_name=_('Name'),
  958. linkify=True,
  959. )
  960. enabled = columns.BooleanColumn(
  961. verbose_name=_('Enabled'),
  962. )
  963. actions = columns.ActionsColumn(
  964. extra_buttons=MODULEBAY_BUTTONS
  965. )
  966. class Meta(ModuleBayTable.Meta):
  967. model = models.ModuleBay
  968. fields = (
  969. 'pk', 'id', 'parent', 'name', 'label', 'enabled', 'position', 'installed_module', 'module_status',
  970. 'module_serial', 'module_asset_tag', 'description', 'tags', 'actions',
  971. )
  972. default_columns = ('pk', 'name', 'label', 'enabled', 'installed_module', 'module_status', 'description')
  973. class InventoryItemTable(DeviceComponentTable):
  974. device = tables.Column(
  975. verbose_name=_('Device'),
  976. linkify={
  977. 'viewname': 'dcim:device_inventory',
  978. 'args': [Accessor('device_id')],
  979. }
  980. )
  981. role = columns.ColoredLabelColumn(
  982. verbose_name=_('Role'),
  983. )
  984. manufacturer = tables.Column(
  985. verbose_name=_('Manufacturer'),
  986. linkify=True
  987. )
  988. component = tables.Column(
  989. verbose_name=_('Component'),
  990. orderable=False,
  991. linkify=True
  992. )
  993. discovered = columns.BooleanColumn(
  994. verbose_name=_('Discovered'),
  995. false_mark=None
  996. )
  997. status = columns.ChoiceFieldColumn(
  998. verbose_name=_('Status'),
  999. )
  1000. parent = tables.Column(
  1001. linkify=True,
  1002. verbose_name=_('Parent'),
  1003. )
  1004. tags = columns.TagColumn(
  1005. url_name='dcim:inventoryitem_list'
  1006. )
  1007. cable = None # Override DeviceComponentTable
  1008. class Meta(NetBoxTable.Meta):
  1009. model = models.InventoryItem
  1010. fields = (
  1011. 'pk', 'id', 'name', 'device', 'parent', 'component', 'label', 'status', 'role', 'manufacturer', 'part_id',
  1012. 'serial', 'asset_tag', 'description', 'discovered', 'tags', 'created', 'last_updated',
  1013. )
  1014. default_columns = (
  1015. 'pk', 'name', 'device', 'label', 'status', 'role', 'manufacturer', 'part_id', 'serial', 'asset_tag',
  1016. )
  1017. class DeviceInventoryItemTable(InventoryItemTable):
  1018. name = tables.TemplateColumn(
  1019. verbose_name=_('Name'),
  1020. template_code='<a href="{{ record.get_absolute_url }}" style="padding-left: {{ record.level }}0px">'
  1021. '{{ value }}</a>',
  1022. attrs={'td': {'class': 'text-nowrap'}}
  1023. )
  1024. class Meta(NetBoxTable.Meta):
  1025. model = models.InventoryItem
  1026. fields = (
  1027. 'pk', 'id', 'name', 'label', 'status', 'role', 'manufacturer', 'part_id', 'serial', 'asset_tag',
  1028. 'component', 'description', 'discovered', 'tags', 'actions',
  1029. )
  1030. default_columns = (
  1031. 'pk', 'name', 'label', 'status', 'role', 'manufacturer', 'part_id', 'serial', 'asset_tag', 'component',
  1032. )
  1033. class InventoryItemRoleTable(OrganizationalModelTable):
  1034. name = tables.Column(
  1035. verbose_name=_('Name'),
  1036. linkify=True
  1037. )
  1038. inventoryitem_count = columns.LinkedCountColumn(
  1039. viewname='dcim:inventoryitem_list',
  1040. url_params={'role_id': 'pk'},
  1041. verbose_name=_('Items')
  1042. )
  1043. color = columns.ColorColumn(
  1044. verbose_name=_('Color'),
  1045. )
  1046. tags = columns.TagColumn(
  1047. url_name='dcim:inventoryitemrole_list'
  1048. )
  1049. class Meta(OrganizationalModelTable.Meta):
  1050. model = models.InventoryItemRole
  1051. fields = (
  1052. 'pk', 'id', 'name', 'inventoryitem_count', 'color', 'description', 'slug', 'comments', 'tags', 'actions',
  1053. )
  1054. default_columns = ('pk', 'name', 'inventoryitem_count', 'color', 'description')
  1055. #
  1056. # Virtual chassis
  1057. #
  1058. class VirtualChassisTable(PrimaryModelTable):
  1059. name = tables.Column(
  1060. verbose_name=_('Name'),
  1061. linkify=True
  1062. )
  1063. master = tables.Column(
  1064. verbose_name=_('Master'),
  1065. linkify=True
  1066. )
  1067. member_count = columns.LinkedCountColumn(
  1068. viewname='dcim:device_list',
  1069. url_params={'virtual_chassis_id': 'pk'},
  1070. verbose_name=_('Members')
  1071. )
  1072. tags = columns.TagColumn(
  1073. url_name='dcim:virtualchassis_list'
  1074. )
  1075. class Meta(PrimaryModelTable.Meta):
  1076. model = models.VirtualChassis
  1077. fields = (
  1078. 'pk', 'id', 'name', 'domain', 'master', 'member_count', 'description', 'comments', 'tags', 'created',
  1079. 'last_updated',
  1080. )
  1081. default_columns = ('pk', 'name', 'domain', 'master', 'member_count')
  1082. class VirtualDeviceContextTable(TenancyColumnsMixin, PrimaryModelTable):
  1083. name = tables.Column(
  1084. verbose_name=_('Name'),
  1085. linkify=True
  1086. )
  1087. device = tables.Column(
  1088. verbose_name=_('Device'),
  1089. order_by=('device__name',),
  1090. linkify=True
  1091. )
  1092. status = columns.ChoiceFieldColumn(
  1093. verbose_name=_('Status'),
  1094. )
  1095. primary_ip = tables.Column(
  1096. linkify=True,
  1097. order_by=('primary_ip4', 'primary_ip6'),
  1098. verbose_name=_('IP Address')
  1099. )
  1100. primary_ip4 = tables.Column(
  1101. linkify=True,
  1102. verbose_name=_('IPv4 Address')
  1103. )
  1104. primary_ip6 = tables.Column(
  1105. linkify=True,
  1106. verbose_name=_('IPv6 Address')
  1107. )
  1108. interface_count = columns.LinkedCountColumn(
  1109. viewname='dcim:interface_list',
  1110. url_params={'vdc_id': 'pk'},
  1111. verbose_name=_('Interfaces')
  1112. )
  1113. tags = columns.TagColumn(
  1114. url_name='dcim:virtualdevicecontext_list'
  1115. )
  1116. class Meta(PrimaryModelTable.Meta):
  1117. model = models.VirtualDeviceContext
  1118. fields = (
  1119. 'pk', 'id', 'name', 'status', 'identifier', 'tenant', 'tenant_group', 'primary_ip', 'primary_ip4',
  1120. 'primary_ip6', 'comments', 'tags', 'interface_count', 'created', 'last_updated',
  1121. )
  1122. default_columns = (
  1123. 'pk', 'name', 'identifier', 'status', 'tenant', 'primary_ip',
  1124. )
  1125. class MACAddressActionsColumn(columns.ActionsColumn):
  1126. actions = {
  1127. **columns.ActionsColumn.actions,
  1128. 'set_primary': columns.ActionsItem('Set as primary', 'star-outline', None, 'warning'),
  1129. }
  1130. def render(self, record, table, **kwargs):
  1131. # Always exclude set_primary from the parent's action loop (which renders GET links).
  1132. # We inject a CSRF-protected POST form for set_primary in the dropdown below.
  1133. show_set_primary = not record.is_primary and record.assigned_object_id
  1134. original_actions = self.actions
  1135. self.actions = {k: v for k, v in original_actions.items() if k != 'set_primary'}
  1136. try:
  1137. html = super().render(record, table, **kwargs)
  1138. finally:
  1139. self.actions = original_actions
  1140. if show_set_primary and html:
  1141. request = getattr(table, 'context', {}).get('request')
  1142. if request:
  1143. url = reverse('dcim:macaddress_set_primary', kwargs={'pk': record.pk})
  1144. # Return the user where they came from, the same way the parent's GET actions
  1145. # (edit/delete/changelog) do. In an embedded panel ObjectsTablePanel injects the parent
  1146. # object's URL as ?return_url=, so this lands on the interface; on the list view it falls
  1147. # back to the list path.
  1148. return_url = request.GET.get('return_url', request.get_full_path())
  1149. url = f'{url}?return_url={quote(return_url)}'
  1150. # Embedded tables need their own form; list tables reuse the surrounding bulk form.
  1151. if getattr(table, 'embedded', False):
  1152. # No surrounding form: a self-contained POST form is valid and carries its own CSRF token.
  1153. action_li = format_html(
  1154. '<li><form method="post" action="{}">'
  1155. '<input type="hidden" name="csrfmiddlewaretoken" value="{}">'
  1156. '<button type="submit" class="dropdown-item">'
  1157. '<i class="mdi mdi-star-outline"></i> {}'
  1158. '</button></form></li>',
  1159. url, get_token(request), _('Set as primary'),
  1160. )
  1161. else:
  1162. # Inside the bulk-edit <form>: a nested <form> is invalid HTML and gets dropped by the
  1163. # parser, so ride the surrounding form via formaction/formmethod instead (matching the
  1164. # DataSource sync button in core/tables/template_code.py).
  1165. action_li = format_html(
  1166. '<li><button type="submit" formaction="{}" formmethod="post" class="dropdown-item">'
  1167. '<i class="mdi mdi-star-outline"></i> {}'
  1168. '</button></li>',
  1169. url, _('Set as primary'),
  1170. )
  1171. html_str = str(html)
  1172. if '</ul>' in html_str:
  1173. html = mark_safe(html_str.replace('</ul>', str(action_li) + '</ul>', 1))
  1174. return html
  1175. class MACAddressTable(PrimaryModelTable):
  1176. mac_address = tables.TemplateColumn(
  1177. template_code=MACADDRESS_LINK,
  1178. verbose_name=_('MAC Address')
  1179. )
  1180. assigned_object = tables.Column(
  1181. linkify=True,
  1182. orderable=False,
  1183. verbose_name=_('Interface')
  1184. )
  1185. assigned_object_parent = tables.Column(
  1186. accessor='assigned_object__parent_object',
  1187. linkify=True,
  1188. orderable=False,
  1189. verbose_name=_('Parent')
  1190. )
  1191. is_primary = columns.BooleanColumn(
  1192. verbose_name=_('Primary'),
  1193. orderable=False,
  1194. )
  1195. tags = columns.TagColumn(
  1196. url_name='dcim:macaddress_list'
  1197. )
  1198. actions = MACAddressActionsColumn(
  1199. actions=('edit', 'delete', 'changelog', 'set_primary'),
  1200. extra_buttons=MACADDRESS_COPY_BUTTON
  1201. )
  1202. class Meta(PrimaryModelTable.Meta):
  1203. model = models.MACAddress
  1204. fields = (
  1205. 'pk', 'id', 'mac_address', 'assigned_object_parent', 'assigned_object', 'description', 'is_primary',
  1206. 'comments', 'tags', 'created', 'last_updated',
  1207. )
  1208. default_columns = (
  1209. 'pk', 'mac_address', 'is_primary', 'assigned_object_parent', 'assigned_object', 'description',
  1210. )
  1211. class MACBlockTable(PrimaryModelTable):
  1212. prefix = tables.Column(
  1213. verbose_name=_('Prefix'),
  1214. linkify=True
  1215. )
  1216. prefix_length = tables.Column(
  1217. verbose_name=_('Length')
  1218. )
  1219. name = tables.Column(
  1220. verbose_name=_('Name')
  1221. )
  1222. size = tables.Column(
  1223. verbose_name=_('Size'),
  1224. orderable=False
  1225. )
  1226. mac_address_count = columns.LinkedCountColumn(
  1227. viewname='dcim:macaddress_list',
  1228. url_params={'mac_block_id': 'pk'},
  1229. verbose_name=_('MAC Addresses'),
  1230. orderable=False
  1231. )
  1232. utilization = columns.UtilizationColumn(
  1233. verbose_name=_('Utilization'),
  1234. orderable=False
  1235. )
  1236. enforce_unique = columns.BooleanColumn(
  1237. verbose_name=_('Enforce Unique')
  1238. )
  1239. tags = columns.TagColumn(
  1240. url_name='dcim:macblock_list'
  1241. )
  1242. def render_prefix(self, record):
  1243. # Include the prefix length, to distinguish blocks which start at the same address
  1244. return str(record)
  1245. def value_prefix(self, record):
  1246. return str(record.prefix)
  1247. class Meta(PrimaryModelTable.Meta):
  1248. model = models.MACBlock
  1249. fields = (
  1250. 'pk', 'id', 'prefix', 'prefix_length', 'name', 'size', 'mac_address_count', 'utilization',
  1251. 'enforce_unique', 'description', 'comments', 'tags', 'created', 'last_updated',
  1252. )
  1253. default_columns = (
  1254. 'pk', 'prefix', 'name', 'mac_address_count', 'utilization', 'enforce_unique', 'description',
  1255. )