Skip to content

fix: show_placeholder did not respect edit/preview mode and failed loudly #8272

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 9 commits into from
Jun 30, 2025

Conversation

fsbraun
Copy link
Member

@fsbraun fsbraun commented Jun 30, 2025

Description

This PR fixes #8271, by

  • letting the {% show_placeholder %} tag fail silently if the targeted page does not have a content object in the required language
  • respecting page content objects only visible in the admin in edit and preview mode

Related resources

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

Enable show_placeholder to respect edit/preview modes by including admin-only content and to handle missing content gracefully outside debug mode.

Bug Fixes:

  • Ensure show_placeholder silently returns an empty string in production mode when the requested page content or placeholder is missing.
  • Include page content objects only visible in the admin when rendering placeholders in edit and preview modes.

Enhancements:

  • Add an admin_manager parameter to Page.get_placeholders for fetching admin-managed content.
  • Remove redundant content permission checks from admin utils.

Tests:

  • Add tests for show_placeholder behavior in debug vs non-debug settings and for missing page content.

Copy link
Contributor

sourcery-ai bot commented Jun 30, 2025

Reviewer's Guide

This PR updates the show_placeholder tag to fail silently for missing page content in production and to respect admin-only content visibility in edit and preview modes by introducing an admin_manager flag in get_placeholders, adds tests for these behaviors, and cleans up obsolete admin utility code.

Sequence diagram for show_placeholder tag with admin/preview mode handling

sequenceDiagram
    participant User as actor User
    participant Template as Template Engine
    participant CMS as CMS Tags
    participant Page as Page Model
    participant PC as PageContent
    participant PH as Placeholder
    participant Toolbar as Toolbar

    User->>Template: Render template with {% show_placeholder %}
    Template->>CMS: _show_placeholder_by_id(...)
    CMS->>Toolbar: get_toolbar_from_request(request)
    CMS->>Toolbar: Check edit_mode_active/preview_mode_active
    CMS->>Page: get_placeholders(lang, admin_manager)
    alt admin_manager is True
        Page->>PC: PageContent.admin_manager.latest_content().get(...)
    else admin_manager is False
        Page->>PC: PageContent.objects.get(...)
    end
    Page->>PH: Placeholder.objects.get_for_obj(page_content)
    CMS->>Template: Return placeholder content or "" if not found
Loading

Class diagram for updated get_placeholders method in Page model

classDiagram
    class Page {
        +get_placeholders(language: str, admin_manager: bool = False) QuerySet
    }
    class PageContent {
        +admin_manager
        +objects
    }
    class Placeholder {
        +get_for_obj(obj)
    }
    Page --> PageContent : uses
    Page --> Placeholder : uses
    PageContent <|-- admin_manager
    PageContent <|-- objects
Loading

Class diagram for _show_placeholder_by_id logic update

classDiagram
    class CMS_Tags {
        +_show_placeholder_by_id(context, placeholder_name, reverse_id, lang=None, site=None)
    }
    class Toolbar {
        +edit_mode_active
        +preview_mode_active
    }
    CMS_Tags --> Toolbar : checks mode
    CMS_Tags --> Page : calls get_placeholders
    CMS_Tags --> Placeholder : gets placeholder
    CMS_Tags ..> settings : uses DEBUG
    CMS_Tags ..> PageContent : handles DoesNotExist exception
Loading

File-Level Changes

Change Details Files
Enhance show_placeholder tag to respect edit/preview modes and silent-fail in production
  • Determine admin_manager flag from toolbar.edit_mode_active or preview_mode_active
  • Pass admin_manager to get_placeholders call
  • Catch PageContent.DoesNotExist alongside PlaceholderModel.DoesNotExist and return "" when DEBUG is False
cms/templatetags/cms_tags.py
Extend get_placeholders API with admin_manager parameter
  • Add admin_manager boolean parameter with type annotation
  • Use PageContent.admin_manager.latest_content() branch when admin_manager is True
  • Fall back to default PageContent.objects.get when admin_manager is False
cms/models/pagemodel.py
Add tests covering fallback and exception paths of show_placeholder
  • Test that DEBUG=True raises PageContent.DoesNotExist for missing content
  • Test that DEBUG=False returns empty string instead of raising
  • Implement test_show_placeholder_for_page_content_does_not_exist in templatetags tests
cms/tests/test_templatetags.py
Remove dead can_change_content logic from admin form utilities
  • Delete conditional block disabling content fields based on can_change_content
  • Clean up related unused imports in admin/utils
cms/admin/utils.py

Assessment against linked issues

Issue Objective Addressed Explanation
#8271 Prevent PageContent.DoesNotExist exception when creating the first page with show_placeholder tag.
#8271 Ensure show_placeholder tag respects edit/preview mode.

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!

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 @fsbraun - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `cms/tests/test_templatetags.py:304` </location>
<code_context>
+            content = _show_placeholder_by_id(context, "does_not_exist", "myreverseid", lang="go-lang")
+            self.assertEqual(content, "")
+
+    def test_show_placeholder_for_page_content_does_not_exist(self):
+        """
+        Verify ``show_placeholder`` correctly handles being given an
+        invalid identifier.
+        """
</code_context>

<issue_to_address>
Potential duplicate or incomplete test for missing page content.

Please implement 'test_show_placeholder_for_page_content_does_not_exist' to cover missing page content in both DEBUG and non-DEBUG modes, ensuring the new error handling is tested.
</issue_to_address>

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 requested a review from vinitkumar June 30, 2025 09:37
Copy link
Member

@vinitkumar vinitkumar left a comment

Choose a reason for hiding this comment

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

LGTM

@fsbraun fsbraun merged commit b2c9b97 into main Jun 30, 2025
65 checks passed
@fsbraun fsbraun deleted the fix/show_placeholder branch June 30, 2025 10:04
fsbraun added a commit that referenced this pull request Jun 30, 2025
…udly (#8272)

* fix: show_placeholder tag

* Add test

* Use current_content

* fix: test names switched

* Better naming inside test

* fix: docs

* Update docs/requirements.txt

* Fix django_settings for docs
fsbraun added a commit that referenced this pull request Jun 30, 2025
…udly (#8272)

* fix: show_placeholder tag

* Add test

* Use current_content

* fix: test names switched

* Better naming inside test

* fix: docs

* Update docs/requirements.txt

* Fix django_settings for docs
fsbraun added a commit that referenced this pull request Jun 30, 2025
…udly (#8272) (#8274)

* fix: show_placeholder tag

* Add test

* Use current_content

* fix: test names switched

* Better naming inside test

* fix: docs

* Update docs/requirements.txt

* Fix django_settings for docs
fsbraun added a commit that referenced this pull request Jun 30, 2025
…udly (#8273)

* fix: show_placeholder did not respect edit/preview mode and failed loudly (#8272)

* fix: show_placeholder tag

* Add test

* Use current_content

* fix: test names switched

* Better naming inside test

* fix: docs

* Update docs/requirements.txt

* Fix django_settings for docs

* Update package.json
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.

[BUG] PageContent.DoesNotExist on first page via show_placeholder
2 participants