Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 446219c

Browse files
vasekchfsbraun
andauthored
fix: copy lang management command - include PageUrl (#7548)
* fix: copy lang management command Include copy of related PageUrl models * Update cms/management/commands/subcommands/copy.py * Apply suggestions from code review * Update cms/management/commands/subcommands/copy.py * Add test to check page urls are created * Add import for sys module in test_management.py --------- Co-authored-by: Fabian Braun <[email protected]>
1 parent d7bdc7e commit 446219c

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

cms/management/commands/subcommands/copy.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
from cms.api import copy_plugins_to_language
88
from cms.management.commands.subcommands.base import SubcommandsCommand
9-
from cms.models import EmptyPageContent, Page, PageContent
9+
from cms.models import EmptyPageContent, Page, PageContent, PageUrl
1010
from cms.utils import get_language_list
11+
from cms.utils.page import get_available_slug
1112
from cms.utils.plugins import copy_plugins_to_placeholder
1213

1314
User = get_user_model()
@@ -71,7 +72,8 @@ def handle(self, *args, **options):
7172
except AssertionError:
7273
raise CommandError('Both languages have to be present in settings.LANGUAGES and settings.CMS_LANGUAGES')
7374

74-
for page in Page.objects.on_site(site):
75+
# obey node path (tree order) to make sure parent records are created before children (for slug generation)
76+
for page in Page.objects.on_site(site).order_by('path'):
7577
# copy title
7678
if from_lang in page.get_languages():
7779

@@ -89,6 +91,29 @@ def handle(self, *args, **options):
8991
new_title["page"] = page
9092
PageContent.objects.with_user(user).create(**new_title)
9193

94+
if to_lang not in page.get_languages():
95+
page.update_languages(page.get_languages() + [to_lang])
96+
97+
# copy PageUrls - inspired from pagemodels.Page.copy() - possibly refactorable
98+
page_url = page.urls.get(language=from_lang)
99+
parent_page = page.parent
100+
101+
new_url = model_to_dict(page_url)
102+
new_url.pop("id", None) # No PK
103+
new_url["page"] = page
104+
new_url["language"] = to_lang
105+
106+
if parent_page:
107+
base = parent_page.get_path(to_lang)
108+
path = '%s/%s' % (base, page_url.slug) if base else page_url.slug
109+
else:
110+
base = ''
111+
path = page_url.slug
112+
113+
new_url["slug"] = get_available_slug(site, path, to_lang)
114+
new_url["path"] = '%s/%s' % (base, new_url["slug"]) if base else new_url["slug"]
115+
PageUrl.objects.with_user(user).create(**new_url)
116+
92117
if copy_content:
93118
# copy plugins using API
94119
if verbose:

cms/tests/test_management.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import os
2-
import shutil
32
import sys
4-
import tempfile
5-
import uuid
63
from io import StringIO
74

85
from django.conf import settings
@@ -15,7 +12,7 @@
1512

1613
from cms.api import add_plugin, create_page, create_page_content
1714
from cms.management.commands.subcommands.list import plugin_report
18-
from cms.models import Page
15+
from cms.models import Page, PageUrl
1916
from cms.models.placeholdermodel import Placeholder
2017
from cms.models.pluginmodel import CMSPlugin
2118
from cms.test_utils.fixtures.navextenders import NavextendersFixture
@@ -346,6 +343,7 @@ def test_copy_langs(self):
346343
"""
347344
site = 1
348345
number_start_plugins = CMSPlugin.objects.all().count()
346+
number_urls = PageUrl.objects.filter(language='en').count()
349347

350348
out = StringIO()
351349
management.call_command(
@@ -382,6 +380,8 @@ def test_copy_langs(self):
382380
self.assertEqual(stack_text_en.plugin_type, stack_text_de.plugin_type)
383381
self.assertEqual(stack_text_en.body, stack_text_de.body)
384382

383+
self.assertEqual(PageUrl.objects.filter(language='de').count(), number_urls)
384+
385385
def test_copy_langs_no_content(self):
386386
"""
387387
Various checks here:

0 commit comments

Comments
 (0)