瀏覽代碼

Replace ModelAdmin with SnippetViewSet for Person and FooterText models

Sage Abdullah 1 年之前
父節點
當前提交
cfc3338abb
共有 2 個文件被更改,包括 18 次插入16 次删除
  1. 6 7
      bakerydemo/base/models.py
  2. 12 9
      bakerydemo/base/wagtail_hooks.py

+ 6 - 7
bakerydemo/base/models.py

@@ -21,12 +21,10 @@ from wagtail.models import (
     RevisionMixin,
 )
 from wagtail.search import index
-from wagtail.snippets.models import register_snippet
 
 from .blocks import BaseStreamBlock
 
 
-@register_snippet
 class Person(
     DraftStateMixin,
     RevisionMixin,
@@ -36,8 +34,8 @@ class Person(
 ):
     """
     A Django model to store Person objects.
-    It uses the `@register_snippet` decorator to allow it to be accessible
-    via the Snippets UI (e.g. /admin/snippets/base/person/)
+    It is registered using `register_snippet` as a function in wagtail_hooks.py
+    to allow it to have a menu item within a custom menu item group.
 
     `Person` uses the `ClusterableModel`, which allows the relationship with
     another model to be stored locally to the 'parent' model (e.g. a PageModel)
@@ -79,6 +77,7 @@ class Person(
     search_fields = [
         index.SearchField("first_name"),
         index.SearchField("last_name"),
+        index.FilterField("job_title"),
         index.AutocompleteField("first_name"),
         index.AutocompleteField("last_name"),
     ]
@@ -137,11 +136,11 @@ class Person(
         verbose_name_plural = "People"
 
 
-@register_snippet
 class FooterText(DraftStateMixin, RevisionMixin, PreviewableMixin, models.Model):
     """
-    This provides editable text for the site footer. Again it uses the decorator
-    `register_snippet` to allow it to be accessible via the admin. It is made
+    This provides editable text for the site footer. Again it is registered
+    using `register_snippet` as a function in wagtail_hooks.py to be grouped
+    together with the Person model inside the same main menu item. It is made
     accessible on the template via a template tag defined in base/templatetags/
     navigation_tags.py
     """

+ 12 - 9
bakerydemo/base/wagtail_hooks.py

@@ -4,6 +4,8 @@ from wagtail.contrib.modeladmin.options import (
     ModelAdminGroup,
     modeladmin_register,
 )
+from wagtail.snippets.models import register_snippet
+from wagtail.snippets.views.snippets import SnippetViewSet, SnippetViewSetGroup
 
 from bakerydemo.base.models import FooterText, Person
 from bakerydemo.breads.models import BreadIngredient, BreadType, Country
@@ -38,6 +40,7 @@ class BreadIngredientAdmin(ModelAdmin):
     # rather than under the default Snippets section.
     model = BreadIngredient
     search_fields = ("name",)
+    inspect_view_enabled = True
 
 
 class BreadTypeAdmin(ModelAdmin):
@@ -57,29 +60,29 @@ class BreadModelAdminGroup(ModelAdminGroup):
     items = (BreadIngredientAdmin, BreadTypeAdmin, BreadCountryAdmin)
 
 
-class PersonModelAdmin(ModelAdmin):
+class PersonViewSet(SnippetViewSet):
     model = Person
     menu_label = "People"  # ditch this to use verbose_name_plural from model
-    menu_icon = "group"  # change as required
+    icon = "group"  # change as required
     list_display = ("first_name", "last_name", "job_title", "thumb_image")
-    list_filter = ("job_title",)
-    search_fields = ("first_name", "last_name", "job_title")
-    inspect_view_enabled = True
+    list_filter = {
+        "job_title": ["icontains"],
+    }
 
 
-class FooterTextAdmin(ModelAdmin):
+class FooterTextViewSet(SnippetViewSet):
     model = FooterText
     search_fields = ("body",)
 
 
-class BakeryModelAdminGroup(ModelAdminGroup):
+class BakerySnippetViewSetGroup(SnippetViewSetGroup):
     menu_label = "Bakery Misc"
     menu_icon = "utensils"  # change as required
     menu_order = 300  # will put in 4th place (000 being 1st, 100 2nd)
-    items = (PersonModelAdmin, FooterTextAdmin)
+    items = (PersonViewSet, FooterTextViewSet)
 
 
 # When using a ModelAdminGroup class to group several ModelAdmin classes together,
 # you only need to register the ModelAdminGroup class with Wagtail:
 modeladmin_register(BreadModelAdminGroup)
-modeladmin_register(BakeryModelAdminGroup)
+register_snippet(BakerySnippetViewSetGroup)