浏览代码

Move get_snippet_panel to get_model_panel and fix missing return in get_snippet_edit_handler

Sage Abdullah 2 年之前
父节点
当前提交
c634fc11cc
共有 5 个文件被更改,包括 47 次插入28 次删除
  1. 1 0
      CHANGELOG.txt
  2. 4 0
      docs/releases/4.0.md
  3. 19 3
      wagtail/admin/panels.py
  4. 17 6
      wagtail/snippets/tests.py
  5. 6 19
      wagtail/snippets/views/snippets.py

+ 1 - 0
CHANGELOG.txt

@@ -41,6 +41,7 @@ Changelog
  * Rename `Page.get_latest_revision_as_page` to `Page.get_latest_revision_as_object` (Sage Abdullah)
  * Cache model permission codenames in PermissionHelper (Tidiane Dia)
  * Selecting a new parent page for moving a page now uses the chooser modal which allows searching (Viggo de Vries)
+ * Move `get_snippet_edit_handler` function to `wagtail.admin.panels.get_edit_handler` (Sage Abdullah)
  * Fix: Typo in `ResumeWorkflowActionFormatter` message (Stefan Hammer)
  * Fix: Throw a meaningful error when saving an image to an unrecognised image format (Christian Franke)
  * Fix: Remove extra padding for headers with breadcrumbs on mobile viewport (Steven Steinwand)

+ 4 - 0
docs/releases/4.0.md

@@ -119,3 +119,7 @@ The `Page.get_latest_revision_as_page` method has been renamed to `Page.get_late
 ### `AdminChooser` replaced with `BaseChooser`
 
 Custom choosers should no longer use `wagtail.admin.widgets.chooser.AdminChooser` which has been replaced with `wagtail.admin.widgets.chooser.BaseChooser`.
+
+### `get_snippet_edit_handler` moved to `wagtail.admin.panels.get_edit_handler`
+
+The `get_snippet_edit_handler` function in `wagtail.snippets.views.snippets` has been moved to `get_edit_handler` in `wagtail.admin.panels`.

+ 19 - 3
wagtail/admin/panels.py

@@ -1082,7 +1082,7 @@ set_default_page_edit_handlers(Page)
 
 
 @cached_classmethod
-def get_edit_handler(cls):
+def _get_page_edit_handler(cls):
     """
     Get the panel to use in the Wagtail admin when editing this page type.
     """
@@ -1111,11 +1111,26 @@ def get_edit_handler(cls):
     return edit_handler.bind_to_model(cls)
 
 
-Page.get_edit_handler = get_edit_handler
+Page.get_edit_handler = _get_page_edit_handler
+
+
+@functools.lru_cache(maxsize=None)
+def get_edit_handler(model):
+    """
+    Get the panel to use in the Wagtail admin when editing this model.
+    """
+    if hasattr(model, "edit_handler"):
+        # use the edit handler specified on the model class
+        panel = model.edit_handler
+    else:
+        panels = extract_panel_definitions_from_model_class(model)
+        panel = ObjectList(panels)
+
+    return panel.bind_to_model(model)
 
 
 @receiver(setting_changed)
-def reset_page_edit_handler_cache(**kwargs):
+def reset_edit_handler_cache(**kwargs):
     """
     Clear page edit handler cache when global WAGTAILADMIN_COMMENTS_ENABLED settings are changed
     """
@@ -1124,6 +1139,7 @@ def reset_page_edit_handler_cache(**kwargs):
         for model in apps.get_models():
             if issubclass(model, Page):
                 model.get_edit_handler.cache_clear()
+        get_edit_handler.cache_clear()
 
 
 class StreamFieldPanel(FieldPanel):

+ 17 - 6
wagtail/snippets/tests.py

@@ -21,7 +21,7 @@ from taggit.models import Tag
 from wagtail import hooks
 from wagtail.admin.admin_url_finder import AdminURLFinder
 from wagtail.admin.forms import WagtailAdminModelForm
-from wagtail.admin.panels import FieldPanel, ObjectList
+from wagtail.admin.panels import FieldPanel, ObjectList, Panel, get_edit_handler
 from wagtail.blocks.field_block import FieldBlockAdapter
 from wagtail.models import Locale, ModelLogEntry, Page
 from wagtail.snippets.action_menu import (
@@ -30,7 +30,7 @@ from wagtail.snippets.action_menu import (
 )
 from wagtail.snippets.blocks import SnippetChooserBlock
 from wagtail.snippets.models import SNIPPET_MODELS, register_snippet
-from wagtail.snippets.views.snippets import get_snippet_panel
+from wagtail.snippets.views.snippets import get_snippet_edit_handler
 from wagtail.snippets.widgets import (
     AdminSnippetChooser,
     SnippetChooserAdapter,
@@ -59,6 +59,7 @@ from wagtail.test.testapp.models import (
     SnippetChooserModelWithCustomPrimaryKey,
 )
 from wagtail.test.utils import WagtailTestUtils
+from wagtail.utils.deprecation import RemovedInWagtail50Warning
 
 
 class TestSnippetIndexView(TestCase, WagtailTestUtils):
@@ -1256,7 +1257,7 @@ class TestSnippetChooserPanel(TestCase, WagtailTestUtils):
             advert=Advert.objects.create(text=self.advert_text)
         )
 
-        self.edit_handler = get_snippet_panel(model)
+        self.edit_handler = get_edit_handler(model)
         self.form_class = self.edit_handler.get_form_class()
         form = self.form_class(instance=test_snippet)
         edit_handler = self.edit_handler.get_bound_panel(
@@ -1915,17 +1916,27 @@ class TestDeleteOnlyPermissions(TestCase, WagtailTestUtils):
 
 class TestSnippetEditHandlers(TestCase, WagtailTestUtils):
     def test_standard_edit_handler(self):
-        edit_handler = get_snippet_panel(StandardSnippet)
+        edit_handler = get_edit_handler(StandardSnippet)
         form_class = edit_handler.get_form_class()
         self.assertTrue(issubclass(form_class, WagtailAdminModelForm))
         self.assertFalse(issubclass(form_class, FancySnippetForm))
 
     def test_fancy_edit_handler(self):
-        edit_handler = get_snippet_panel(FancySnippet)
+        edit_handler = get_edit_handler(FancySnippet)
         form_class = edit_handler.get_form_class()
         self.assertTrue(issubclass(form_class, WagtailAdminModelForm))
         self.assertTrue(issubclass(form_class, FancySnippetForm))
 
+    def test_get_snippet_edit_handler(self):
+        # TODO: Remove in Wagtail 5.0
+        with self.assertWarnsMessage(
+            RemovedInWagtail50Warning,
+            "The get_snippet_edit_handler function has been moved to wagtail.admin.panels.get_edit_handler",
+        ):
+            edit_handler = get_snippet_edit_handler(StandardSnippet)
+        self.assertIsNotNone(edit_handler)
+        self.assertIsInstance(edit_handler, Panel)
+
 
 class TestInlinePanelMedia(TestCase, WagtailTestUtils):
     """
@@ -2294,7 +2305,7 @@ class TestSnippetChooserPanelWithCustomPrimaryKey(TestCase, WagtailTestUtils):
             )
         )
 
-        self.edit_handler = get_snippet_panel(model)
+        self.edit_handler = get_edit_handler(model)
         self.form_class = self.edit_handler.get_form_class()
         form = self.form_class(instance=test_snippet)
         edit_handler = self.edit_handler.get_bound_panel(

+ 6 - 19
wagtail/snippets/views/snippets.py

@@ -1,5 +1,5 @@
 import warnings
-from functools import lru_cache, partial
+from functools import partial
 from urllib.parse import urlencode
 
 import django_filters
@@ -17,7 +17,7 @@ from django.views.generic import TemplateView
 
 from wagtail.admin import messages
 from wagtail.admin.filters import DateRangePickerWidget, WagtailFilterSet
-from wagtail.admin.panels import ObjectList, extract_panel_definitions_from_model_class
+from wagtail.admin.panels import get_edit_handler
 from wagtail.admin.ui.tables import Column, DateColumn, InlineActionsTable, UserColumn
 from wagtail.admin.views.generic import CreateView, DeleteView, EditView, IndexView
 from wagtail.admin.views.generic.mixins import RevisionsRevertMixin
@@ -52,26 +52,13 @@ def get_snippet_model_from_url_params(app_name, model_name):
     return model
 
 
-@lru_cache(maxsize=None)
-def get_snippet_panel(model):
-    if hasattr(model, "edit_handler"):
-        # use the edit handler specified on the snippet class
-        panel = model.edit_handler
-    else:
-        panels = extract_panel_definitions_from_model_class(model)
-        panel = ObjectList(panels)
-
-    return panel.bind_to_model(model)
-
-
 def get_snippet_edit_handler(model):
-    get_snippet_panel(model)
-
     warnings.warn(
-        "The get_snippet_edit_handler function has been renamed to get_snippet_panel",
+        "The get_snippet_edit_handler function has been moved to wagtail.admin.panels.get_edit_handler",
         category=RemovedInWagtail50Warning,
         stacklevel=2,
     )
+    return get_edit_handler(model)
 
 
 # == Views ==
@@ -193,7 +180,7 @@ class Create(CreateView):
         return self.run_hook("after_create_snippet", self.request, self.object)
 
     def get_panel(self):
-        return get_snippet_panel(self.model)
+        return get_edit_handler(self.model)
 
     def get_add_url(self):
         url = reverse(self.add_url_name)
@@ -291,7 +278,7 @@ class Edit(EditView):
         super().setup(request, *args, **kwargs)
 
     def get_panel(self):
-        return get_snippet_panel(self.model)
+        return get_edit_handler(self.model)
 
     def get_object(self, queryset=None):
         return get_object_or_404(self.model, pk=unquote(self.pk))