Skip to content

fix: Slug uniqueness not checked when moving page #8185

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

amandasavluchinske
Copy link
Contributor

@amandasavluchinske amandasavluchinske commented Mar 23, 2025

Description

Closes #6840

Implements slug checking when a page is moved so that there are never two pages at the same level with the same slug.

Checklist

  • I have opened this pull request against main
  • I have added or modified the tests when changing logic
  • I have followed the conventional commits guidelines to add meaningful information into the changelog
  • I have read the contribution guidelines and I have joined the channel #pr-reviews on our Discord Server to find a “pr review buddy” who is going to review my pull request.

Summary by Sourcery

Bug Fixes:

  • Ensure slug uniqueness when moving pages to prevent duplicate slugs at the same level.

Copy link
Contributor

sourcery-ai bot commented Mar 23, 2025

Reviewer's Guide by Sourcery

This pull request fixes a bug where slug uniqueness was not checked when moving pages in the Django CMS admin interface. It implements slug validation in the MovePageForm to prevent creating pages with duplicate slugs at the same level. The pull request also includes refactoring of the MovePageForm and new tests to ensure the validation works correctly.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Implemented slug uniqueness validation when moving pages in the admin interface.
  • Added validation in the MovePageForm to prevent creating pages with duplicate slugs at the same level.
  • The validation checks for slug collisions when moving a page to a new parent or as a sibling.
  • The form now raises a validation error if moving a page would result in a duplicate slug.
  • Added tests for the MovePageForm to verify the slug uniqueness validation and other form behaviors.
cms/admin/pageadmin.py
cms/tests/test_forms.py
cms/admin/forms.py
Refactored the MovePageForm to improve readability and maintainability.
  • Extracted the logic for determining the target page and position into a separate get_tree_options method.
  • Improved the error message when moving the homepage inside another page.
  • Added tests for different position options in MovePageForm.
cms/tests/test_forms.py
cms/admin/forms.py

Possibly linked issues


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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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
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 @amandasavluchinske - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a test case that specifically checks the scenario where moving a page would result in a slug collision.
  • The MovePageForm's clean method could be simplified by extracting the slug uniqueness check into a separate, reusable function.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

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.

@fsbraun
Copy link
Member

fsbraun commented Mar 25, 2025

@amandasavluchinske What do you think about the edge case moving a page next to itself? Can (or have) we cover this?

Also, what do you think about sourcery's idea of factoring out the uniqueness check?

@amandasavluchinske amandasavluchinske force-pushed the fix-slug-uniqueness-when-moving-pages branch from 7198e2d to b2e7cc3 Compare March 26, 2025 09:46
@amandasavluchinske
Copy link
Contributor Author

Thanks for the suggestions @fsbraun ! The changes have been pushed :)

@jrief
Copy link
Contributor

jrief commented Mar 27, 2025

In our private version of django-CMS we actually use these constraints to prevent inconsistencies while moving pages, etc.:

https://github.com/jrief/django-cms/blob/a54ec3d02d80064b64e6d22da45512ad13211f5d/cms/models/pagemodel.py#L1092

@amandasavluchinske amandasavluchinske force-pushed the fix-slug-uniqueness-when-moving-pages branch from ed4fa02 to 16824f6 Compare April 13, 2025 10:56
Copy link
Member

@fsbraun fsbraun left a comment

Choose a reason for hiding this comment

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

@amandasavluchinske I have not been able to find out what makes the tests fail on mysql. I'm still on it. In the meantime I have two questions.

@amandasavluchinske
Copy link
Contributor Author

I have not been able to find out what makes the tests fail on mysql.

Thanks @fsbraun , I'll reply to your comments as soon as I can, but in the meantime: I looked the error up, and it seems like it's related to the condition= in line 1117 of pagemodel.py, which is not supported by MySQL.

@fsbraun
Copy link
Member

fsbraun commented Apr 15, 2025

That seems reasonable. But I still try to wrap my head around why we need it.

@amandasavluchinske
Copy link
Contributor Author

That seems reasonable. But I still try to wrap my head around why we need it.

Yes, I'll review everything once I get some time.

@amandasavluchinske
Copy link
Contributor Author

Hi @fsbraun ! I performed the requested changes. The condition wasn't needed after all. I tested manually and everything seems to be working as expected, but it would be good to have another pair of eyes on it as well. Thanks!

Copy link
Member

@fsbraun fsbraun left a comment

Choose a reason for hiding this comment

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

@amandasavluchinske I feel this is really getting somewhere! Sorry for the bickering, but I have two additional comments (see below) which could save us from having to duplicate the site field and might improve DRY. What do you think?

cms/api.py Outdated
# 3. The matching path is not the child of our page
if existing_url_collision.exists():
# For parent pages, the collision is not a problem if it's their child
if page.parent_id is not None:
Copy link
Member

Choose a reason for hiding this comment

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

I am wondering if we cen reduce the amount of repeated code if we use the validator here?


for lang in languages_to_check:
# Check if the new path exists for this language on another page on the same site
existing_url_collision = (
Copy link
Member

Choose a reason for hiding this comment

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

Again I am wondering if the check can be done in the validator to improve DRY?

Copy link
Member

@fsbraun fsbraun left a comment

Choose a reason for hiding this comment

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

Great work! Thank you, @amandasavluchinske !

@fsbraun fsbraun merged commit 8d50660 into django-cms:develop-4 Apr 28, 2025
63 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.

Slug uniqueness not checked when moving page
4 participants