admin.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. list_display = [
  67. 'name', 'models', 'type', 'required', 'filter_logic', 'default', 'weight', 'description',
  68. ]
  69. list_filter = [
  70. 'type', 'required', 'obj_type',
  71. ]
  72. form = CustomFieldForm
  73. def models(self, obj):
  74. return ', '.join([ct.name for ct in obj.obj_type.all()])
  75. #
  76. # Custom links
  77. #
  78. class CustomLinkForm(forms.ModelForm):
  79. class Meta:
  80. model = CustomLink
  81. exclude = []
  82. widgets = {
  83. 'text': forms.Textarea,
  84. 'url': forms.Textarea,
  85. }
  86. help_texts = {
  87. 'weight': 'A numeric weight to influence the ordering of this link among its peers. Lower weights appear '
  88. 'first in a list.',
  89. 'text': 'Jinja2 template code for the link text. Reference the object as <code>{{ obj }}</code>. Links '
  90. 'which render as empty text will not be displayed.',
  91. 'url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ obj }}</code>.',
  92. }
  93. def __init__(self, *args, **kwargs):
  94. super().__init__(*args, **kwargs)
  95. # Format ContentType choices
  96. order_content_types(self.fields['content_type'])
  97. self.fields['content_type'].choices.insert(0, ('', '---------'))
  98. @admin.register(CustomLink)
  99. class CustomLinkAdmin(admin.ModelAdmin):
  100. fieldsets = (
  101. ('Custom Link', {
  102. 'fields': ('content_type', 'name', 'group_name', 'weight', 'button_class', 'new_window')
  103. }),
  104. ('Templates', {
  105. 'fields': ('text', 'url'),
  106. 'classes': ('monospace',)
  107. })
  108. )
  109. list_display = [
  110. 'name', 'content_type', 'group_name', 'weight',
  111. ]
  112. list_filter = [
  113. 'content_type',
  114. ]
  115. form = CustomLinkForm
  116. #
  117. # Export templates
  118. #
  119. class ExportTemplateForm(forms.ModelForm):
  120. class Meta:
  121. model = ExportTemplate
  122. exclude = []
  123. def __init__(self, *args, **kwargs):
  124. super().__init__(*args, **kwargs)
  125. # Format ContentType choices
  126. order_content_types(self.fields['content_type'])
  127. self.fields['content_type'].choices.insert(0, ('', '---------'))
  128. @admin.register(ExportTemplate)
  129. class ExportTemplateAdmin(admin.ModelAdmin):
  130. fieldsets = (
  131. ('Export Template', {
  132. 'fields': ('content_type', 'name', 'description', 'mime_type', 'file_extension')
  133. }),
  134. ('Content', {
  135. 'fields': ('template_code',),
  136. 'classes': ('monospace',)
  137. })
  138. )
  139. list_display = [
  140. 'name', 'content_type', 'description', 'mime_type', 'file_extension',
  141. ]
  142. list_filter = [
  143. 'content_type',
  144. ]
  145. form = ExportTemplateForm
  146. #
  147. # Reports
  148. #
  149. @admin.register(JobResult)
  150. class JobResultAdmin(admin.ModelAdmin):
  151. list_display = [
  152. 'obj_type', 'name', 'created', 'completed', 'user', 'status',
  153. ]
  154. fields = [
  155. 'obj_type', 'name', 'created', 'completed', 'user', 'status', 'data', 'job_id'
  156. ]
  157. list_filter = [
  158. 'status',
  159. ]
  160. readonly_fields = fields
  161. def has_add_permission(self, request):
  162. return False