Skip to content

Commit

Permalink
Fix: Bad Title.path in Multilanguage sites if parent slug is created …
Browse files Browse the repository at this point in the history
…or modified (#6968)
  • Loading branch information
fp4code committed Feb 6, 2021
1 parent ee3af05 commit 6e7b0ae
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Unreleased

* Fixed builds on RTD
* Enforce use of coverage > 4 for python 3.8 support
* Fixed 66622 bad Title.path in multilingual sites when parent slug is created or modified

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
50 changes: 50 additions & 0 deletions cms/tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,56 @@ def test_page_preview_persists(self):
self.assertContains(resp, public_text)
self.assertNotContains(resp, draft_text)

def test_translated_subpage_title_path_regeneration(self):
"""
When a child page is created with multiple translations before parent translation,
child title translation path should be regenerated to take into account parent path.
This test enforces the issues found in: https://github.com/django-cms/django-cms/issues/6622,
where the slug was not regenerated.
"""
parent = create_page('en-parent', "nav_playground.html", 'en',
slug = 'en-parent', published=True)
child = create_page('en-child', "nav_playground.html", 'en',
slug = 'en-child', parent=parent, published=True)

create_title('de', 'de-child', child, slug='de-child')

# Parent 'de' title created after child translation
create_title('de', 'de-parent', parent, slug='de-parent')
parent._update_title_path_recursive('de', slug='de-parent')
parent.clear_cache(menu=True)

parent.publish('de')
child.publish('de')

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

def test_subpage_title_path_regeneration_after_parent_slug_change(self):
"""
When a parent page slug changes,
the child title path should be regenerated.
This test enforces the issues found in: https://github.com/django-cms/django-cms/issues/6622,
where the slug was not regenerated.
"""
parent = create_page('BadFoo', "nav_playground.html", 'en',
slug = 'badfoo', published=True)
child = create_page('Bar', "nav_playground.html", 'en',
slug = 'bar', parent=parent, published=True)
title = parent.get_title_obj(language='en', fallback=False)
title.title='Foo'
title.save()
parent._update_title_path_recursive('en', slug='foo')
parent.clear_cache(menu=True)

parent.publish('en')
child.publish('en')

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


class PageTreeTests(CMSTestCase):

Expand Down

0 comments on commit 6e7b0ae

Please sign in to comment.