wagtail_hooks.py 2.5 KB

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