Skip to content

Commit

Permalink
Merge pull request #472 from dkirkham/migrate-modeladmin-snippets
Browse files Browse the repository at this point in the history
Migrate from ModelAdmin to Snippets
  • Loading branch information
schlich authored Feb 21, 2024
2 parents 00652aa + 06cc104 commit 84b1ee6
Show file tree
Hide file tree
Showing 18 changed files with 381 additions and 330 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This is a [Jazzband](https://jazzband.co/) project. By contributing you agree to
* Gabriel Augendre (Crocmagnon)
* Bojan Mihelac (bmihelac)
* Ben Froelich-Leon (benfroelich)
* Daniel Kirkham (dkirkham)

## Translators

Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ wagtailmenus is an extension for Torchbox's `Wagtail CMS <https://github.com/tor

The current version is tested for compatibility with the following:

- Wagtail versions >= 4.1
- Django versions 3.2, 4.1, and 4.2
- Wagtail versions >= 5.2
- Django versions 3.2, 4.2 and >= 5.0
- Python versions 3.8 to 3.12

.. image:: https://raw.githubusercontent.com/jazzband/wagtailmenus/master/docs/source/_static/images/repeating-item.png
Expand Down
3 changes: 0 additions & 3 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Installing wagtailmenus
.. code-block:: console
pip install wagtailmenus
pip install wagtail_modeladmin # if Wagtail >= 5.1
2. Add ``wagtailmenus`` and ``modeladmin`` to the
``INSTALLED_APPS`` setting in your project settings:
Expand All @@ -19,8 +18,6 @@ Installing wagtailmenus
INSTALLED_APPS = [
...
'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',
]
Expand Down
7 changes: 2 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

# Essential dependencies
requires = [
'wagtail>=5.2',
'django-cogwheels==0.3',
]

Expand All @@ -24,8 +25,7 @@
]

development_extras = [
'wagtail>=2.15',
'wagtail_modeladmin>=1.0',
'wagtail>=5.2',
'django-debug-toolbar',
'django-extensions',
'ipdb',
Expand Down Expand Up @@ -71,9 +71,6 @@
'Framework :: Django :: 3.2',
'Framework :: Django :: 4.1',
'Framework :: Django :: 4.2',
'Framework :: Wagtail :: 2',
'Framework :: Wagtail :: 3',
'Framework :: Wagtail :: 4',
'Framework :: Wagtail :: 5',
'Topic :: Internet :: WWW/HTTP',
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
Expand Down
17 changes: 9 additions & 8 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ skipsdist = True
usedevelop = True

envlist =
py{38,39,310}-dj{32,41}-wt{41,42,50}
py{311,312}-dj41-wt{41,42,50}
py{38,39,310,311,312}-dj{41,42}-wt{51,52}-wma{10}
wt42-dj41-py310
wt52-dj32-py{38,39,310}
wt52-dj41-py{38,39,310,311}
wt52-dj42-py{38,39,310,311,312}
wt52-dj50-py{310,311,312}
wt60-dj42-py{38,39,310,311,312}
wt60-dj50-py{310,311,312}

[gh-actions]
python =
Expand All @@ -23,12 +27,9 @@ commands = coverage run --source=wagtailmenus runtests.py

deps =
coverage
wma10: wagtail_modeladmin>=1.0,<1.1
dj32: Django>=3.2,<4.0
dj41: Django>=4.1,<4.2
dj42: Django>=4.2,<4.3
wt41: wagtail>=4.1,<4.2
wt42: wagtail>=4.2,<5.0
wt50: wagtail>=5.0,<5.1
wt51: wagtail>=5.1,<5.2
dj50: Django>=5.0,<5.1
wt52: wagtail>=5.2,<5.3
wt60: wagtail>=6.0,<6.1
4 changes: 2 additions & 2 deletions wagtailmenus/conf/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

FLAT_MENUS_EDITABLE_IN_WAGTAILADMIN = True

FLAT_MENUS_MODELADMIN_CLASS = 'wagtailmenus.modeladmin.FlatMenuAdmin'
FLAT_MENUS_ADMIN_CLASS = 'wagtailmenus.modeladmin.FlatMenuAdmin'

FLAT_MENUS_HANDLE_CHOICES = None

MAINMENU_MENU_ICON = 'list-ol'

MAIN_MENUS_EDITABLE_IN_WAGTAILADMIN = True

MAIN_MENUS_MODELADMIN_CLASS = 'wagtailmenus.modeladmin.MainMenuAdmin'
MAIN_MENUS_ADMIN_CLASS = 'wagtailmenus.modeladmin.MainMenuAdmin'

USE_CONDENSEDINLINEPANEL = True

Expand Down
8 changes: 2 additions & 6 deletions wagtailmenus/conf/tests/test_modeladmin_disable_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
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'
modeladmin_register_str = 'wagtail.snippets.models.register_snippet'


class TestDisablingFlatMenusInWagtailCMS(TestCase):
"""
Expand Down
82 changes: 82 additions & 0 deletions wagtailmenus/copyable_snippetviewset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from django.contrib.admin.utils import quote
from django.urls import path, reverse
from django.utils.translation import gettext_lazy as _

from wagtail.admin.widgets.button import ListingButton
from wagtail.snippets.views.snippets import CreateView, IndexView, SnippetViewSet

"""
Extension to SnippetViewSet which enables Snippet copying
Can be removed once Wagtail supports this function natively,
with some small refactoring in FlatMenuAdmin.
"""

class CopyView(CreateView):
view_name = "copy"
template_name = "wagtailsnippets/snippets/create.html"
error_message = _("The snippet could not be copied due to errors.")

def run_before_hook(self):
return self.run_hook("before_copy_snippet", self.request, self.model)

def run_after_hook(self):
return self.run_hook("after_copy_snippet", self.request, self.object)

def _get_initial_form_instance(self):
return self.get_object()


class CopyableSnippetIndexView(IndexView):
copy_url_name = None

def get_copy_url(self, instance):
return reverse(self.copy_url_name, args=(quote(instance.pk),))

def get_list_more_buttons(self, instance):
r = super().get_list_more_buttons(instance)
r.append(ListingButton(
_("Copy"),
url=self.get_copy_url(instance),
icon_name="copy",
attrs={ "aria-label": _("Copy '%(title)s'") % {"title": str(instance)} },
priority=15,
))
return r


class CopyableSnippetViewSet(SnippetViewSet):
#: The view class to use for the copy view; must be a subclass of ``wagtail.snippet.views.snippets.CopyView``.
copy_view_class = CopyView

#: The template to use for the edit view.
copy_template_name = ""

@property
def copy_view(self):
return self.copy_view_class.as_view(
model=self.model,
template_name=self.get_copy_template(),
header_icon=self.icon,
permission_policy=self.permission_policy,
panel=self._edit_handler,
form_class=self.get_form_class(),
index_url_name=self.get_url_name("list"),
add_url_name=self.get_url_name("add"),
edit_url_name=self.get_url_name("edit"),
preview_url_name=self.get_url_name("preview_on_add"),
)

def get_common_view_kwargs(self, **kwargs):
view_kwargs = super().get_common_view_kwargs(**kwargs)
view_kwargs["copy_url_name"] = self.get_url_name("copy")
return view_kwargs

def get_copy_template(self):
return self.copy_template_name or self.get_templates("copy")

def get_urlpatterns(self):
urls = super().get_urlpatterns()

return urls + [
path("copy/<str:pk>/", self.copy_view, name="copy")
]
21 changes: 21 additions & 0 deletions wagtailmenus/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from django import forms
from django.contrib.admin.utils import quote
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from wagtail.admin.forms import WagtailAdminModelForm, WagtailAdminPageForm
from wagtail.models import Site

from wagtailmenus.conf import settings

Expand All @@ -22,3 +26,20 @@ def __init__(self, *args, **kwargs):
"By default, this will be used as the link text when appearing "
"in menus."
)


class SiteSwitchForm(forms.Form):
site = forms.ChoiceField(choices=[])

class Media:
js = [
'wagtailmenus/js/site-switcher.js',
]

def __init__(self, current_site, edit_url_name, **kwargs):
initial = {'site': reverse(edit_url_name, args=(quote(current_site.pk),))}
super().__init__(initial=initial, **kwargs)
sites = []
for site in Site.objects.all():
sites.append((reverse(edit_url_name, args=(quote(site.pk),)), site))
self.fields['site'].choices = sites
Loading

0 comments on commit 84b1ee6

Please sign in to comment.