Skip to content

Commit

Permalink
add djangocms 4.1 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
FreemanPancake committed May 28, 2024
1 parent 8e5c98a commit 72b02a4
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 56 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ History
2.1.0 (Unreleased)
==================

Features
--------

- Add support for CMS 4.1.x


Features
--------

Expand Down
10 changes: 10 additions & 0 deletions djangocms_page_sitemap/compat.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 8 additions & 8 deletions djangocms_page_sitemap/sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,35 @@ 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
else:
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
else:
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

Expand Down
40 changes: 6 additions & 34 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -30,46 +32,16 @@ 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)
page_3 = create_page("page three", "page.html", language="en", created_by=self.user)
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)
Expand Down
1 change: 1 addition & 0 deletions tests/requirements/dj32_cms40.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

Django>=3.2,<4

https://github.com/django-cms/django-cms/tarball/release/4.0.1.x#egg=django-cms
1 change: 1 addition & 0 deletions tests/requirements/dj42_cms40.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

Django>=4.2,<5

https://github.com/django-cms/django-cms/tarball/release/4.0.1.x#egg=django-cms
5 changes: 5 additions & 0 deletions tests/requirements/dj42_cms41.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions tests/requirements/requirements_base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 9 additions & 8 deletions tests/test_sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = (
"<url><loc>http://example.com/%s/</loc><lastmod>%s</lastmod><changefreq>"
"monthly</changefreq><priority>0.5</priority></url>" % (title_language, now().strftime("%Y-%m-%d"))
"monthly</changefreq><priority>0.5</priority></url>" % (page_content_language, now().strftime("%Y-%m-%d"))
)
self.assertContains(sitemap, test_string)

Expand All @@ -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 = (
"<url><loc>http://example.com/%s/</loc><lastmod>%s</lastmod><changefreq>"
"never</changefreq><priority>0.2</priority></url>" % (title_language, now().strftime("%Y-%m-%d"))
"never</changefreq><priority>0.2</priority></url>" % (page_content_language, now().strftime("%Y-%m-%d"))
)
sitemap = self.client.get("/sitemap.xml")
self.assertContains(sitemap, test_string)
Expand Down Expand Up @@ -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 = "<lastmod>%s</lastmod>" % (page_content.versions.first().modified.strftime("%Y-%m-%d"))
Expand Down
3 changes: 2 additions & 1 deletion tests/test_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
8 changes: 5 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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_*
Expand Down

0 comments on commit 72b02a4

Please sign in to comment.