wagtail_hooks.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import mimetypes
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.templatetags.static import static
  4. from django.http.response import HttpResponse
  5. from django.urls import reverse
  6. from django.utils.html import format_html, mark_safe
  7. from django.utils.translation import gettext_lazy as _
  8. from wagtail.core import hooks
  9. from wagtail.core.models import UserPagePermissionsProxy, get_page_models
  10. from wagtailcache.cache import clear_cache
  11. from coderedcms import __version__
  12. from coderedcms.wagtail_flexible_forms.wagtail_hooks import FormAdmin, SubmissionAdmin
  13. @hooks.register('insert_global_admin_css')
  14. def global_admin_css():
  15. return format_html(
  16. '<link rel="stylesheet" type="text/css" href="{}?v={}">',
  17. static('coderedcms/css/codered-admin.css'),
  18. __version__,
  19. )
  20. @hooks.register('insert_editor_css')
  21. def editor_css():
  22. return format_html(
  23. '<link rel="stylesheet" type="text/css" href="{}?v={}">',
  24. static('coderedcms/css/codered-editor.css'),
  25. __version__,
  26. )
  27. @hooks.register('insert_editor_js')
  28. def collapsible_js():
  29. return format_html(
  30. '<script src="{}?v={}"></script>',
  31. static('coderedcms/js/codered-editor.js'),
  32. __version__,
  33. )
  34. def clear_wagtailcache(*args, **kwargs):
  35. clear_cache()
  36. # Clear cache whenever pages/snippets are changed. Err on the side of clearing
  37. # the cache vs not clearing the cache, as this usually leads to support requests
  38. # when staff members make edits but do not see the changes.
  39. hooks.register('after_delete_page', clear_wagtailcache)
  40. hooks.register('after_move_page', clear_wagtailcache)
  41. hooks.register('after_publish_page', clear_wagtailcache)
  42. hooks.register('after_unpublish_page', clear_wagtailcache)
  43. hooks.register('after_create_snippet', clear_wagtailcache)
  44. hooks.register('after_edit_snippet', clear_wagtailcache)
  45. hooks.register('after_delete_snippet', clear_wagtailcache)
  46. @hooks.register('filter_form_submissions_for_user')
  47. def codered_forms(user, editable_forms):
  48. from coderedcms.models import CoderedFormMixin
  49. """
  50. Add our own CoderedFormPage to editable_forms, since wagtail is unaware
  51. of its existence. Essentially this is a fork of wagtail.contrib.forms.get_forms_for_user()
  52. and wagtail.contrib.forms.get_form_types()
  53. """
  54. form_models = [
  55. model for model in get_page_models()
  56. if issubclass(model, CoderedFormMixin)
  57. ]
  58. form_types = list(
  59. ContentType.objects.get_for_models(*form_models).values()
  60. )
  61. editable_forms = UserPagePermissionsProxy(user).editable_pages()
  62. editable_forms = editable_forms.filter(content_type__in=form_types)
  63. return editable_forms
  64. @hooks.register('before_serve_document')
  65. def serve_document_directly(document, request):
  66. """
  67. This hook prevents documents from being downloaded unless
  68. specified by an <a> tag with the download attribute.
  69. """
  70. content_type, content_encoding = mimetypes.guess_type(document.filename)
  71. response = HttpResponse(document.file.read(), content_type=content_type)
  72. response['Content-Disposition'] = 'inline;filename="{0}"'.format(document.filename)
  73. response['Content-Encoding'] = content_encoding
  74. return response
  75. class CoderedSubmissionAdmin(SubmissionAdmin):
  76. def __init__(self, parent=None):
  77. from coderedcms.models import CoderedSessionFormSubmission
  78. self.model = CoderedSessionFormSubmission
  79. super().__init__(parent=parent)
  80. class CoderedFormAdmin(FormAdmin):
  81. list_display = ('title', 'action_links')
  82. def all_submissions_link(self, obj, label=_('See all submissions'),
  83. url_suffix=''):
  84. return '<a href="%s?page_id=%s%s">%s</a>' % (
  85. reverse(CoderedSubmissionAdmin().url_helper.get_action_url_name('index')),
  86. obj.pk, url_suffix, label)
  87. all_submissions_link.short_description = ''
  88. all_submissions_link.allow_tags = True
  89. def action_links(self, obj):
  90. from coderedcms.models import CoderedFormPage, CoderedStreamFormPage
  91. actions = []
  92. if issubclass(type(obj.specific), CoderedFormPage):
  93. actions.append(
  94. '<a href="{0}">{1}</a>'.format(reverse(
  95. 'wagtailforms:list_submissions',
  96. args=(obj.pk,)),
  97. _('See all Submissions')
  98. )
  99. )
  100. actions.append(
  101. '<a href="{0}">{1}</a>'.format(
  102. reverse("wagtailadmin_pages:edit", args=(obj.pk,)), _("Edit this form page")
  103. )
  104. )
  105. elif issubclass(type(obj.specific), CoderedStreamFormPage):
  106. actions.append(self.unprocessed_submissions_link(obj))
  107. actions.append(self.all_submissions_link(obj))
  108. actions.append(self.edit_link(obj))
  109. return mark_safe("<br />".join(actions))
  110. # modeladmin_register(CoderedFormAdmin)
  111. # modeladmin_register(CoderedSubmissionAdmin)