2
0

wagtail_hooks.py 3.5 KB

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