admin.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from django import forms
  2. from django.contrib import admin
  3. from django.contrib.contenttypes.models import ContentType
  4. from utilities.forms import ContentTypeChoiceField, ContentTypeMultipleChoiceField, LaxURLField
  5. from .models import CustomLink, ExportTemplate, JobResult, Webhook
  6. from .utils import FeatureQuery
  7. #
  8. # Webhooks
  9. #
  10. class WebhookForm(forms.ModelForm):
  11. content_types = ContentTypeMultipleChoiceField(
  12. queryset=ContentType.objects.all(),
  13. limit_choices_to=FeatureQuery('webhooks')
  14. )
  15. payload_url = LaxURLField(
  16. label='URL'
  17. )
  18. class Meta:
  19. model = Webhook
  20. exclude = ()
  21. @admin.register(Webhook)
  22. class WebhookAdmin(admin.ModelAdmin):
  23. list_display = [
  24. 'name', 'models', 'payload_url', 'http_content_type', 'enabled', 'type_create', 'type_update', 'type_delete',
  25. 'ssl_verification',
  26. ]
  27. list_filter = [
  28. 'enabled', 'type_create', 'type_update', 'type_delete', 'content_types',
  29. ]
  30. form = WebhookForm
  31. fieldsets = (
  32. (None, {
  33. 'fields': ('name', 'content_types', 'enabled')
  34. }),
  35. ('Events', {
  36. 'fields': ('type_create', 'type_update', 'type_delete')
  37. }),
  38. ('HTTP Request', {
  39. 'fields': (
  40. 'payload_url', 'http_method', 'http_content_type', 'additional_headers', 'body_template', 'secret',
  41. ),
  42. 'classes': ('monospace',)
  43. }),
  44. ('SSL', {
  45. 'fields': ('ssl_verification', 'ca_file_path')
  46. })
  47. )
  48. def models(self, obj):
  49. return ', '.join([ct.name for ct in obj.content_types.all()])
  50. #
  51. # Custom links
  52. #
  53. class CustomLinkForm(forms.ModelForm):
  54. content_type = ContentTypeChoiceField(
  55. queryset=ContentType.objects.all(),
  56. limit_choices_to=FeatureQuery('custom_links')
  57. )
  58. class Meta:
  59. model = CustomLink
  60. exclude = []
  61. widgets = {
  62. 'link_text': forms.Textarea,
  63. 'link_url': forms.Textarea,
  64. }
  65. help_texts = {
  66. 'weight': 'A numeric weight to influence the ordering of this link among its peers. Lower weights appear '
  67. 'first in a list.',
  68. 'link_text': 'Jinja2 template code for the link text. Reference the object as <code>{{ obj }}</code>. '
  69. 'Links which render as empty text will not be displayed.',
  70. 'link_url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ obj }}</code>.',
  71. }
  72. @admin.register(CustomLink)
  73. class CustomLinkAdmin(admin.ModelAdmin):
  74. fieldsets = (
  75. ('Custom Link', {
  76. 'fields': ('content_type', 'name', 'group_name', 'weight', 'button_class', 'new_window')
  77. }),
  78. ('Templates', {
  79. 'fields': ('link_text', 'link_url'),
  80. 'classes': ('monospace',)
  81. })
  82. )
  83. list_display = [
  84. 'name', 'content_type', 'group_name', 'weight',
  85. ]
  86. list_filter = [
  87. 'content_type',
  88. ]
  89. form = CustomLinkForm
  90. #
  91. # Export templates
  92. #
  93. class ExportTemplateForm(forms.ModelForm):
  94. content_type = ContentTypeChoiceField(
  95. queryset=ContentType.objects.all(),
  96. limit_choices_to=FeatureQuery('custom_links')
  97. )
  98. class Meta:
  99. model = ExportTemplate
  100. exclude = []
  101. @admin.register(ExportTemplate)
  102. class ExportTemplateAdmin(admin.ModelAdmin):
  103. fieldsets = (
  104. ('Export Template', {
  105. 'fields': ('content_type', 'name', 'description', 'mime_type', 'file_extension', 'as_attachment')
  106. }),
  107. ('Content', {
  108. 'fields': ('template_code',),
  109. 'classes': ('monospace',)
  110. })
  111. )
  112. list_display = [
  113. 'name', 'content_type', 'description', 'mime_type', 'file_extension', 'as_attachment',
  114. ]
  115. list_filter = [
  116. 'content_type',
  117. ]
  118. form = ExportTemplateForm
  119. #
  120. # Reports
  121. #
  122. @admin.register(JobResult)
  123. class JobResultAdmin(admin.ModelAdmin):
  124. list_display = [
  125. 'obj_type', 'name', 'created', 'completed', 'user', 'status',
  126. ]
  127. fields = [
  128. 'obj_type', 'name', 'created', 'completed', 'user', 'status', 'data', 'job_id'
  129. ]
  130. list_filter = [
  131. 'status',
  132. ]
  133. readonly_fields = fields
  134. def has_add_permission(self, request):
  135. return False