admin.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. from django import forms
  2. from django.contrib import admin
  3. from utilities.forms import LaxURLField
  4. from .choices import CustomFieldTypeChoices
  5. from .models import CustomField, CustomLink, ExportTemplate, JobResult, Webhook
  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. widgets = {
  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. def clean(self):
  74. # Validate selection choices
  75. if self.cleaned_data['type'] == CustomFieldTypeChoices.TYPE_SELECT and len(self.cleaned_data['choices']) < 2:
  76. raise forms.ValidationError({
  77. 'choices': 'Selection fields must specify at least two choices.'
  78. })
  79. @admin.register(CustomField)
  80. class CustomFieldAdmin(admin.ModelAdmin):
  81. actions = None
  82. form = CustomFieldForm
  83. list_display = [
  84. 'name', 'models', 'type', 'required', 'filter_logic', 'default', 'weight', 'description',
  85. ]
  86. list_filter = [
  87. 'type', 'required', 'content_types',
  88. ]
  89. fieldsets = (
  90. ('Custom Field', {
  91. 'fields': ('type', 'name', 'weight', 'label', 'description', 'required', 'default', 'filter_logic')
  92. }),
  93. ('Assignment', {
  94. 'description': 'A custom field must be assigned to one or more object types.',
  95. 'fields': ('content_types',)
  96. }),
  97. ('Validation Rules', {
  98. 'fields': ('validation_minimum', 'validation_maximum', 'validation_regex'),
  99. 'classes': ('monospace',)
  100. }),
  101. ('Choices', {
  102. 'description': 'A selection field must have two or more choices assigned to it.',
  103. 'fields': ('choices',)
  104. })
  105. )
  106. def models(self, obj):
  107. return ', '.join([ct.name for ct in obj.content_types.all()])
  108. #
  109. # Custom links
  110. #
  111. class CustomLinkForm(forms.ModelForm):
  112. class Meta:
  113. model = CustomLink
  114. exclude = []
  115. widgets = {
  116. 'text': forms.Textarea,
  117. 'url': forms.Textarea,
  118. }
  119. help_texts = {
  120. 'weight': 'A numeric weight to influence the ordering of this link among its peers. Lower weights appear '
  121. 'first in a list.',
  122. 'text': 'Jinja2 template code for the link text. Reference the object as <code>{{ obj }}</code>. Links '
  123. 'which render as empty text will not be displayed.',
  124. 'url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ obj }}</code>.',
  125. }
  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(CustomLink)
  132. class CustomLinkAdmin(admin.ModelAdmin):
  133. fieldsets = (
  134. ('Custom Link', {
  135. 'fields': ('content_type', 'name', 'group_name', 'weight', 'button_class', 'new_window')
  136. }),
  137. ('Templates', {
  138. 'fields': ('text', 'url'),
  139. 'classes': ('monospace',)
  140. })
  141. )
  142. list_display = [
  143. 'name', 'content_type', 'group_name', 'weight',
  144. ]
  145. list_filter = [
  146. 'content_type',
  147. ]
  148. form = CustomLinkForm
  149. #
  150. # Export templates
  151. #
  152. class ExportTemplateForm(forms.ModelForm):
  153. class Meta:
  154. model = ExportTemplate
  155. exclude = []
  156. def __init__(self, *args, **kwargs):
  157. super().__init__(*args, **kwargs)
  158. # Format ContentType choices
  159. order_content_types(self.fields['content_type'])
  160. self.fields['content_type'].choices.insert(0, ('', '---------'))
  161. @admin.register(ExportTemplate)
  162. class ExportTemplateAdmin(admin.ModelAdmin):
  163. fieldsets = (
  164. ('Export Template', {
  165. 'fields': ('content_type', 'name', 'description', 'mime_type', 'file_extension')
  166. }),
  167. ('Content', {
  168. 'fields': ('template_code',),
  169. 'classes': ('monospace',)
  170. })
  171. )
  172. list_display = [
  173. 'name', 'content_type', 'description', 'mime_type', 'file_extension',
  174. ]
  175. list_filter = [
  176. 'content_type',
  177. ]
  178. form = ExportTemplateForm
  179. #
  180. # Reports
  181. #
  182. @admin.register(JobResult)
  183. class JobResultAdmin(admin.ModelAdmin):
  184. list_display = [
  185. 'obj_type', 'name', 'created', 'completed', 'user', 'status',
  186. ]
  187. fields = [
  188. 'obj_type', 'name', 'created', 'completed', 'user', 'status', 'data', 'job_id'
  189. ]
  190. list_filter = [
  191. 'status',
  192. ]
  193. readonly_fields = fields
  194. def has_add_permission(self, request):
  195. return False