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

add djangocms 4.1 support. #4

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
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
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ jobs:
continue-on-error: ${{ matrix.continue-on-error }}
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
django: [32, 42]
cms: [40]
cms: [40, 41]
requirements-file: [
dj32_cms40.txt,
dj42_cms40.txt,
dj42_cms41.txt,
]
continue-on-error: [true]
steps:
Expand All @@ -40,7 +41,7 @@ jobs:
- name: Install dependencies
run: |
python setup.py install
pip install -r tests/requirements/dj${{matrix.django}}_cms${{matrix.cms}}.txt
pip install -r tests/requirements/${{matrix.requirements-file}}
sudo apt-get install gettext
python -m pip install --upgrade pip setuptools tox>4
- name: Test with tox
Expand Down
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
2 changes: 2 additions & 0 deletions changes/41.feat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Add compatibility with DjangoCMS 4.1
* Drop compatibility with DjangoCMS < 4.0
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
9 changes: 6 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ envlist =
ruff
pypi-description
towncrier
py{39, 310, 311}-django{32,42}-cms{40}
py{38, 39, 310, 311}-django{32,42}-cms{40}
py{38, 39, 310, 311}-django{42}-cms{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
Loading