wagtail_hooks.py 3.4 KB

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