Repository: jazzband/wagtailmenus (language: python)

Issue:
Migrate to wagtail.snippets or wagtail-modeladmin 
Seeing as how wagtail.contrib.modeladmin is going away in Wagtail 6.0, are there plans to update this package to support either wagtail.snippets or move to wagtail-modeladmin? Would be interested to know the roadmap here.


Discussion / hints:
We will likely transition to snippets as it's more aligned to the overall Wagtail 6 architecture.

wagtail-modeladmin feels more like a maintenance package and may not be updated in the future.
I'm working on it : https://github.com/frague59/wagtailmenus
Is it perhaps worth a quick fix to make use of the decoupled `wagtail_modeladmin` package, as per the instructions at https://github.com/wagtail-nest/wagtail-modeladmin/blob/main/docs/installation.md?

This will at least remove the annoying deprecation messages while the more substantial changes are made to use snippets.

I'm happy to do this, and to assist (maybe with testing if you are making progress @frague59) with the snippet version.

Fix patch:
diff --git a/docs/source/installation.rst b/docs/source/installation.rst
index 893beb45..7ecd9486 100644
--- a/docs/source/installation.rst
+++ b/docs/source/installation.rst
@@ -4,13 +4,14 @@
 Installing wagtailmenus
 =========================
 
-1.  Install the package using pip:
+1.  Install packages using pip:
 
     .. code-block:: console
 
         pip install wagtailmenus
+        pip install wagtail_modeladmin  # if Wagtail >= 5.1
 
-2.  Add ``wagtailmenus`` and ``wagtail.contrib.modeladmin`` to the
+2.  Add ``wagtailmenus`` and ``modeladmin`` to the
     ``INSTALLED_APPS`` setting in your project settings:
 
     .. code-block:: python
@@ -18,7 +19,8 @@ Installing wagtailmenus
 
         INSTALLED_APPS = [
             ...
-            'wagtail.contrib.modeladmin',  # Don't repeat if it's there already
+            'wagtail_modeladmin',          # if Wagtail >=5.1; Don't repeat if it's there already
+            'wagtail.contrib.modeladmin',  # if Wagtail <5.1;  Don't repeat if it's there already
             'wagtailmenus',
         ]
 
diff --git a/setup.py b/setup.py
index 059f3026..ebb3176b 100644
--- a/setup.py
+++ b/setup.py
@@ -25,6 +25,7 @@
 
 development_extras = [
     'wagtail>=2.15',
+    'wagtail_modeladmin>=1.0',
     'django-debug-toolbar',
     'django-extensions',
     'ipdb',
diff --git a/tox.ini b/tox.ini
index 1c529005..97b65a3d 100644
--- a/tox.ini
+++ b/tox.ini
@@ -6,7 +6,7 @@ envlist =
     py{38,39,310}-dj{40}-wt{41,42}
     py{38,39,310}-dj{32,41}-wt{41,42,50}
     py{311}-dj{41}-wt{41,42,50}
-    py{311}-dj{42}-wt{50}
+    py{38,39,310,311}-dj{41,42}-wt{51}-wma{10}
 
 [gh-actions]
 python =
@@ -23,6 +23,7 @@ commands = coverage run --source=wagtailmenus runtests.py
 
 deps =
     coverage
+    wma10: wagtail_modeladmin>=1.0,<1.1
     dj32: Django>=3.2,<4.0
     dj40: Django>=4.0,<4.1
     dj41: Django>=4.1,<4.2
@@ -30,3 +31,4 @@ deps =
     wt41: wagtail>=4.1,<4.2
     wt42: wagtail>=4.2,<5.0
     wt50: wagtail>=5.0,<5.1
+    wt51: wagtail>=5.1,<5.2
diff --git a/wagtailmenus/modeladmin.py b/wagtailmenus/modeladmin.py
index 6458351b..c04abb0b 100644
--- a/wagtailmenus/modeladmin.py
+++ b/wagtailmenus/modeladmin.py
@@ -2,8 +2,12 @@
 from django.urls import re_path
 from django.utils.safestring import mark_safe
 from django.utils.translation import gettext_lazy as _
-from wagtail.contrib.modeladmin.helpers import ButtonHelper
-from wagtail.contrib.modeladmin.options import ModelAdmin
+try:
+    from wagtail_modeladmin.helpers import ButtonHelper
+    from wagtail_modeladmin.options import ModelAdmin
+except ModuleNotFoundError:
+    from wagtail.contrib.modeladmin.helpers import ButtonHelper
+    from wagtail.contrib.modeladmin.options import ModelAdmin
 
 from wagtailmenus import views
 from wagtailmenus.conf import settings
diff --git a/wagtailmenus/settings/base.py b/wagtailmenus/settings/base.py
index 7b5ab339..45a7a9f8 100644
--- a/wagtailmenus/settings/base.py
+++ b/wagtailmenus/settings/base.py
@@ -1,5 +1,7 @@
 import os
 
+import wagtail
+
 DEBUG = True
 SITE_ID = 1
 
@@ -10,7 +12,7 @@
 LANGUAGE_CODE = 'en'
 
 
-INSTALLED_APPS = (
+INSTALLED_APPS = [
     'wagtailmenus',
 
     'taggit',
@@ -35,9 +37,12 @@
     'wagtail.contrib.redirects',
     'wagtail.contrib.routable_page',
     'wagtail.contrib.settings',
-    'wagtail.contrib.modeladmin',
+]
 
-)
+if wagtail.VERSION >= (5, 1):
+    INSTALLED_APPS += ['wagtail_modeladmin']
+else:
+    INSTALLED_APPS += ['wagtail.contrib.modeladmin']
 
 PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
 STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
diff --git a/wagtailmenus/views.py b/wagtailmenus/views.py
index 90a6927f..453cbbe4 100644
--- a/wagtailmenus/views.py
+++ b/wagtailmenus/views.py
@@ -8,8 +8,12 @@
 from django.utils.translation import gettext as _
 from wagtail.admin import messages
 from wagtail.admin.panels import ObjectList, TabbedInterface
-from wagtail.contrib.modeladmin.views import (CreateView, EditView,
-                                              ModelFormView, WMABaseView)
+try:
+    from wagtail_modeladmin.views import (CreateView, EditView,
+                                          ModelFormView, WMABaseView)
+except ModuleNotFoundError:
+    from wagtail.contrib.modeladmin.views import (CreateView, EditView,
+                                                  ModelFormView, WMABaseView)
 from wagtail.models import Site
 
 from wagtailmenus.conf import settings
diff --git a/wagtailmenus/wagtail_hooks.py b/wagtailmenus/wagtail_hooks.py
index 416f0180..675f0bc6 100644
--- a/wagtailmenus/wagtail_hooks.py
+++ b/wagtailmenus/wagtail_hooks.py
@@ -1,5 +1,8 @@
 from wagtail import hooks
-from wagtail.contrib.modeladmin.options import modeladmin_register
+try:
+    from wagtail_modeladmin.options import modeladmin_register
+except ModuleNotFoundError:
+    from wagtail.contrib.modeladmin.options import modeladmin_register
 
 from wagtailmenus.conf import settings
 from wagtailmenus.utils.misc import derive_section_root


Test patch:
diff --git a/wagtailmenus/conf/tests/test_modeladmin_disable_settings.py b/wagtailmenus/conf/tests/test_modeladmin_disable_settings.py
index fa3fe62b..891c4482 100644
--- a/wagtailmenus/conf/tests/test_modeladmin_disable_settings.py
+++ b/wagtailmenus/conf/tests/test_modeladmin_disable_settings.py
@@ -6,12 +6,18 @@
 from wagtailmenus import wagtail_hooks
 from wagtailmenus.modeladmin import FlatMenuAdmin, MainMenuAdmin
 
+try:
+    from wagtail_modeladmin.options import modeladmin_register
+    modeladmin_register_str = 'wagtail_modeladmin.options.modeladmin_register'
+except ModuleNotFoundError:
+    from wagtail.contrib.modeladmin.options import modeladmin_register
+    modeladmin_register_str = 'wagtail.contrib.modeladmin.options.modeladmin_register'
 
 class TestDisablingFlatMenusInWagtailCMS(TestCase):
     """
     Tests if modeladmin is registered based on settings.
     """
-    @patch('wagtail.contrib.modeladmin.options.modeladmin_register')
+    @patch(modeladmin_register_str)
     def test_modeladmin_registered_by_default(self, mock_modeladmin_register):
         reload(wagtail_hooks)
         self.assertIn(
@@ -22,7 +28,7 @@ def test_modeladmin_registered_by_default(self, mock_modeladmin_register):
     @override_settings(
         WAGTAILMENUS_FLAT_MENUS_EDITABLE_IN_WAGTAILADMIN=False,
     )
-    @patch('wagtail.contrib.modeladmin.options.modeladmin_register')
+    @patch(modeladmin_register_str)
     def test_modeladmin_not_registered_if_disabled(self, mock_modeladmin_register):
         """
         Disabling the 'Flat menu' via setting should prevent registering the
@@ -38,7 +44,7 @@ def test_modeladmin_not_registered_if_disabled(self, mock_modeladmin_register):
 
 class TestDisablingMainMenusInWagtailCMS(TestCase):
 
-    @patch('wagtail.contrib.modeladmin.options.modeladmin_register')
+    @patch(modeladmin_register_str)
     def test_modeladmin_registered_by_default(self, mock_modeladmin_register):
         reload(wagtail_hooks)
         self.assertIn(
@@ -49,7 +55,7 @@ def test_modeladmin_registered_by_default(self, mock_modeladmin_register):
     @override_settings(
         WAGTAILMENUS_MAIN_MENUS_EDITABLE_IN_WAGTAILADMIN=False,
     )
-    @patch('wagtail.contrib.modeladmin.options.modeladmin_register')
+    @patch(modeladmin_register_str)
     def test_disable_main_menus(self, mock_modeladmin_register):
         """
         Disabling the 'Main menu' via setting should prevent registering the