2
0

wagtail_hooks.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 wagtailcrx 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('wagtailcrx/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('wagtailcrx/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('wagtailcrx/js/codered-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("wagtailcrx/icons/cr-align-left.svg")
  43. icons.append("wagtailcrx/icons/cr-check-square-o.svg")
  44. icons.append("wagtailcrx/icons/cr-columns.svg")
  45. icons.append("wagtailcrx/icons/cr-desktop.svg")
  46. icons.append("wagtailcrx/icons/cr-font.svg")
  47. icons.append("wagtailcrx/icons/cr-google.svg")
  48. icons.append("wagtailcrx/icons/cr-hand-pointer-o.svg")
  49. icons.append("wagtailcrx/icons/cr-hashtag.svg")
  50. icons.append("wagtailcrx/icons/cr-header.svg")
  51. icons.append("wagtailcrx/icons/cr-list-alt.svg")
  52. icons.append("wagtailcrx/icons/cr-map.svg")
  53. icons.append("wagtailcrx/icons/cr-newspaper-o.svg")
  54. icons.append("wagtailcrx/icons/cr-puzzle-piece.svg")
  55. icons.append("wagtailcrx/icons/cr-recycle.svg")
  56. icons.append("wagtailcrx/icons/cr-stop.svg")
  57. icons.append("wagtailcrx/icons/cr-th-large.svg")
  58. icons.append("wagtailcrx/icons/cr-universal-access.svg")
  59. icons.append("wagtailcrx/icons/cr-usd.svg")
  60. icons.append("wagtailcrx/icons/cr-window-maximize.svg")
  61. icons.append("wagtailcrx/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 codered_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 wagtailcrx.models import CoderedFormMixin
  83. form_models = [
  84. model for model in get_page_models()
  85. if issubclass(model, CoderedFormMixin)
  86. ]
  87. form_types = list(
  88. ContentType.objects.get_for_models(*form_models).values()
  89. )
  90. editable_forms = UserPagePermissionsProxy(user).editable_pages()
  91. editable_forms = editable_forms.filter(content_type__in=form_types)
  92. return editable_forms
  93. @hooks.register('before_serve_document')
  94. def serve_document_directly(document, request):
  95. """
  96. This hook prevents documents from being downloaded unless
  97. specified by an <a> tag with the download attribute.
  98. """
  99. content_type, content_encoding = mimetypes.guess_type(document.filename)
  100. response = HttpResponse(document.file.read(), content_type=content_type)
  101. response['Content-Disposition'] = 'inline;filename="{0}"'.format(document.filename)
  102. response['Content-Encoding'] = content_encoding
  103. return response
  104. class ImportExportMenuItem(MenuItem):
  105. def is_shown(self, request):
  106. return request.user.is_superuser
  107. @hooks.register('register_settings_menu_item')
  108. def register_import_export_menu_item():
  109. return ImportExportMenuItem(
  110. _('Import'),
  111. reverse('import_index'),
  112. classnames='icon icon-download',
  113. )