Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added initial django-cms 4.0.x compatibility #41

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion djangocms_redirect/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions djangocms_redirect/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from distutils.version import LooseVersion

import cms

CMS_VERSION = cms.__version__

CMS_LT_4 = LooseVersion(CMS_VERSION) < LooseVersion("4.0")
21 changes: 21 additions & 0 deletions tests/tests_admin.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand All @@ -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")