diff --git a/djangocms_redirect/admin.py b/djangocms_redirect/admin.py index da7028e..2781b38 100644 --- a/djangocms_redirect/admin.py +++ b/djangocms_redirect/admin.py @@ -3,6 +3,7 @@ from django.forms import ModelForm from django.utils.translation import get_language +from .compat import CMS_LT_4 from .models import Redirect from .utils import normalize_url @@ -14,7 +15,13 @@ class Meta: def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - widget = PageSmartLinkWidget(ajax_view="admin:cms_page_get_published_pagelist") + + pagelist_endpoint = "admin:cms_page_get_published_pagelist" + # django-cms 4+ support + if not CMS_LT_4: + pagelist_endpoint = "admin:cms_page_get_list" + + widget = PageSmartLinkWidget(ajax_view=pagelist_endpoint) widget.language = get_language() self.fields["old_path"].widget = widget self.fields["new_path"].widget = widget diff --git a/djangocms_redirect/compat.py b/djangocms_redirect/compat.py new file mode 100644 index 0000000..f68db32 --- /dev/null +++ b/djangocms_redirect/compat.py @@ -0,0 +1,7 @@ +from distutils.version import LooseVersion + +import cms + +CMS_VERSION = cms.__version__ + +CMS_LT_4 = LooseVersion(CMS_VERSION) < LooseVersion("4.0") diff --git a/tests/tests_admin.py b/tests/tests_admin.py index a65f11a..611e848 100644 --- a/tests/tests_admin.py +++ b/tests/tests_admin.py @@ -1,8 +1,11 @@ +from unittest import skipIf + from cms.forms.widgets import PageSmartLinkWidget from django.contrib import admin from django.urls import reverse from django.utils.translation import activate +from djangocms_redirect.compat import CMS_LT_4 from djangocms_redirect.models import Redirect from . import BaseRedirectTest @@ -11,6 +14,7 @@ class AdminPageTest(BaseRedirectTest): + @skipIf(not CMS_LT_4, "Test relevant only for CMS<4.0") def test_get_form_widgets(self): """ Admin form has proper widgets. @@ -26,3 +30,20 @@ def test_get_form_widgets(self): self.assertEqual(form.fields["old_path"].widget.ajax_url, reverse("admin:cms_page_get_published_pagelist")) self.assertEqual(form.fields["new_path"].widget.ajax_url, reverse("admin:cms_page_get_published_pagelist")) activate("en") + + @skipIf(CMS_LT_4, "Test relevant only for CMS>=4.0") + def test_get_form_widgets_djangocms_4_plus(self): + """ + Admin form has proper widgets. + """ + activate("it") + request = self.request("/", lang="it", user=self.user) + form_class = redirect_admin.get_form(request) + form = form_class() + self.assertTrue(form.fields["old_path"].widget, PageSmartLinkWidget) + self.assertTrue(form.fields["new_path"].widget, PageSmartLinkWidget) + self.assertEqual(form.fields["old_path"].widget.language, "it") + self.assertEqual(form.fields["new_path"].widget.language, "it") + self.assertEqual(form.fields["old_path"].widget.ajax_url, reverse("admin:cms_page_get_list")) + self.assertEqual(form.fields["new_path"].widget.ajax_url, reverse("admin:cms_page_get_list")) + activate("en")