wagtail_hooks.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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
  7. from django.utils.translation import gettext_lazy as _
  8. from wagtail.admin.menu import MenuItem
  9. from wagtail import hooks
  10. from wagtail.models import UserPagePermissionsProxy, get_page_models
  11. from wagtailcache.cache import clear_cache
  12. from coderedcms import __version__
  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/crx-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/crx-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/crx-editor.js"),
  32. __version__,
  33. )
  34. @hooks.register("register_icons")
  35. def register_icons(icons):
  36. """
  37. Add custom SVG icons to the Wagtail admin.
  38. """
  39. # These SVG files should be in the django templates folder, and follow exact
  40. # specifications to work with Wagtail:
  41. # https://github.com/wagtail/wagtail/pull/6028
  42. icons.append("coderedcms/icons/cr-align-left.svg")
  43. icons.append("coderedcms/icons/cr-check-square-o.svg")
  44. icons.append("coderedcms/icons/cr-columns.svg")
  45. icons.append("coderedcms/icons/cr-desktop.svg")
  46. icons.append("coderedcms/icons/cr-font.svg")
  47. icons.append("coderedcms/icons/cr-google.svg")
  48. icons.append("coderedcms/icons/cr-hand-pointer-o.svg")
  49. icons.append("coderedcms/icons/cr-hashtag.svg")
  50. icons.append("coderedcms/icons/cr-header.svg")
  51. icons.append("coderedcms/icons/cr-list-alt.svg")
  52. icons.append("coderedcms/icons/cr-map.svg")
  53. icons.append("coderedcms/icons/cr-newspaper-o.svg")
  54. icons.append("coderedcms/icons/cr-puzzle-piece.svg")
  55. icons.append("coderedcms/icons/cr-recycle.svg")
  56. icons.append("coderedcms/icons/cr-stop.svg")
  57. icons.append("coderedcms/icons/cr-th-large.svg")
  58. icons.append("coderedcms/icons/cr-universal-access.svg")
  59. icons.append("coderedcms/icons/cr-usd.svg")
  60. icons.append("coderedcms/icons/cr-window-maximize.svg")
  61. icons.append("coderedcms/icons/cr-window-minimize.svg")
  62. return icons
  63. def clear_wagtailcache(*args, **kwargs):
  64. clear_cache()
  65. # Clear cache whenever pages/snippets are changed. Err on the side of clearing
  66. # the cache vs not clearing the cache, as this usually leads to support requests
  67. # when staff members make edits but do not see the changes.
  68. hooks.register("after_delete_page", clear_wagtailcache)
  69. hooks.register("after_move_page", clear_wagtailcache)
  70. hooks.register("after_publish_page", clear_wagtailcache)
  71. hooks.register("after_unpublish_page", clear_wagtailcache)
  72. hooks.register("after_create_snippet", clear_wagtailcache)
  73. hooks.register("after_edit_snippet", clear_wagtailcache)
  74. hooks.register("after_delete_snippet", clear_wagtailcache)
  75. @hooks.register("filter_form_submissions_for_user")
  76. def crx_forms(user, editable_forms):
  77. """
  78. Add our own CoderedFormPage to editable_forms, since wagtail is unaware
  79. of its existence. Essentially this is a fork of wagtail.contrib.forms.get_forms_for_user()
  80. and wagtail.contrib.forms.get_form_types()
  81. """
  82. from coderedcms.models import CoderedFormMixin
  83. form_models = [
  84. model
  85. for model in get_page_models()
  86. if issubclass(model, CoderedFormMixin)
  87. ]
  88. form_types = list(ContentType.objects.get_for_models(*form_models).values())
  89. editable_forms = UserPagePermissionsProxy(user).editable_pages()
  90. editable_forms = editable_forms.filter(content_type__in=form_types)
  91. return editable_forms
  92. @hooks.register("before_serve_document")
  93. def serve_document_directly(document, request):
  94. """
  95. This hook prevents documents from being downloaded unless
  96. specified by an <a> tag with the download attribute.
  97. """
  98. content_type, content_encoding = mimetypes.guess_type(document.filename)
  99. response = HttpResponse(document.file.read(), content_type=content_type)
  100. response["Content-Disposition"] = 'inline;filename="{0}"'.format(
  101. document.filename
  102. )
  103. response["Content-Encoding"] = content_encoding
  104. return response
  105. class ImportExportMenuItem(MenuItem):
  106. def is_shown(self, request):
  107. return request.user.is_superuser
  108. @hooks.register("register_settings_menu_item")
  109. def register_import_export_menu_item():
  110. return ImportExportMenuItem(
  111. _("Import"),
  112. reverse("import_index"),
  113. classnames="icon icon-download",
  114. )