admin.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. from django import forms
  2. from django.contrib import admin
  3. from utilities.forms import LaxURLField
  4. from .models import CustomField, CustomLink, ExportTemplate, JobResult, Webhook
  5. def order_content_types(field):
  6. """
  7. Order the list of available ContentTypes by application
  8. """
  9. queryset = field.queryset.order_by('app_label', 'model')
  10. field.choices = [(ct.pk, '{} > {}'.format(ct.app_label, ct.name)) for ct in queryset]
  11. #
  12. # Webhooks
  13. #
  14. class WebhookForm(forms.ModelForm):
  15. payload_url = LaxURLField(
  16. label='URL'
  17. )
  18. class Meta:
  19. model = Webhook
  20. exclude = ()
  21. def __init__(self, *args, **kwargs):
  22. super().__init__(*args, **kwargs)
  23. if 'obj_type' in self.fields:
  24. order_content_types(self.fields['obj_type'])
  25. @admin.register(Webhook)
  26. class WebhookAdmin(admin.ModelAdmin):
  27. list_display = [
  28. 'name', 'models', 'payload_url', 'http_content_type', 'enabled', 'type_create', 'type_update', 'type_delete',
  29. 'ssl_verification',
  30. ]
  31. list_filter = [
  32. 'enabled', 'type_create', 'type_update', 'type_delete', 'obj_type',
  33. ]
  34. form = WebhookForm
  35. fieldsets = (
  36. (None, {
  37. 'fields': ('name', 'obj_type', 'enabled')
  38. }),
  39. ('Events', {
  40. 'fields': ('type_create', 'type_update', 'type_delete')
  41. }),
  42. ('HTTP Request', {
  43. 'fields': (
  44. 'payload_url', 'http_method', 'http_content_type', 'additional_headers', 'body_template', 'secret',
  45. ),
  46. 'classes': ('monospace',)
  47. }),
  48. ('SSL', {
  49. 'fields': ('ssl_verification', 'ca_file_path')
  50. })
  51. )
  52. def models(self, obj):
  53. return ', '.join([ct.name for ct in obj.obj_type.all()])
  54. #
  55. # Custom fields
  56. #
  57. class CustomFieldForm(forms.ModelForm):
  58. class Meta:
  59. model = CustomField
  60. exclude = []
  61. def __init__(self, *args, **kwargs):
  62. super().__init__(*args, **kwargs)
  63. order_content_types(self.fields['obj_type'])
  64. @admin.register(CustomField)
  65. class CustomFieldAdmin(admin.ModelAdmin):
  66. actions = None
  67. form = CustomFieldForm
  68. list_display = [
  69. 'name', 'models', 'type', 'required', 'filter_logic', 'default', 'weight', 'description',
  70. ]
  71. list_filter = [
  72. 'type', 'required', 'obj_type',
  73. ]
  74. def models(self, obj):
  75. return ', '.join([ct.name for ct in obj.obj_type.all()])
  76. #
  77. # Custom links
  78. #
  79. class CustomLinkForm(forms.ModelForm):
  80. class Meta:
  81. model = CustomLink
  82. exclude = []
  83. widgets = {
  84. 'text': forms.Textarea,
  85. 'url': forms.Textarea,
  86. }
  87. help_texts = {
  88. 'weight': 'A numeric weight to influence the ordering of this link among its peers. Lower weights appear '
  89. 'first in a list.',
  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)
  100. class CustomLinkAdmin(admin.ModelAdmin):
  101. fieldsets = (
  102. ('Custom Link', {
  103. 'fields': ('content_type', 'name', 'group_name', 'weight', 'button_class', 'new_window')
  104. }),
  105. ('Templates', {
  106. 'fields': ('text', 'url'),
  107. 'classes': ('monospace',)
  108. })
  109. )
  110. list_display = [
  111. 'name', 'content_type', 'group_name', 'weight',
  112. ]
  113. list_filter = [
  114. 'content_type',
  115. ]
  116. form = CustomLinkForm
  117. #
  118. # Export templates
  119. #
  120. class ExportTemplateForm(forms.ModelForm):
  121. class Meta:
  122. model = ExportTemplate
  123. exclude = []
  124. def __init__(self, *args, **kwargs):
  125. super().__init__(*args, **kwargs)
  126. # Format ContentType choices
  127. order_content_types(self.fields['content_type'])
  128. self.fields['content_type'].choices.insert(0, ('', '---------'))
  129. @admin.register(ExportTemplate)
  130. class ExportTemplateAdmin(admin.ModelAdmin):
  131. fieldsets = (
  132. ('Export Template', {
  133. 'fields': ('content_type', 'name', 'description', 'mime_type', 'file_extension')
  134. }),
  135. ('Content', {
  136. 'fields': ('template_code',),
  137. 'classes': ('monospace',)
  138. })
  139. )
  140. list_display = [
  141. 'name', 'content_type', 'description', 'mime_type', 'file_extension',
  142. ]
  143. list_filter = [
  144. 'content_type',
  145. ]
  146. form = ExportTemplateForm
  147. #
  148. # Reports
  149. #
  150. @admin.register(JobResult)
  151. class JobResultAdmin(admin.ModelAdmin):
  152. list_display = [
  153. 'obj_type', 'name', 'created', 'completed', 'user', 'status',
  154. ]
  155. fields = [
  156. 'obj_type', 'name', 'created', 'completed', 'user', 'status', 'data', 'job_id'
  157. ]
  158. list_filter = [
  159. 'status',
  160. ]
  161. readonly_fields = fields
  162. def has_add_permission(self, request):
  163. return False