-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix: copy lang management command - include PageUrl (#7548) #8336
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
Conversation
* 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]>
🧙 Sourcery has finished reviewing your pull request! Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
👋 Hi there! Please remember to MERGE COMMIT pull requests from Do not SQUASH commits to preserve history for the changelog. |
There was a problem hiding this 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>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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: |
There was a problem hiding this comment.
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.
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: |
Description
Related resources
Checklist
main
Summary by Sourcery
Enhance the copy subcommand to include URL records and ensure correct ordering when copying page languages
Enhancements:
Tests: