Skip to content

Conversation

@fsbraun
Copy link
Member

@fsbraun fsbraun commented Oct 24, 2025

Bug Fixes:

  • Check for the toolbar attribute before assigning preview_mode_active to avoid exceptions on requests without a toolbar

@metaforx This should now be safe for "non-CMS" api endpoints. Could you check?

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 24, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR adds a safeguard to the preview mode setter by checking for the existence of the request.toolbar attribute before setting preview_mode_active, preventing errors on requests without a toolbar.

Class diagram for updated preview mode setter in views_base.py

classDiagram
    class Request {
        toolbar
    }
    class Toolbar {
        preview_mode_active
    }
    Request --> Toolbar : has

    class ViewsBase {
        _preview_requested()
        _preview_requested() checks for toolbar before setting preview_mode_active
    }
    ViewsBase --> Request : uses
Loading

File-Level Changes

Change Details Files
Guard toolbar attribute existence before enabling preview mode
  • Wrap the assignment to preview_mode_active in an hasattr(self.request, 'toolbar') check
  • Preserve existing return logic for preview_mode
djangocms_rest/views_base.py

Possibly linked issues

  • #Menu does not return preview content: The PR adds a check for the toolbar attribute before setting preview mode, which directly addresses why the menu does not return preview content when the preview flag is enabled.

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
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 and they look great!


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.

@codecov
Copy link

codecov bot commented Oct 24, 2025

Codecov Report

❌ Patch coverage is 60.71429% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.81%. Comparing base (68036fc) to head (52969c2).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
djangocms_rest/serializers/menus.py 33.33% 3 Missing and 1 partial ⚠️
djangocms_rest/serializers/pages.py 66.66% 1 Missing and 1 partial ⚠️
djangocms_rest/serializers/placeholders.py 71.42% 1 Missing and 1 partial ⚠️
djangocms_rest/views_base.py 75.00% 1 Missing and 1 partial ⚠️
djangocms_rest/views.py 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #71      +/-   ##
==========================================
- Coverage   91.78%   90.81%   -0.98%     
==========================================
  Files          18       18              
  Lines         828      849      +21     
  Branches       89       98       +9     
==========================================
+ Hits          760      771      +11     
- Misses         42       48       +6     
- Partials       26       30       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@metaforx
Copy link
Collaborator

metaforx commented Oct 24, 2025

@fsbraun: This is not breaking Swagger, nice; I can reach http://localhost:8080/api/docs/. But preview is not working compared to pages. Is it cached?

curl -X 'GET' \
  'http://localhost:8080/api/cms/de/pages/?preview=true' \
  -H 'accept: application/json'
  
{
  "title": "Home (Draft)",
  "page_title": "Home (Draft)",
  "menu_title": "Home (Draft)",

curl -X 'GET' \
  'http://localhost:8080/api/cms/de/menu/?preview=true' \
  -H 'accept: application/json'
  
  
Response bodyDownload[   {     "namespace": "CMSMenu",     "title": "Home",     "url": "http://localhost:8080/",     "api_endpoint": "http://localhost:8080/api/cms/de/pages/",
--

pages correctly fetches preview:
"page_title": "Home (Draft)",

@fsbraun
Copy link
Member Author

fsbraun commented Oct 24, 2025

@metaforx
The menu is always cached. The cache is cleared if you change a page content object through the admin (but not if you directly work on the db).

I assume in your example you have added a (Draft) to the page's title which was not reflected in the menu?

This did not happen when I tested now, but then again, I had made some changes to the code:

  • Actually create a missing Toolbar object if preview is asked for
  • Add the "?preview=1" to api endpoints in the serializers to actually be able to follow a menu link to its api endpoint in preview

@fsbraun fsbraun merged commit 6b3b2d6 into main Oct 25, 2025
36 of 38 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.

3 participants