wagtail_hooks.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from wagtail import hooks
  2. from wagtail.contrib.modeladmin.options import (
  3. ModelAdmin,
  4. ModelAdminGroup,
  5. modeladmin_register,
  6. )
  7. from wagtail.snippets.models import register_snippet
  8. from wagtail.snippets.views.snippets import SnippetViewSet, SnippetViewSetGroup
  9. from bakerydemo.base.models import FooterText, Person
  10. from bakerydemo.breads.models import BreadIngredient, BreadType, Country
  11. """
  12. N.B. To see what icons are available for use in Wagtail menus and StreamField block types,
  13. enable the styleguide in settings:
  14. INSTALLED_APPS = (
  15. ...
  16. 'wagtail.contrib.styleguide',
  17. ...
  18. )
  19. or see https://thegrouchy.dev/general/2015/12/06/wagtail-streamfield-icons.html
  20. This demo project also includes the wagtail-font-awesome-svg package, allowing further icons to be
  21. installed as detailed here: https://github.com/allcaps/wagtail-font-awesome-svg#usage
  22. """
  23. @hooks.register("register_icons")
  24. def register_icons(icons):
  25. return icons + [
  26. "wagtailfontawesomesvg/solid/suitcase.svg",
  27. "wagtailfontawesomesvg/solid/utensils.svg",
  28. ]
  29. class BreadIngredientAdmin(ModelAdmin):
  30. # These stub classes allow us to put various models into the custom "Wagtail Bakery" menu item
  31. # rather than under the default Snippets section.
  32. model = BreadIngredient
  33. search_fields = ("name",)
  34. inspect_view_enabled = True
  35. class BreadTypeAdmin(ModelAdmin):
  36. model = BreadType
  37. search_fields = ("title",)
  38. class BreadCountryAdmin(ModelAdmin):
  39. model = Country
  40. search_fields = ("title",)
  41. class BreadModelAdminGroup(ModelAdminGroup):
  42. menu_label = "Bread Categories"
  43. menu_icon = "suitcase" # change as required
  44. menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
  45. items = (BreadIngredientAdmin, BreadTypeAdmin, BreadCountryAdmin)
  46. class PersonViewSet(SnippetViewSet):
  47. model = Person
  48. menu_label = "People" # ditch this to use verbose_name_plural from model
  49. icon = "group" # change as required
  50. list_display = ("first_name", "last_name", "job_title", "thumb_image")
  51. list_filter = {
  52. "job_title": ["icontains"],
  53. }
  54. class FooterTextViewSet(SnippetViewSet):
  55. model = FooterText
  56. search_fields = ("body",)
  57. class BakerySnippetViewSetGroup(SnippetViewSetGroup):
  58. menu_label = "Bakery Misc"
  59. menu_icon = "utensils" # change as required
  60. menu_order = 300 # will put in 4th place (000 being 1st, 100 2nd)
  61. items = (PersonViewSet, FooterTextViewSet)
  62. # When using a ModelAdminGroup class to group several ModelAdmin classes together,
  63. # you only need to register the ModelAdminGroup class with Wagtail:
  64. modeladmin_register(BreadModelAdminGroup)
  65. register_snippet(BakerySnippetViewSetGroup)