From 72b02a438b1a7f94b418575d308bc2b9e078a42d Mon Sep 17 00:00:00 2001 From: "Freeman,Yu" Date: Tue, 28 May 2024 11:19:33 +0800 Subject: [PATCH] add djangocms 4.1 support. --- HISTORY.rst | 6 ++++ djangocms_page_sitemap/compat.py | 10 ++++++ djangocms_page_sitemap/sitemap.py | 16 +++++----- tests/base.py | 40 ++++-------------------- tests/requirements/dj32_cms40.txt | 1 + tests/requirements/dj42_cms40.txt | 1 + tests/requirements/dj42_cms41.txt | 5 +++ tests/requirements/requirements_base.txt | 2 -- tests/test_sitemap.py | 17 +++++----- tests/test_toolbar.py | 3 +- tox.ini | 8 +++-- 11 files changed, 53 insertions(+), 56 deletions(-) create mode 100644 djangocms_page_sitemap/compat.py create mode 100644 tests/requirements/dj42_cms41.txt diff --git a/HISTORY.rst b/HISTORY.rst index d254577..9bb7c55 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -10,6 +10,12 @@ History 2.1.0 (Unreleased) ================== +Features +-------- + +- Add support for CMS 4.1.x + + Features -------- diff --git a/djangocms_page_sitemap/compat.py b/djangocms_page_sitemap/compat.py new file mode 100644 index 0000000..17a9a4c --- /dev/null +++ b/djangocms_page_sitemap/compat.py @@ -0,0 +1,10 @@ +from cms import __version__ as CMS_VERSION +from packaging.version import Version + +CMS_41 = Version('4.1') <= Version(CMS_VERSION) + + +if CMS_41: + from cms.api import create_page_content # noqa: F401 +else: + from cms.api import create_title as create_page_content # noqa: F401 diff --git a/djangocms_page_sitemap/sitemap.py b/djangocms_page_sitemap/sitemap.py index 676459b..4fe68aa 100644 --- a/djangocms_page_sitemap/sitemap.py +++ b/djangocms_page_sitemap/sitemap.py @@ -50,8 +50,8 @@ def items(self): except ImportError: return super().items().exclude(page__pagesitemapproperties__include_in_sitemap=False) - def priority(self, title): - ext_key = get_cache_key(title.page) + def priority(self, page_url): + ext_key = get_cache_key(page_url.page) properties = cache.get(ext_key) if properties: return properties.priority @@ -59,15 +59,15 @@ def priority(self, title): try: cache.set( ext_key, - title.page.pagesitemapproperties, + page_url.page.pagesitemapproperties, PAGE_SITEMAP_CACHE_DURATION, ) - return title.page.pagesitemapproperties.priority + return page_url.page.pagesitemapproperties.priority except PageSitemapProperties.DoesNotExist: return self.default_priority - def changefreq(self, title): - ext_key = get_cache_key(title.page) + def changefreq(self, page_url): + ext_key = get_cache_key(page_url.page) properties = cache.get(ext_key) if properties: # pragma: no cover return properties.changefreq @@ -75,10 +75,10 @@ def changefreq(self, title): try: cache.set( ext_key, - title.page.pagesitemapproperties, + page_url.page.pagesitemapproperties, PAGE_SITEMAP_CACHE_DURATION, ) - return title.page.pagesitemapproperties.changefreq + return page_url.page.pagesitemapproperties.changefreq except PageSitemapProperties.DoesNotExist: return self.default_changefreq diff --git a/tests/base.py b/tests/base.py index 4611dcf..3adbb24 100644 --- a/tests/base.py +++ b/tests/base.py @@ -1,11 +1,13 @@ from io import StringIO -from cms.api import create_page, create_title +from cms.api import create_page +from cms.models import PageContent from cms.utils.i18n import get_language_list from django.contrib.auth.models import User from django.http import HttpResponse, SimpleCookie from django.test import RequestFactory, TestCase +from djangocms_page_sitemap.compat import create_page_content from djangocms_page_sitemap.utils import is_versioning_enabled @@ -30,36 +32,6 @@ def setUpClass(cls): cls.user_normal = User.objects.create(username="normal") def get_pages(self): - try: - from cms.models import PageContent # noqa: F401 - - return self._cms_4_pages() - except ImportError: - return self._cms_3_pages() - - def _cms_3_pages(self): - page_1 = create_page("page one", "page.html", language="en") - page_2 = create_page("page two", "page.html", language="en") - page_3 = create_page("page three", "page.html", language="en") - create_title(language="fr", title="page un", page=page_1) - create_title(language="it", title="pagina uno", page=page_1) - create_title(language="fr", title="page trois", page=page_3) - for lang in self.languages: - page_1.publish(lang) - page_2.publish("en") - page_3.publish("en") - page_3.publish("fr") - if hasattr(page_1, "set_as_homepage"): - page_1.set_as_homepage() - - return ( - page_1.get_draft_object(), - page_2.get_draft_object(), - page_3.get_draft_object(), - ) - - def _cms_4_pages(self): - from cms.models import PageContent # noqa: F401 page_1 = create_page("page one", "page.html", language="en", created_by=self.user) page_2 = create_page("page two", "page.html", language="en", created_by=self.user) @@ -67,9 +39,9 @@ def _cms_4_pages(self): page_content1 = PageContent._base_manager.get(page=page_1, language="en") page_content2 = PageContent._base_manager.get(page=page_2, language="en") page_content3 = PageContent._base_manager.get(page=page_3, language="en") - page_1_content_fr = create_title(language="fr", title="page un", page=page_1, created_by=self.user) - page_1_content_it = create_title(language="it", title="pagina uno", page=page_1, created_by=self.user) - page_3_content_fr = create_title(language="fr", title="page trois", page=page_3, created_by=self.user) + page_1_content_fr = create_page_content(language="fr", title="page un", page=page_1, created_by=self.user) + page_1_content_it = create_page_content(language="it", title="pagina uno", page=page_1, created_by=self.user) + page_3_content_fr = create_page_content(language="fr", title="page trois", page=page_3, created_by=self.user) if is_versioning_enabled(): page_content1.versions.first().publish(self.user) page_content2.versions.first().publish(self.user) diff --git a/tests/requirements/dj32_cms40.txt b/tests/requirements/dj32_cms40.txt index 80e163e..d60bfad 100644 --- a/tests/requirements/dj32_cms40.txt +++ b/tests/requirements/dj32_cms40.txt @@ -2,3 +2,4 @@ Django>=3.2,<4 +https://github.com/django-cms/django-cms/tarball/release/4.0.1.x#egg=django-cms diff --git a/tests/requirements/dj42_cms40.txt b/tests/requirements/dj42_cms40.txt index 9107cb2..64a91e0 100644 --- a/tests/requirements/dj42_cms40.txt +++ b/tests/requirements/dj42_cms40.txt @@ -2,3 +2,4 @@ Django>=4.2,<5 +https://github.com/django-cms/django-cms/tarball/release/4.0.1.x#egg=django-cms diff --git a/tests/requirements/dj42_cms41.txt b/tests/requirements/dj42_cms41.txt new file mode 100644 index 0000000..41ab8e9 --- /dev/null +++ b/tests/requirements/dj42_cms41.txt @@ -0,0 +1,5 @@ +-r requirements_base.txt + +Django>=4.2,<5 + +https://github.com/django-cms/django-cms/tarball/release/4.1.x#egg=django-cms diff --git a/tests/requirements/requirements_base.txt b/tests/requirements/requirements_base.txt index 4ad6077..4f6ebe2 100644 --- a/tests/requirements/requirements_base.txt +++ b/tests/requirements/requirements_base.txt @@ -3,5 +3,3 @@ coverage>5 coveralls>2 mock>=1.0.1 django-app-helper>=2.0.0 - -https://github.com/django-cms/django-cms/tarball/release/4.0.1.x#egg=django-cms \ No newline at end of file diff --git a/tests/test_sitemap.py b/tests/test_sitemap.py index facd270..58f0bf8 100644 --- a/tests/test_sitemap.py +++ b/tests/test_sitemap.py @@ -2,11 +2,12 @@ from unittest import skipIf import cms -from cms.api import create_page, create_title +from cms.api import create_page from cms.test_utils.util.fuzzy_int import FuzzyInt from django.core.cache import cache from django.utils.timezone import now +from djangocms_page_sitemap.compat import CMS_41, create_page_content from djangocms_page_sitemap.models import PageSitemapProperties from djangocms_page_sitemap.sitemap import ExtendedSitemap from djangocms_page_sitemap.utils import get_cache_key, is_versioning_enabled @@ -19,14 +20,14 @@ def test_sitemap_base(self): page1, page2, page3 = self.get_pages() try: - title_language = page1.get_title_obj().language + page_content_language = page1.get_content_obj().language if CMS_41 else page1.get_title_obj().language except AttributeError: - title_language = page1.get_page_content_obj_attribute("language") + page_content_language = page1.get_page_content_obj_attribute("language") sitemap = self.client.get("/sitemap.xml") test_string = ( "http://example.com/%s/%s" - "monthly0.5" % (title_language, now().strftime("%Y-%m-%d")) + "monthly0.5" % (page_content_language, now().strftime("%Y-%m-%d")) ) self.assertContains(sitemap, test_string) @@ -37,12 +38,12 @@ def test_sitemap_extended(self): page1.publish("it") try: - title_language = page1.get_title_obj().language + page_content_language = page1.get_content_obj().language if CMS_41 else page1.get_title_obj().language except AttributeError: - title_language = page1.get_page_content_obj_attribute("language") + page_content_language = page1.get_page_content_obj_attribute("language") test_string = ( "http://example.com/%s/%s" - "never0.2" % (title_language, now().strftime("%Y-%m-%d")) + "never0.2" % (page_content_language, now().strftime("%Y-%m-%d")) ) sitemap = self.client.get("/sitemap.xml") self.assertContains(sitemap, test_string) @@ -104,7 +105,7 @@ def test_pageurl_lastmod_with_cms4_versioning(self): # Check the latest version modified date for the page is checked for lastmod() # if versioning is enabled, Currenly test is skipped , as this require changes in testsuite page_1 = create_page("page-one", "page.html", language="en", created_by=self.user) - page_content = create_title(title="page un", language="en", page=page_1, created_by=self.user) + page_content = create_page_content(title="page un", language="en", page=page_1, created_by=self.user) if is_versioning_enabled(): page_content.versions.first().publish(self.user) last_modified_date = "%s" % (page_content.versions.first().modified.strftime("%Y-%m-%d")) diff --git a/tests/test_toolbar.py b/tests/test_toolbar.py index bd5ad92..e37743f 100644 --- a/tests/test_toolbar.py +++ b/tests/test_toolbar.py @@ -26,7 +26,8 @@ def find_toolbar_buttons(button_name, toolbar): """ found = [] for button_list in toolbar.get_right_items(): - found = found + [button for button in button_list.buttons if button.name == button_name] + if hasattr(button_list, "buttons"): + found = found + [button for button in button_list.buttons if button.name == button_name] return found diff --git a/tox.ini b/tox.ini index 9d7592a..d88b034 100644 --- a/tox.ini +++ b/tox.ini @@ -8,18 +8,20 @@ envlist = ruff pypi-description towncrier - py{39, 310, 311}-django{32,42}-cms{40} + py{38, 39, 310, 311}-django{32,42}-cms{40,41} [testenv] commands = {env:COMMAND:python} cms_helper.py djangocms_page_sitemap test {posargs} deps = -r{toxinidir}/tests/requirements/requirements_base.txt - django32: -r{toxinidir}/tests/requirements/dj32_cms40.txt - django42: -r{toxinidir}/tests/requirements/dj42_cms40.txt + django32_cms40: -r{toxinidir}/tests/requirements/dj32_cms40.txt + django42_cms40: -r{toxinidir}/tests/requirements/dj42_cms40.txt + django42_cms41: -r{toxinidir}/tests/requirements/dj42_cms41.txt basepython= py38: python3.8 py39: python3.9 py310: python3.10 + py311: python3.11 passenv = COMMAND PYTEST_*