admin.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 'content_types' in self.fields:
  24. order_content_types(self.fields['content_types'])
  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', 'content_types',
  33. ]
  34. form = WebhookForm
  35. fieldsets = (
  36. (None, {
  37. 'fields': ('name', 'content_types', '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.content_types.all()])
  54. #
  55. # Custom fields
  56. #
  57. class CustomFieldForm(forms.ModelForm):
  58. class Meta:
  59. model = CustomField
  60. exclude = []
  61. widgets = {
  62. 'default': forms.TextInput(),
  63. 'validation_regex': forms.Textarea(
  64. attrs={
  65. 'cols': 80,
  66. 'rows': 3,
  67. }
  68. )
  69. }
  70. def __init__(self, *args, **kwargs):
  71. super().__init__(*args, **kwargs)
  72. order_content_types(self.fields['content_types'])
  73. @admin.register(CustomField)
  74. class CustomFieldAdmin(admin.ModelAdmin):
  75. actions = None
  76. form = CustomFieldForm
  77. list_display = [
  78. 'name', 'models', 'type', 'required', 'filter_logic', 'default', 'weight', 'description',
  79. ]
  80. list_filter = [
  81. 'type', 'required', 'content_types',
  82. ]
  83. fieldsets = (
  84. ('Custom Field', {
  85. 'fields': ('type', 'name', 'weight', 'label', 'description', 'required', 'default', 'filter_logic')
  86. }),
  87. ('Assignment', {
  88. 'description': 'A custom field must be assigned to one or more object types.',
  89. 'fields': ('content_types',)
  90. }),
  91. ('Validation Rules', {
  92. 'fields': ('validation_minimum', 'validation_maximum', 'validation_regex'),
  93. 'classes': ('monospace',)
  94. }),
  95. ('Choices', {
  96. 'description': 'A selection field must have two or more choices assigned to it.',
  97. 'fields': ('choices',)
  98. })
  99. )
  100. def models(self, obj):
  101. return ', '.join([ct.name for ct in obj.content_types.all()])
  102. #
  103. # Custom links
  104. #
  105. class CustomLinkForm(forms.ModelForm):
  106. class Meta:
  107. model = CustomLink
  108. exclude = []
  109. widgets = {
  110. 'link_text': forms.Textarea,
  111. 'link_url': forms.Textarea,
  112. }
  113. help_texts = {
  114. 'weight': 'A numeric weight to influence the ordering of this link among its peers. Lower weights appear '
  115. 'first in a list.',
  116. 'link_text': 'Jinja2 template code for the link text. Reference the object as <code>{{ obj }}</code>. '
  117. 'Links which render as empty text will not be displayed.',
  118. 'link_url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ obj }}</code>.',
  119. }
  120. def __init__(self, *args, **kwargs):
  121. super().__init__(*args, **kwargs)
  122. # Format ContentType choices
  123. order_content_types(self.fields['content_type'])
  124. self.fields['content_type'].choices.insert(0, ('', '---------'))
  125. @admin.register(CustomLink)
  126. class CustomLinkAdmin(admin.ModelAdmin):
  127. fieldsets = (
  128. ('Custom Link', {
  129. 'fields': ('content_type', 'name', 'group_name', 'weight', 'button_class', 'new_window')
  130. }),
  131. ('Templates', {
  132. 'fields': ('link_text', 'link_url'),
  133. 'classes': ('monospace',)
  134. })
  135. )
  136. list_display = [
  137. 'name', 'content_type', 'group_name', 'weight',
  138. ]
  139. list_filter = [
  140. 'content_type',
  141. ]
  142. form = CustomLinkForm
  143. #
  144. # Export templates
  145. #
  146. class ExportTemplateForm(forms.ModelForm):
  147. class Meta:
  148. model = ExportTemplate
  149. exclude = []
  150. def __init__(self, *args, **kwargs):
  151. super().__init__(*args, **kwargs)
  152. # Format ContentType choices
  153. order_content_types(self.fields['content_type'])
  154. self.fields['content_type'].choices.insert(0, ('', '---------'))
  155. @admin.register(ExportTemplate)
  156. class ExportTemplateAdmin(admin.ModelAdmin):
  157. fieldsets = (
  158. ('Export Template', {
  159. 'fields': ('content_type', 'name', 'description', 'mime_type', 'file_extension', 'as_attachment')
  160. }),
  161. ('Content', {
  162. 'fields': ('template_code',),
  163. 'classes': ('monospace',)
  164. })
  165. )
  166. list_display = [
  167. 'name', 'content_type', 'description', 'mime_type', 'file_extension', 'as_attachment',
  168. ]
  169. list_filter = [
  170. 'content_type',
  171. ]
  172. form = ExportTemplateForm
  173. #
  174. # Reports
  175. #
  176. @admin.register(JobResult)
  177. class JobResultAdmin(admin.ModelAdmin):
  178. list_display = [
  179. 'obj_type', 'name', 'created', 'completed', 'user', 'status',
  180. ]
  181. fields = [
  182. 'obj_type', 'name', 'created', 'completed', 'user', 'status', 'data', 'job_id'
  183. ]
  184. list_filter = [
  185. 'status',
  186. ]
  187. readonly_fields = fields
  188. def has_add_permission(self, request):
  189. return False