tables.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import django_tables2 as tables
  2. from django.conf import settings
  3. from django.utils.translation import gettext as _
  4. from extras.models import *
  5. from netbox.tables import NetBoxTable, columns
  6. from .template_code import *
  7. __all__ = (
  8. 'ConfigContextTable',
  9. 'ConfigTemplateTable',
  10. 'CustomFieldTable',
  11. 'CustomLinkTable',
  12. 'ExportTemplateTable',
  13. 'JobResultTable',
  14. 'JournalEntryTable',
  15. 'ObjectChangeTable',
  16. 'SavedFilterTable',
  17. 'TaggedItemTable',
  18. 'TagTable',
  19. 'WebhookTable',
  20. )
  21. class CustomFieldTable(NetBoxTable):
  22. name = tables.Column(
  23. linkify=True
  24. )
  25. content_types = columns.ContentTypesColumn()
  26. required = columns.BooleanColumn()
  27. ui_visibility = columns.ChoiceFieldColumn(verbose_name="UI visibility")
  28. class Meta(NetBoxTable.Meta):
  29. model = CustomField
  30. fields = (
  31. 'pk', 'id', 'name', 'content_types', 'label', 'type', 'group_name', 'required', 'default', 'description',
  32. 'search_weight', 'filter_logic', 'ui_visibility', 'weight', 'choices', 'created', 'last_updated',
  33. )
  34. default_columns = ('pk', 'name', 'content_types', 'label', 'group_name', 'type', 'required', 'description')
  35. class JobResultTable(NetBoxTable):
  36. name = tables.Column(
  37. linkify=True
  38. )
  39. obj_type = columns.ContentTypeColumn(
  40. verbose_name=_('Type')
  41. )
  42. status = columns.ChoiceFieldColumn()
  43. created = columns.DateTimeColumn()
  44. scheduled = columns.DateTimeColumn()
  45. interval = columns.DurationColumn()
  46. started = columns.DateTimeColumn()
  47. completed = columns.DateTimeColumn()
  48. actions = columns.ActionsColumn(
  49. actions=('delete',)
  50. )
  51. class Meta(NetBoxTable.Meta):
  52. model = JobResult
  53. fields = (
  54. 'pk', 'id', 'obj_type', 'name', 'status', 'created', 'scheduled', 'interval', 'started', 'completed',
  55. 'user', 'job_id',
  56. )
  57. default_columns = (
  58. 'pk', 'id', 'obj_type', 'name', 'status', 'created', 'scheduled', 'interval', 'started', 'completed',
  59. 'user',
  60. )
  61. class CustomLinkTable(NetBoxTable):
  62. name = tables.Column(
  63. linkify=True
  64. )
  65. content_types = columns.ContentTypesColumn()
  66. enabled = columns.BooleanColumn()
  67. new_window = columns.BooleanColumn()
  68. class Meta(NetBoxTable.Meta):
  69. model = CustomLink
  70. fields = (
  71. 'pk', 'id', 'name', 'content_types', 'enabled', 'link_text', 'link_url', 'weight', 'group_name',
  72. 'button_class', 'new_window', 'created', 'last_updated',
  73. )
  74. default_columns = ('pk', 'name', 'content_types', 'enabled', 'group_name', 'button_class', 'new_window')
  75. class ExportTemplateTable(NetBoxTable):
  76. name = tables.Column(
  77. linkify=True
  78. )
  79. content_types = columns.ContentTypesColumn()
  80. as_attachment = columns.BooleanColumn()
  81. data_source = tables.Column(
  82. linkify=True
  83. )
  84. data_file = tables.Column(
  85. linkify=True
  86. )
  87. is_synced = columns.BooleanColumn(
  88. verbose_name='Synced'
  89. )
  90. class Meta(NetBoxTable.Meta):
  91. model = ExportTemplate
  92. fields = (
  93. 'pk', 'id', 'name', 'content_types', 'description', 'mime_type', 'file_extension', 'as_attachment',
  94. 'data_source', 'data_file', 'data_synced', 'created', 'last_updated',
  95. )
  96. default_columns = (
  97. 'pk', 'name', 'content_types', 'description', 'mime_type', 'file_extension', 'as_attachment', 'is_synced',
  98. )
  99. class SavedFilterTable(NetBoxTable):
  100. name = tables.Column(
  101. linkify=True
  102. )
  103. content_types = columns.ContentTypesColumn()
  104. enabled = columns.BooleanColumn()
  105. shared = columns.BooleanColumn()
  106. class Meta(NetBoxTable.Meta):
  107. model = SavedFilter
  108. fields = (
  109. 'pk', 'id', 'name', 'slug', 'content_types', 'description', 'user', 'weight', 'enabled', 'shared',
  110. 'created', 'last_updated',
  111. )
  112. default_columns = (
  113. 'pk', 'name', 'content_types', 'user', 'description', 'enabled', 'shared',
  114. )
  115. class WebhookTable(NetBoxTable):
  116. name = tables.Column(
  117. linkify=True
  118. )
  119. content_types = columns.ContentTypesColumn()
  120. enabled = columns.BooleanColumn()
  121. type_create = columns.BooleanColumn(
  122. verbose_name='Create'
  123. )
  124. type_update = columns.BooleanColumn(
  125. verbose_name='Update'
  126. )
  127. type_delete = columns.BooleanColumn(
  128. verbose_name='Delete'
  129. )
  130. ssl_validation = columns.BooleanColumn(
  131. verbose_name='SSL Validation'
  132. )
  133. class Meta(NetBoxTable.Meta):
  134. model = Webhook
  135. fields = (
  136. 'pk', 'id', 'name', 'content_types', 'enabled', 'type_create', 'type_update', 'type_delete', 'http_method',
  137. 'payload_url', 'secret', 'ssl_validation', 'ca_file_path', 'created', 'last_updated',
  138. )
  139. default_columns = (
  140. 'pk', 'name', 'content_types', 'enabled', 'type_create', 'type_update', 'type_delete', 'http_method',
  141. 'payload_url',
  142. )
  143. class TagTable(NetBoxTable):
  144. name = tables.Column(
  145. linkify=True
  146. )
  147. color = columns.ColorColumn()
  148. class Meta(NetBoxTable.Meta):
  149. model = Tag
  150. fields = ('pk', 'id', 'name', 'items', 'slug', 'color', 'description', 'created', 'last_updated', 'actions')
  151. default_columns = ('pk', 'name', 'items', 'slug', 'color', 'description')
  152. class TaggedItemTable(NetBoxTable):
  153. id = tables.Column(
  154. verbose_name='ID',
  155. linkify=lambda record: record.content_object.get_absolute_url(),
  156. accessor='content_object__id'
  157. )
  158. content_type = columns.ContentTypeColumn(
  159. verbose_name='Type'
  160. )
  161. content_object = tables.Column(
  162. linkify=True,
  163. orderable=False,
  164. verbose_name='Object'
  165. )
  166. actions = columns.ActionsColumn(
  167. actions=()
  168. )
  169. class Meta(NetBoxTable.Meta):
  170. model = TaggedItem
  171. fields = ('id', 'content_type', 'content_object')
  172. class ConfigContextTable(NetBoxTable):
  173. data_source = tables.Column(
  174. linkify=True
  175. )
  176. data_file = tables.Column(
  177. linkify=True
  178. )
  179. name = tables.Column(
  180. linkify=True
  181. )
  182. is_active = columns.BooleanColumn(
  183. verbose_name='Active'
  184. )
  185. is_synced = columns.BooleanColumn(
  186. verbose_name='Synced'
  187. )
  188. class Meta(NetBoxTable.Meta):
  189. model = ConfigContext
  190. fields = (
  191. 'pk', 'id', 'name', 'weight', 'is_active', 'is_synced', 'description', 'regions', 'sites', 'locations',
  192. 'roles', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', 'tenants',
  193. 'data_source', 'data_file', 'data_synced', 'created', 'last_updated',
  194. )
  195. default_columns = ('pk', 'name', 'weight', 'is_active', 'is_synced', 'description')
  196. class ConfigTemplateTable(NetBoxTable):
  197. name = tables.Column(
  198. linkify=True
  199. )
  200. data_source = tables.Column(
  201. linkify=True
  202. )
  203. data_file = tables.Column(
  204. linkify=True
  205. )
  206. is_synced = columns.BooleanColumn(
  207. verbose_name='Synced'
  208. )
  209. tags = columns.TagColumn(
  210. url_name='extras:configtemplate_list'
  211. )
  212. class Meta(NetBoxTable.Meta):
  213. model = ConfigTemplate
  214. fields = (
  215. 'pk', 'id', 'name', 'description', 'data_source', 'data_file', 'data_synced', 'created', 'last_updated',
  216. 'tags',
  217. )
  218. default_columns = (
  219. 'pk', 'name', 'description', 'is_synced',
  220. )
  221. class ObjectChangeTable(NetBoxTable):
  222. time = tables.DateTimeColumn(
  223. linkify=True,
  224. format=settings.SHORT_DATETIME_FORMAT
  225. )
  226. user_name = tables.Column(
  227. verbose_name='Username'
  228. )
  229. full_name = tables.TemplateColumn(
  230. accessor=tables.A('user'),
  231. template_code=OBJECTCHANGE_FULL_NAME,
  232. verbose_name='Full Name',
  233. orderable=False
  234. )
  235. action = columns.ChoiceFieldColumn()
  236. changed_object_type = columns.ContentTypeColumn(
  237. verbose_name='Type'
  238. )
  239. object_repr = tables.TemplateColumn(
  240. accessor=tables.A('changed_object'),
  241. template_code=OBJECTCHANGE_OBJECT,
  242. verbose_name='Object',
  243. orderable=False
  244. )
  245. request_id = tables.TemplateColumn(
  246. template_code=OBJECTCHANGE_REQUEST_ID,
  247. verbose_name='Request ID'
  248. )
  249. actions = columns.ActionsColumn(
  250. actions=()
  251. )
  252. class Meta(NetBoxTable.Meta):
  253. model = ObjectChange
  254. fields = (
  255. 'pk', 'id', 'time', 'user_name', 'full_name', 'action', 'changed_object_type', 'object_repr', 'request_id',
  256. 'actions',
  257. )
  258. class JournalEntryTable(NetBoxTable):
  259. created = tables.DateTimeColumn(
  260. linkify=True,
  261. format=settings.SHORT_DATETIME_FORMAT
  262. )
  263. assigned_object_type = columns.ContentTypeColumn(
  264. verbose_name='Object type'
  265. )
  266. assigned_object = tables.Column(
  267. linkify=True,
  268. orderable=False,
  269. verbose_name='Object'
  270. )
  271. kind = columns.ChoiceFieldColumn()
  272. comments = columns.MarkdownColumn()
  273. comments_short = tables.TemplateColumn(
  274. accessor=tables.A('comments'),
  275. template_code='{{ value|markdown|truncatewords_html:50 }}',
  276. verbose_name='Comments (Short)'
  277. )
  278. tags = columns.TagColumn(
  279. url_name='extras:journalentry_list'
  280. )
  281. class Meta(NetBoxTable.Meta):
  282. model = JournalEntry
  283. fields = (
  284. 'pk', 'id', 'created', 'created_by', 'assigned_object_type', 'assigned_object', 'kind', 'comments',
  285. 'comments_short', 'tags', 'actions',
  286. )
  287. default_columns = (
  288. 'pk', 'created', 'created_by', 'assigned_object_type', 'assigned_object', 'kind', 'comments'
  289. )