wagtail_hooks.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from wagtail import hooks
  2. from wagtail.admin.filters import WagtailFilterSet
  3. from wagtail.admin.userbar import AccessibilityItem
  4. from wagtail.admin.utils import get_admin_base_url
  5. from wagtail.snippets.models import register_snippet
  6. from wagtail.snippets.views.snippets import SnippetViewSet, SnippetViewSetGroup
  7. from wagtail_headless_preview.settings import headless_preview_settings
  8. from bakerydemo.base.filters import RevisionFilterSetMixin
  9. from bakerydemo.base.models import FooterText, Person
  10. """
  11. N.B. To see what icons are available for use in Wagtail menus and StreamField block types,
  12. enable the styleguide in settings:
  13. INSTALLED_APPS = (
  14. ...
  15. 'wagtail.contrib.styleguide',
  16. ...
  17. )
  18. or see https://thegrouchy.dev/general/2015/12/06/wagtail-streamfield-icons.html
  19. This demo project also includes the wagtail-font-awesome-svg package, allowing further icons to be
  20. installed as detailed here: https://github.com/allcaps/wagtail-font-awesome-svg#usage
  21. """
  22. @hooks.register("register_icons")
  23. def register_icons(icons):
  24. return icons + [
  25. "wagtailfontawesomesvg/solid/suitcase.svg",
  26. "wagtailfontawesomesvg/solid/utensils.svg",
  27. ]
  28. class CustomAccessibilityItem(AccessibilityItem):
  29. axe_run_only = None
  30. def get_axe_spec(self, request):
  31. spec = super().get_axe_spec(request)
  32. spec["allowedOrigins"] = [
  33. headless_preview_settings.CLIENT_URLS["default"]
  34. if self.in_editor
  35. else get_admin_base_url()
  36. ]
  37. return spec
  38. @hooks.register("construct_wagtail_userbar")
  39. def replace_userbar_accessibility_item(request, items, page):
  40. items[:] = [
  41. CustomAccessibilityItem(in_editor=item.in_editor)
  42. if isinstance(item, AccessibilityItem)
  43. else item
  44. for item in items
  45. ]
  46. class PersonFilterSet(RevisionFilterSetMixin, WagtailFilterSet):
  47. class Meta:
  48. model = Person
  49. fields = {
  50. "job_title": ["icontains"],
  51. "live": ["exact"],
  52. "locked": ["exact"],
  53. }
  54. class PersonViewSet(SnippetViewSet):
  55. # Instead of decorating the Person model class definition in models.py with
  56. # @register_snippet - which has Wagtail automatically generate an admin interface for this model - we can also provide our own
  57. # SnippetViewSet class which allows us to customize the admin interface for this snippet.
  58. # See the documentation for SnippetViewSet for more details
  59. # https://docs.wagtail.org/en/stable/reference/viewsets.html#snippetviewset
  60. model = Person
  61. menu_label = "People" # ditch this to use verbose_name_plural from model
  62. icon = "group" # change as required
  63. list_display = ("first_name", "last_name", "job_title", "thumb_image")
  64. list_export = ("first_name", "last_name", "job_title")
  65. filterset_class = PersonFilterSet
  66. class FooterTextFilterSet(RevisionFilterSetMixin, WagtailFilterSet):
  67. class Meta:
  68. model = FooterText
  69. fields = {
  70. "live": ["exact"],
  71. }
  72. class FooterTextViewSet(SnippetViewSet):
  73. model = FooterText
  74. search_fields = ("body",)
  75. filterset_class = FooterTextFilterSet
  76. class BakerySnippetViewSetGroup(SnippetViewSetGroup):
  77. menu_label = "Bakery Misc"
  78. menu_icon = "utensils" # change as required
  79. menu_order = 300 # will put in 4th place (000 being 1st, 100 2nd)
  80. items = (PersonViewSet, FooterTextViewSet)
  81. # When using a SnippetViewSetGroup class to group several SnippetViewSet classes together,
  82. # you only need to register the SnippetViewSetGroup class with Wagtail:
  83. register_snippet(BakerySnippetViewSetGroup)