Skip to content

fix: copy lang management command - include PageUrl #7548

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
26 changes: 24 additions & 2 deletions cms/management/commands/subcommands/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

from cms.api import copy_plugins_to_language
from cms.management.commands.subcommands.base import SubcommandsCommand
from cms.models import EmptyPageContent, Page, PageContent, StaticPlaceholder
from cms.models import EmptyPageContent, Page, PageContent, PageUrl, StaticPlaceholder
from cms.utils import get_language_list
from cms.utils.page import get_available_slug
from cms.utils.plugins import copy_plugins_to_placeholder

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

for page in Page.objects.on_site(site):
# obey node path (tree order) to make sure parent records are created before children (for slug generation)
for page in Page.objects.on_site(site).order_by('node__path'):
# copy title
if from_lang in page.get_languages():

Expand All @@ -92,6 +94,26 @@ def handle(self, *args, **options):
if to_lang not in page.get_languages():
page.update_languages(page.get_languages() + [to_lang])

# copy PageUrls - inspired from pagemodels.Page.copy() - possibly refactorable
page_url = page.urls.get(language=from_lang)
parent_page = page.node.parent.cms_pages.first()

new_url = model_to_dict(page_url)
new_url.pop("id", None) # No PK
new_url["page"] = page
new_url["language"] = to_lang

if parent_page:
base = parent_page.get_path(to_lang)
path = '%s/%s' % (base, page_url.slug) if base else page_url.slug
else:
base = ''
path = page_url.slug

new_url["slug"] = get_available_slug(site, path, to_lang)
new_url["path"] = '%s/%s' % (base, new_url["slug"]) if base else new_url["slug"]
PageUrl.objects.with_user(user).create(**new_url)

if copy_content:
# copy plugins using API
if verbose:
Expand Down
Loading