wagtail_hooks.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from django.contrib.contenttypes.models import ContentType
  2. from django.templatetags.static import static
  3. from django.urls import reverse
  4. from django.utils.html import format_html
  5. from django.utils.translation import gettext_lazy as _
  6. from wagtail import hooks
  7. from wagtail.admin.menu import MenuItem
  8. from wagtail.models import get_page_models
  9. from wagtail.permissions import page_permission_policy
  10. from wagtailcache.cache import clear_cache
  11. from coderedcms import __version__
  12. @hooks.register("insert_global_admin_css")
  13. def global_admin_css():
  14. return format_html(
  15. '<link rel="stylesheet" type="text/css" href="{}?v={}">',
  16. static("coderedcms/css/crx-admin.css"),
  17. __version__,
  18. )
  19. @hooks.register("insert_editor_js")
  20. def collapsible_js():
  21. return format_html(
  22. '<script src="{}?v={}"></script>',
  23. static("coderedcms/js/crx-editor.js"),
  24. __version__,
  25. )
  26. @hooks.register("register_icons")
  27. def register_icons(icons):
  28. """
  29. Add custom SVG icons to the Wagtail admin.
  30. """
  31. # These SVG files should be in the django templates folder, and follow exact
  32. # specifications to work with Wagtail:
  33. # https://github.com/wagtail/wagtail/pull/6028
  34. icons.append("coderedcms/icons/cr-align-left.svg")
  35. icons.append("coderedcms/icons/cr-check-square-o.svg")
  36. icons.append("coderedcms/icons/cr-columns.svg")
  37. icons.append("coderedcms/icons/cr-desktop.svg")
  38. icons.append("coderedcms/icons/cr-font.svg")
  39. icons.append("coderedcms/icons/cr-google.svg")
  40. icons.append("coderedcms/icons/cr-hand-pointer-o.svg")
  41. icons.append("coderedcms/icons/cr-hashtag.svg")
  42. icons.append("coderedcms/icons/cr-header.svg")
  43. icons.append("coderedcms/icons/cr-list-alt.svg")
  44. icons.append("coderedcms/icons/cr-map.svg")
  45. icons.append("coderedcms/icons/cr-newspaper-o.svg")
  46. icons.append("coderedcms/icons/cr-puzzle-piece.svg")
  47. icons.append("coderedcms/icons/cr-recycle.svg")
  48. icons.append("coderedcms/icons/cr-stop.svg")
  49. icons.append("coderedcms/icons/cr-th-large.svg")
  50. icons.append("coderedcms/icons/cr-universal-access.svg")
  51. icons.append("coderedcms/icons/cr-usd.svg")
  52. icons.append("coderedcms/icons/cr-window-maximize.svg")
  53. icons.append("coderedcms/icons/cr-window-minimize.svg")
  54. return icons
  55. def clear_wagtailcache(*args, **kwargs):
  56. clear_cache()
  57. # Clear cache whenever pages/snippets are changed. Err on the side of clearing
  58. # the cache vs not clearing the cache, as this usually leads to support requests
  59. # when staff members make edits but do not see the changes.
  60. hooks.register("after_delete_page", clear_wagtailcache)
  61. hooks.register("after_move_page", clear_wagtailcache)
  62. hooks.register("after_publish_page", clear_wagtailcache)
  63. hooks.register("after_unpublish_page", clear_wagtailcache)
  64. hooks.register("after_create_snippet", clear_wagtailcache)
  65. hooks.register("after_edit_snippet", clear_wagtailcache)
  66. hooks.register("after_delete_snippet", clear_wagtailcache)
  67. @hooks.register("filter_form_submissions_for_user")
  68. def crx_forms(user, editable_forms):
  69. """
  70. Add our own CoderedFormPage to editable_forms, since wagtail is unaware
  71. of its existence. Essentially this is a fork of wagtail.contrib.forms.get_forms_for_user()
  72. and wagtail.contrib.forms.get_form_types()
  73. """
  74. from coderedcms.models import CoderedFormMixin
  75. # Get content types of pages that inherit from CRX mixins.
  76. form_models = [
  77. model
  78. for model in get_page_models()
  79. if issubclass(model, (CoderedFormMixin,))
  80. ]
  81. form_types = list(ContentType.objects.get_for_models(*form_models).values())
  82. # Get all pages this user can access.
  83. all_editable_pages = (
  84. page_permission_policy.instances_user_has_permission_for(user, "change")
  85. )
  86. crx_editable_forms = all_editable_pages.filter(content_type__in=form_types)
  87. # Combine the previous hook's ``editable_forms`` with our ``editable_forms``.
  88. combined_forms_pks = list(
  89. crx_editable_forms.values_list("pk", flat=True)
  90. ) + list(editable_forms.values_list("pk", flat=True))
  91. combined_editable_forms = all_editable_pages.filter(
  92. pk__in=combined_forms_pks
  93. )
  94. return combined_editable_forms
  95. class ImportExportMenuItem(MenuItem):
  96. def is_shown(self, request):
  97. return request.user.is_superuser
  98. @hooks.register("register_settings_menu_item")
  99. def register_import_export_menu_item():
  100. return ImportExportMenuItem(
  101. _("Import"),
  102. reverse("import_index"),
  103. classnames="icon icon-download",
  104. )