admin.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. from django import forms
  2. from django.contrib import admin
  3. from netbox.admin import admin_site
  4. from utilities.forms import LaxURLField
  5. from .models import CustomField, CustomFieldChoice, CustomLink, Graph, ExportTemplate, ReportResult, Webhook
  6. from .reports import get_report
  7. def order_content_types(field):
  8. """
  9. Order the list of available ContentTypes by application
  10. """
  11. queryset = field.queryset.order_by('app_label', 'model')
  12. field.choices = [(ct.pk, '{} > {}'.format(ct.app_label, ct.name)) for ct in queryset]
  13. #
  14. # Webhooks
  15. #
  16. class WebhookForm(forms.ModelForm):
  17. payload_url = LaxURLField(
  18. label='URL'
  19. )
  20. class Meta:
  21. model = Webhook
  22. exclude = ()
  23. def __init__(self, *args, **kwargs):
  24. super().__init__(*args, **kwargs)
  25. if 'obj_type' in self.fields:
  26. order_content_types(self.fields['obj_type'])
  27. @admin.register(Webhook, site=admin_site)
  28. class WebhookAdmin(admin.ModelAdmin):
  29. list_display = [
  30. 'name', 'models', 'payload_url', 'http_content_type', 'enabled', 'type_create', 'type_update', 'type_delete',
  31. 'ssl_verification',
  32. ]
  33. list_filter = [
  34. 'enabled', 'type_create', 'type_update', 'type_delete', 'obj_type',
  35. ]
  36. form = WebhookForm
  37. fieldsets = (
  38. (None, {
  39. 'fields': ('name', 'obj_type', 'enabled')
  40. }),
  41. ('Events', {
  42. 'fields': ('type_create', 'type_update', 'type_delete')
  43. }),
  44. ('HTTP Request', {
  45. 'fields': ('payload_url', 'http_content_type', 'additional_headers', 'body_template', 'secret')
  46. }),
  47. ('SSL', {
  48. 'fields': ('ssl_verification', 'ca_file_path')
  49. })
  50. )
  51. def models(self, obj):
  52. return ', '.join([ct.name for ct in obj.obj_type.all()])
  53. #
  54. # Custom fields
  55. #
  56. class CustomFieldForm(forms.ModelForm):
  57. class Meta:
  58. model = CustomField
  59. exclude = []
  60. def __init__(self, *args, **kwargs):
  61. super().__init__(*args, **kwargs)
  62. order_content_types(self.fields['obj_type'])
  63. class CustomFieldChoiceAdmin(admin.TabularInline):
  64. model = CustomFieldChoice
  65. extra = 5
  66. @admin.register(CustomField, site=admin_site)
  67. class CustomFieldAdmin(admin.ModelAdmin):
  68. inlines = [CustomFieldChoiceAdmin]
  69. list_display = [
  70. 'name', 'models', 'type', 'required', 'filter_logic', 'default', 'weight', 'description',
  71. ]
  72. list_filter = [
  73. 'type', 'required', 'obj_type',
  74. ]
  75. form = CustomFieldForm
  76. def models(self, obj):
  77. return ', '.join([ct.name for ct in obj.obj_type.all()])
  78. #
  79. # Custom links
  80. #
  81. class CustomLinkForm(forms.ModelForm):
  82. class Meta:
  83. model = CustomLink
  84. exclude = []
  85. widgets = {
  86. 'text': forms.Textarea,
  87. 'url': forms.Textarea,
  88. }
  89. help_texts = {
  90. 'text': 'Jinja2 template code for the link text. Reference the object as <code>{{ obj }}</code>. Links '
  91. 'which render as empty text will not be displayed.',
  92. 'url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ obj }}</code>.',
  93. }
  94. def __init__(self, *args, **kwargs):
  95. super().__init__(*args, **kwargs)
  96. # Format ContentType choices
  97. order_content_types(self.fields['content_type'])
  98. self.fields['content_type'].choices.insert(0, ('', '---------'))
  99. @admin.register(CustomLink, site=admin_site)
  100. class CustomLinkAdmin(admin.ModelAdmin):
  101. list_display = [
  102. 'name', 'content_type', 'group_name', 'weight',
  103. ]
  104. list_filter = [
  105. 'content_type',
  106. ]
  107. form = CustomLinkForm
  108. #
  109. # Graphs
  110. #
  111. @admin.register(Graph, site=admin_site)
  112. class GraphAdmin(admin.ModelAdmin):
  113. list_display = [
  114. 'name', 'type', 'weight', 'template_language', 'source',
  115. ]
  116. list_filter = [
  117. 'type', 'template_language',
  118. ]
  119. #
  120. # Export templates
  121. #
  122. class ExportTemplateForm(forms.ModelForm):
  123. class Meta:
  124. model = ExportTemplate
  125. exclude = []
  126. def __init__(self, *args, **kwargs):
  127. super().__init__(*args, **kwargs)
  128. # Format ContentType choices
  129. order_content_types(self.fields['content_type'])
  130. self.fields['content_type'].choices.insert(0, ('', '---------'))
  131. @admin.register(ExportTemplate, site=admin_site)
  132. class ExportTemplateAdmin(admin.ModelAdmin):
  133. list_display = [
  134. 'name', 'content_type', 'description', 'mime_type', 'file_extension',
  135. ]
  136. list_filter = [
  137. 'content_type',
  138. ]
  139. form = ExportTemplateForm
  140. #
  141. # Reports
  142. #
  143. @admin.register(ReportResult, site=admin_site)
  144. class ReportResultAdmin(admin.ModelAdmin):
  145. list_display = [
  146. 'report', 'active', 'created', 'user', 'passing',
  147. ]
  148. fields = [
  149. 'report', 'user', 'passing', 'data',
  150. ]
  151. list_filter = [
  152. 'failed',
  153. ]
  154. readonly_fields = fields
  155. def has_add_permission(self, request):
  156. return False
  157. def active(self, obj):
  158. module, report_name = obj.report.split('.')
  159. return True if get_report(module, report_name) else False
  160. active.boolean = True
  161. def passing(self, obj):
  162. return not obj.failed
  163. passing.boolean = True