tables.py 8.9 KB

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