admin.py 6.2 KB

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