wagtail_hooks.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. @hooks.register("construct_wagtail_userbar")
  29. def replace_userbar_accessibility_item(request, items):
  30. items[:] = [
  31. CustomAccessibilityItem() if isinstance(item, AccessibilityItem) else item
  32. for item in items
  33. ]
  34. class PersonFilterSet(RevisionFilterSetMixin, WagtailFilterSet):
  35. class Meta:
  36. model = Person
  37. fields = {
  38. "job_title": ["icontains"],
  39. "live": ["exact"],
  40. "locked": ["exact"],
  41. }
  42. class PersonViewSet(SnippetViewSet):
  43. # Instead of decorating the Person model class definition in models.py with
  44. # @register_snippet - which has Wagtail automatically generate an admin interface for this model - we can also provide our own
  45. # SnippetViewSet class which allows us to customize the admin interface for this snippet.
  46. # See the documentation for SnippetViewSet for more details
  47. # https://docs.wagtail.org/en/stable/reference/viewsets.html#snippetviewset
  48. model = Person
  49. menu_label = "People" # ditch this to use verbose_name_plural from model
  50. icon = "group" # change as required
  51. list_display = ("first_name", "last_name", "job_title", "thumb_image")
  52. list_export = ("first_name", "last_name", "job_title")
  53. filterset_class = PersonFilterSet
  54. class FooterTextFilterSet(RevisionFilterSetMixin, WagtailFilterSet):
  55. class Meta:
  56. model = FooterText
  57. fields = {
  58. "live": ["exact"],
  59. }
  60. class FooterTextViewSet(SnippetViewSet):
  61. model = FooterText
  62. search_fields = ("body",)
  63. filterset_class = FooterTextFilterSet
  64. class BakerySnippetViewSetGroup(SnippetViewSetGroup):
  65. menu_label = "Bakery Misc"
  66. menu_icon = "utensils" # change as required
  67. menu_order = 300 # will put in 4th place (000 being 1st, 100 2nd)
  68. items = (PersonViewSet, FooterTextViewSet)
  69. # When using a SnippetViewSetGroup class to group several SnippetViewSet classes together,
  70. # you only need to register the SnippetViewSetGroup class with Wagtail:
  71. register_snippet(BakerySnippetViewSetGroup)