Skip to content

fix: Detect page when getting toolbar for endpoint (#8137) #8138

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

Merged
merged 1 commit into from
Feb 10, 2025

Conversation

fsbraun
Copy link
Member

@fsbraun fsbraun commented Feb 10, 2025

Description

Backport of #8137

Related resources

Checklist

  • I have opened this pull request against develop-4
  • 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:

  • Fixed an issue where the toolbar was not correctly detecting the current page when accessed from the edit plugin endpoint.

Copy link
Contributor

sourcery-ai bot commented Feb 10, 2025

Reviewer's Guide by Sourcery

This pull request backports a fix to properly detect the page when obtaining the toolbar for an endpoint. The changes revolve around enhancing the logic for identifying the correct page from the request or the attached object, particularly handling instances where the attached object is of type PageContent, and adding tests to ensure the toolbar renders the page menu correctly. Minor formatting changes (e.g., switching from single to double quotes) were also applied in test files.

Sequence diagram for detecting page in toolbar retrieval

sequenceDiagram
    participant Req as request
    participant Settings as get_toolbar()
    participant PC_Check as PageContent check
    participant PageProvider as get_page_from_request()

    Req->>Settings: Call get_toolbar(request)
    Settings->>Settings: Extract cms_path and origin_url
    Settings->>PC_Check: Check if attached_obj is instance of PageContent
    alt attached_obj is PageContent
        PC_Check-->>Settings: True, return attached_obj.page
    else not PageContent
        Settings->>PageProvider: get_page_from_request(request, origin_url.path)
        PageProvider-->>Settings: Return current_page
    end
    Settings->>Req: Continue processing with current_page
Loading

Sequence diagram for admin form cleaning with PageContent

sequenceDiagram
    participant Req as Request
    participant Form as clean()
    participant TypeCheck as ModelClass check
    participant AdminMgr as admin_manager
    participant DefaultGetter as model_class.objects

    Req->>Form: Call clean() with obj_id
    Form->>TypeCheck: Check if model_class is subclass of PageContent
    alt model_class is PageContent
        TypeCheck-->>Form: Yes
        Form->>AdminMgr: Use admin_manager.select_related("page").get(pk=obj_id)
        AdminMgr-->>Form: Return generic_obj
    else if admin_manager exists
        TypeCheck-->>Form: No, but admin_manager exists
        Form->>AdminMgr: Use admin_manager.get(pk=obj_id)
        AdminMgr-->>Form: Return generic_obj
    else
        TypeCheck-->>Form: No admin_manager
        Form->>DefaultGetter: Call objects.get(pk=obj_id)
        DefaultGetter-->>Form: Return generic_obj
    end
    Form->>Req: Return generic_obj
Loading

File-Level Changes

Change Details Files
Enhanced page detection logic in the toolbar endpoint.
  • Updated the get_toolbar method to check if the attached object is an instance of PageContent and, if so, to set the current page from the attached object's page attribute.
  • Retained the previous logic of obtaining the page from the request path if the attached object is not a PageContent instance.
cms/admin/settingsadmin.py
Refined object retrieval in form cleaning for PageContent.
  • Modified the clean method in forms to first check if the model class is a subclass of PageContent.
  • Used the admin_manager with select_related('page') when processing PageContent to ensure the related page is fetched.
cms/admin/forms.py
Testing and formatting improvements in placeholder administration tests.
  • Replaced single-quoted strings with double-quoted strings for consistency.
  • Added a new test (test_get_toolbar_endpoint) to verify that the toolbar endpoint correctly returns the toolbar including the page menu when called from the edit endpoint.
  • Ensured that endpoints for adding, copying, editing, and deleting plugins in placeholders are functioning properly.
cms/tests/test_placeholder_admin.py

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

👋 Hi there!

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

Do not SQUASH commits to preserve history for the changelog.

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

Overall Comments:

  • Consider adding a comment to explain why the attached_obj check was removed in cms/admin/settingsadmin.py.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 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 fsbraun merged commit be69b30 into release/4.1.x Feb 10, 2025
95 checks passed
@fsbraun fsbraun deleted the chore/backport-8137 branch April 5, 2025 06:04
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.

1 participant