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

Skip to content

Conversation

fsbraun
Copy link
Member

@fsbraun fsbraun commented Sep 10, 2025

Description

Related resources

Checklist

Summary by Sourcery

Enhance the copy subcommand to include URL records and ensure correct ordering when copying page languages

Enhancements:

  • Order pages by node path to guarantee parent pages are processed before children during language copying
  • Copy PageUrl entries for the new language with unique slug and path generation

Tests:

  • Assert that PageUrl objects are correctly duplicated for the target language

vasekch and others added 2 commits September 10, 2025 09:32
* 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]>
Copy link
Contributor

sourcery-ai bot commented Sep 10, 2025

🧙 Sourcery has finished reviewing your pull request!


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

👋 Hi there!

Please remember to MERGE COMMIT pull requests from develop or develop-4!

Do not SQUASH commits to preserve history for the changelog.

@fsbraun fsbraun changed the title Chore/bp4 7548 fix: copy lang management command - include PageUrl (#7548) Sep 10, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Extract the PageUrl copy logic into a shared helper or integrate it with Page.copy to avoid duplication and keep slug/path generation in one place.
  • Instead of using model_to_dict to clone PageUrl, explicitly select and set only the fields you need (e.g. slug, path) so you don’t inadvertently copy metadata or PKs.
  • Add a guard or fallback when a source‐language PageUrl is missing (rather than unconditionally calling .get()), to avoid raising DoesNotExist during the copy.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Extract the PageUrl copy logic into a shared helper or integrate it with Page.copy to avoid duplication and keep slug/path generation in one place.
- Instead of using model_to_dict to clone PageUrl, explicitly select and set only the fields you need (e.g. slug, path) so you don’t inadvertently copy metadata or PKs.
- Add a guard or fallback when a source‐language PageUrl is missing (rather than unconditionally calling `.get()`), to avoid raising DoesNotExist during the copy.

## Individual Comments

### Comment 1
<location> `cms/management/commands/subcommands/copy.py:115` </location>
<code_context>
+
+                    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:
</code_context>

<issue_to_address>
Consider handling database errors during PageUrl creation.

Currently, any database error during PageUrl creation will stop execution. Consider catching exceptions to log errors or allow processing to continue for other pages, based on the desired behavior.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
                    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:
=======
                    import logging
                    from django.db import DatabaseError

                    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"]
                    try:
                        PageUrl.objects.with_user(user).create(**new_url)
                    except DatabaseError as e:
                        logging.error("Failed to create PageUrl for slug '%s': %s", new_url["slug"], str(e))
                        # Optionally, you could add more error handling here (e.g., collect failed slugs, notify user, etc.)

                if copy_content:
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +113 to 117
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 link
Contributor

Choose a reason for hiding this comment

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

suggestion: Consider handling database errors during PageUrl creation.

Currently, any database error during PageUrl creation will stop execution. Consider catching exceptions to log errors or allow processing to continue for other pages, based on the desired behavior.

Suggested change
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:
import logging
from django.db import DatabaseError
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"]
try:
PageUrl.objects.with_user(user).create(**new_url)
except DatabaseError as e:
logging.error("Failed to create PageUrl for slug '%s': %s", new_url["slug"], str(e))
# Optionally, you could add more error handling here (e.g., collect failed slugs, notify user, etc.)
if copy_content:

@fsbraun fsbraun merged commit aa25276 into release/4.1.x Sep 10, 2025
96 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants