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

Fix: Bad Title.path in Multilanguage sites if parent slug is created or modified #6968

Merged
merged 12 commits into from
Feb 6, 2021
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
Changelog
=========

fp-6622 (2021-01-19)
====================

* Fixed 66622 bad Title.path in multilingual sites when parent slug is created or modified
fp4code marked this conversation as resolved.
Show resolved Hide resolved


Unreleased
==================

* Fixed builds on RTD

fp4code marked this conversation as resolved.
Show resolved Hide resolved

3.8.0 (2020-10-28)
==================

Expand Down
5 changes: 3 additions & 2 deletions cms/admin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,9 @@ def save(self, commit=True):

if update_count == 0:
api.create_title(language=self._language, page=cms_page, **translation_data)
else:
cms_page._update_title_path_recursive(self._language)
# _update_title_path_recursive should be called if the new page is the parent
# of already created children in multilingual sites.
cms_page._update_title_path_recursive(self._language, slug=self.data['slug'])
cms_page.clear_cache(menu=True)
return cms_page

Expand Down
7 changes: 5 additions & 2 deletions cms/models/pagemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,18 @@ def _update_title_path(self, language):
title_obj.path = title_obj.get_path_for_base(base)
title_obj.save()

def _update_title_path_recursive(self, language):
def _update_title_path_recursive(self, language, slug=None):
assert self.publisher_is_draft
from cms.models import Title

if self.node.is_leaf() or language not in self.get_languages():
return

pages = self.get_child_pages()
base = self.get_path(language, fallback=True)
if slug:
base = self.get_path_for_slug(slug, language)
else:
base = self.get_path(language, fallback=True)

if base:
new_path = Concat(models.Value(base), models.Value('/'), models.F('slug'))
Expand Down
62 changes: 62 additions & 0 deletions cms/tests/test_titlepath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
from cms.api import create_page, create_title, publish_pages
from cms.test_utils.testcases import (CMSTestCase)

fp4code marked this conversation as resolved.
Show resolved Hide resolved

class TitlePathTestCase(CMSTestCase):

def test0001_translated_subpage_title_path(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pleae name the tests consistently with the other tests in the project. Maybe: "test_translated_subpage_slug_generation_for_pages_with_different_language_translations"

Copy link
Author

@fp4code fp4code Jan 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New name is:
"test_translated_subpage_title_path_regeneration"

"""
Test the path when parent page is created after child translation
Aiky30 marked this conversation as resolved.
Show resolved Hide resolved
"""

p_1 = create_page('en-1', "nav_playground.html", 'en',
fp4code marked this conversation as resolved.
Show resolved Hide resolved
slug = 'en-1', published=True)
p_1_1 = create_page('en-1-1', "nav_playground.html", 'en',
slug = 'en-1-1', parent=p_1, published=True)

create_title('de', 'de-1-1', p_1_1, slug='de-1-1')

# Parent 'de' title created after de-1-1 translation
create_title('de', 'de-1', p_1, slug='de-1')

p_1.publish('de')
p_1_1.publish('de')

response = self.client.get('/de/de-1/')
self.assertEqual(response.status_code, 200)

response = self.client.get('/de/en-1/de-1-1/')
if response.status_code == 200:
print('\n********** Unexpected response 200 for /de/en-1/de-1-1/')
Aiky30 marked this conversation as resolved.
Show resolved Hide resolved
response = self.client.get('/de/de-1/de-1-1/')
self.assertEqual(response.status_code, 200)


def test0002_changed_parent_slug(self):
Aiky30 marked this conversation as resolved.
Show resolved Hide resolved
"""
Test the child path when parent page slug is changed
"""

p_1 = create_page('BadFoo', "nav_playground.html", 'en',
slug = 'badfoo', published=True)
p_1_1 = create_page('Bar', "nav_playground.html", 'en',
slug = 'bar', parent=p_1, published=True)
t_1 = p_1.get_title_obj(language='en', fallback=False)
t_1.title='Foo'
t_1.path='foo'
t_1.save()

p_1.publish('en')
p_1_1.publish('en')

response = self.client.get('/en/foo/')
self.assertEqual(response.status_code, 200)

response = self.client.get('/en/badfoo/bar/')
if response.status_code == 200:
print('\n********** Unexpected response 200 for /en/badfoo/bar/')
response = self.client.get('/en/foo/bar/')
self.assertEqual(response.status_code, 200)