-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix: copy lang management command - include PageUrl (#7548) #8335
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]>
Reviewer's GuideExtends the copy language management command to include PageUrl instances by importing necessary models/utilities, enforcing tree order, generating new slugs/paths, and adds tests to verify URL duplication. Sequence diagram for copying PageUrl in the copy language management commandsequenceDiagram
participant "Management Command"
participant "Page"
participant "PageUrl"
participant "PageContent"
participant "User"
participant "Site"
participant "ParentPage"
"Management Command"->>"Site": Get site
"Management Command"->>"Page": Iterate pages in tree order
"Management Command"->>"Page": Check if from_lang exists
alt If from_lang exists
"Management Command"->>"PageContent": Create new PageContent for to_lang
"Management Command"->>"Page": Update languages to include to_lang
"Management Command"->>"PageUrl": Get PageUrl for from_lang
"Management Command"->>"ParentPage": Get parent page (if any)
"Management Command"->>"PageUrl": Generate new slug and path for to_lang
"Management Command"->>"PageUrl": Create new PageUrl for to_lang
end
Entity relationship diagram for Page and PageUrl after copy command updateerDiagram
Page ||--o{ PageUrl : has
Page {
id int PK
parent_id int FK
path string
languages list
}
PageUrl {
id int PK
page_id int FK
language string
slug string
path string
}
Class diagram for updated Page and PageUrl handling in copy commandclassDiagram
class Page {
+get_languages()
+update_languages(langs)
+get_path(lang)
parent : Page
path : str
}
class PageUrl {
language : str
slug : str
path : str
}
class PageContent {
+create(**kwargs)
}
class User {
}
Page "1" -- "*" PageUrl : has
Page "1" -- "*" PageContent : has
Page "1" -- "0..1" Page : parent
PageUrl "*" -- "1" User : created_by
PageContent "*" -- "1" User : created_by
File-Level Changes
Possibly linked issues
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:
- Instead of ordering pages by
path
to enforce parent-first order, use the model’s MPTT attributes (e.g.tree_id
andlft
) for deterministic tree traversal. - Extract the PageUrl copy logic into a shared helper or into the Page model’s copy method to avoid duplication and improve maintainability.
- Iterate over all related PageUrl instances (
page.urls.all()
) so that any URL aliases are also copied, not just the primary URL.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Instead of ordering pages by `path` to enforce parent-first order, use the model’s MPTT attributes (e.g. `tree_id` and `lft`) for deterministic tree traversal.
- Extract the PageUrl copy logic into a shared helper or into the Page model’s copy method to avoid duplication and improve maintainability.
- Iterate over all related PageUrl instances (`page.urls.all()`) so that any URL aliases are also copied, not just the primary URL.
## Individual Comments
### Comment 1
<location> `cms/management/commands/subcommands/copy.py:98` </location>
<code_context>
+ 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.parent
+
+ new_url = model_to_dict(page_url)
</code_context>
<issue_to_address>
No error handling for missing PageUrl in source language.
Accessing page.urls.get(language=from_lang) without checking for existence can cause exceptions. Please add error handling to manage missing PageUrl cases.
</issue_to_address>
### Comment 2
<location> `cms/management/commands/subcommands/copy.py:114` </location>
<code_context>
+ 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)
+
</code_context>
<issue_to_address>
Path construction may not handle leading/trailing slashes or empty base correctly.
Normalize base and slug to prevent double slashes or malformed paths when concatenating.
Suggested implementation:
```python
base = parent_page.get_path(to_lang)
# Normalize base and slug to avoid double slashes
base_norm = base.strip('/') if base else ''
slug_norm = page_url.slug.strip('/')
path = '%s/%s' % (base_norm, slug_norm) if base_norm else slug_norm
else:
base_norm = ''
slug_norm = page_url.slug.strip('/')
path = slug_norm
new_url["slug"] = get_available_slug(site, path, to_lang)
```
```python
new_url["slug"] = get_available_slug(site, path, to_lang)
# Normalize base and slug for the final path as well
final_base = base.strip('/') if base else ''
final_slug = new_url["slug"].strip('/')
new_url["path"] = '%s/%s' % (final_base, final_slug) if final_base else final_slug
PageUrl.objects.with_user(user).create(**new_url)
```
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
page_url = page.urls.get(language=from_lang) | ||
parent_page = page.parent |
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.
issue: No error handling for missing PageUrl in source language.
Accessing page.urls.get(language=from_lang) without checking for existence can cause exceptions. Please add error handling to manage missing PageUrl cases.
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
Description
Related resources
Checklist
main
Summary by Sourcery
Extend the copy language management command to duplicate related PageUrl models, including slug and path generation, and ensure proper parent-child ordering.
Enhancements:
Tests: