tables.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import django_tables2 as tables
  2. from django.conf import settings
  3. from extras.models import *
  4. from netbox.tables import NetBoxTable, columns
  5. from .template_code import *
  6. __all__ = (
  7. 'ConfigContextTable',
  8. 'CustomFieldTable',
  9. 'CustomLinkTable',
  10. 'ExportTemplateTable',
  11. 'JournalEntryTable',
  12. 'ObjectChangeTable',
  13. 'TaggedItemTable',
  14. 'TagTable',
  15. 'WebhookTable',
  16. )
  17. #
  18. # Custom fields
  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', 'weight', 'default',
  31. 'description', 'filter_logic', 'ui_visibility', 'choices', 'created', 'last_updated',
  32. )
  33. default_columns = ('pk', 'name', 'content_types', 'label', 'group_name', 'type', 'required', 'description')
  34. #
  35. # Custom links
  36. #
  37. class CustomLinkTable(NetBoxTable):
  38. name = tables.Column(
  39. linkify=True
  40. )
  41. content_type = columns.ContentTypeColumn()
  42. enabled = columns.BooleanColumn()
  43. new_window = columns.BooleanColumn()
  44. class Meta(NetBoxTable.Meta):
  45. model = CustomLink
  46. fields = (
  47. 'pk', 'id', 'name', 'content_type', 'enabled', 'link_text', 'link_url', 'weight', 'group_name',
  48. 'button_class', 'new_window', 'created', 'last_updated',
  49. )
  50. default_columns = ('pk', 'name', 'content_type', 'enabled', 'group_name', 'button_class', 'new_window')
  51. #
  52. # Export templates
  53. #
  54. class ExportTemplateTable(NetBoxTable):
  55. name = tables.Column(
  56. linkify=True
  57. )
  58. content_type = columns.ContentTypeColumn()
  59. as_attachment = columns.BooleanColumn()
  60. class Meta(NetBoxTable.Meta):
  61. model = ExportTemplate
  62. fields = (
  63. 'pk', 'id', 'name', 'content_type', 'description', 'mime_type', 'file_extension', 'as_attachment',
  64. 'created', 'last_updated',
  65. )
  66. default_columns = (
  67. 'pk', 'name', 'content_type', 'description', 'mime_type', 'file_extension', 'as_attachment',
  68. )
  69. #
  70. # Webhooks
  71. #
  72. class WebhookTable(NetBoxTable):
  73. name = tables.Column(
  74. linkify=True
  75. )
  76. content_types = columns.ContentTypesColumn()
  77. enabled = columns.BooleanColumn()
  78. type_create = columns.BooleanColumn(
  79. verbose_name='Create'
  80. )
  81. type_update = columns.BooleanColumn(
  82. verbose_name='Update'
  83. )
  84. type_delete = columns.BooleanColumn(
  85. verbose_name='Delete'
  86. )
  87. ssl_validation = columns.BooleanColumn(
  88. verbose_name='SSL Validation'
  89. )
  90. class Meta(NetBoxTable.Meta):
  91. model = Webhook
  92. fields = (
  93. 'pk', 'id', 'name', 'content_types', 'enabled', 'type_create', 'type_update', 'type_delete', 'http_method',
  94. 'payload_url', 'secret', 'ssl_validation', 'ca_file_path', 'created', 'last_updated',
  95. )
  96. default_columns = (
  97. 'pk', 'name', 'content_types', 'enabled', 'type_create', 'type_update', 'type_delete', 'http_method',
  98. 'payload_url',
  99. )
  100. #
  101. # Tags
  102. #
  103. class TagTable(NetBoxTable):
  104. name = tables.Column(
  105. linkify=True
  106. )
  107. color = columns.ColorColumn()
  108. class Meta(NetBoxTable.Meta):
  109. model = Tag
  110. fields = ('pk', 'id', 'name', 'items', 'slug', 'color', 'description', 'created', 'last_updated', 'actions')
  111. default_columns = ('pk', 'name', 'items', 'slug', 'color', 'description')
  112. class TaggedItemTable(NetBoxTable):
  113. id = tables.Column(
  114. verbose_name='ID',
  115. linkify=lambda record: record.content_object.get_absolute_url(),
  116. accessor='content_object__id'
  117. )
  118. content_type = columns.ContentTypeColumn(
  119. verbose_name='Type'
  120. )
  121. content_object = tables.Column(
  122. linkify=True,
  123. orderable=False,
  124. verbose_name='Object'
  125. )
  126. actions = columns.ActionsColumn(
  127. actions=()
  128. )
  129. class Meta(NetBoxTable.Meta):
  130. model = TaggedItem
  131. fields = ('id', 'content_type', 'content_object')
  132. class ConfigContextTable(NetBoxTable):
  133. name = tables.Column(
  134. linkify=True
  135. )
  136. is_active = columns.BooleanColumn(
  137. verbose_name='Active'
  138. )
  139. class Meta(NetBoxTable.Meta):
  140. model = ConfigContext
  141. fields = (
  142. 'pk', 'id', 'name', 'weight', 'is_active', 'description', 'regions', 'sites', 'locations', 'roles',
  143. 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', 'tenants', 'created',
  144. 'last_updated',
  145. )
  146. default_columns = ('pk', 'name', 'weight', 'is_active', 'description')
  147. class ObjectChangeTable(NetBoxTable):
  148. time = tables.DateTimeColumn(
  149. linkify=True,
  150. format=settings.SHORT_DATETIME_FORMAT
  151. )
  152. user_name = tables.Column(
  153. verbose_name='Username'
  154. )
  155. full_name = tables.TemplateColumn(
  156. template_code=OBJECTCHANGE_FULL_NAME,
  157. verbose_name='Full Name',
  158. orderable=False
  159. )
  160. action = columns.ChoiceFieldColumn()
  161. changed_object_type = columns.ContentTypeColumn(
  162. verbose_name='Type'
  163. )
  164. object_repr = tables.TemplateColumn(
  165. template_code=OBJECTCHANGE_OBJECT,
  166. verbose_name='Object'
  167. )
  168. request_id = tables.TemplateColumn(
  169. template_code=OBJECTCHANGE_REQUEST_ID,
  170. verbose_name='Request ID'
  171. )
  172. actions = columns.ActionsColumn(
  173. actions=()
  174. )
  175. class Meta(NetBoxTable.Meta):
  176. model = ObjectChange
  177. fields = (
  178. 'pk', 'id', 'time', 'user_name', 'full_name', 'action', 'changed_object_type', 'object_repr', 'request_id',
  179. 'actions',
  180. )
  181. class JournalEntryTable(NetBoxTable):
  182. created = tables.DateTimeColumn(
  183. linkify=True,
  184. format=settings.SHORT_DATETIME_FORMAT
  185. )
  186. assigned_object_type = columns.ContentTypeColumn(
  187. verbose_name='Object type'
  188. )
  189. assigned_object = tables.Column(
  190. linkify=True,
  191. orderable=False,
  192. verbose_name='Object'
  193. )
  194. kind = columns.ChoiceFieldColumn()
  195. comments = columns.MarkdownColumn()
  196. comments_short = tables.TemplateColumn(
  197. accessor=tables.A('comments'),
  198. template_code='{{ value|markdown|truncatewords_html:50 }}',
  199. verbose_name='Comments (Short)'
  200. )
  201. tags = columns.TagColumn(
  202. url_name='extras:journalentry_list'
  203. )
  204. class Meta(NetBoxTable.Meta):
  205. model = JournalEntry
  206. fields = (
  207. 'pk', 'id', 'created', 'created_by', 'assigned_object_type', 'assigned_object', 'kind', 'comments',
  208. 'comments_short', 'tags', 'actions',
  209. )
  210. default_columns = (
  211. 'pk', 'created', 'created_by', 'assigned_object_type', 'assigned_object', 'kind', 'comments'
  212. )