8000 fix: Allow lazy wizard initialization by fsbraun · Pull Request #8265 · django-cms/django-cms · GitHub
[go: up one dir, main page]

Skip to content

fix: Allow lazy wizard initialization #8265

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 3 commits into from
Jun 28, 2025
Merged

Conversation

fsbraun
Copy link
Member
@fsbraun fsbraun commented Jun 27, 2025

Description

This PR fixes two issues and a chore:

  1. Django 5.1 accessing the database during app initialization is discouraged. Since cms_config runs during app initialization, wizards needing, e.g., some config from the db, need to be able to initialize lazily.
  2. When on an app-hooked page, the wizards were not accessible (i.e, always disabled). This is particularly confusing, since the apphooked app might have registered its own wizard, e.g, djangocms-stories wizard should be available when viewing a post.
  3. Added RemovedInDjangoCMS51Warning to the legacy wizard pool (deprecated since Django CMS 4.0(!))

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 lazy wizard initialization in the CMS to avoid database access at startup, consolidate wizard registration into a cached property, add core helper functions in wizard_base, deprecate legacy helper and pool modules with warnings, and adjust toolbar logic and tests accordingly.

New Features:

  • Add get_entries, get_entry, and entry_choices functions to wizard_base for direct wizard access

Bug Fixes:

  • Defer wizard instantiation to avoid database access during app initialization
  • Restore wizard availability on app-hooked pages by simplifying toolbar disabling logic

Enhancements:

  • Implement lazy loading of CMS wizards via a cached_property in CMSCoreExtensions
  • Deprecate cms.wizards.helpers and WizardPool modules with warnings ahead of Django CMS 5.1

Tests:

  • Update tests for lazy wizard loading, teardown of cached wizards, and configuration error handling

Summary by Sourcery

Implement lazy initialization of CMS wizards to avoid database access at startup, add helper functions for wizard access, restore toolbar wizard availability on app-hooked pages, and deprecate legacy wizard helper and pool modules with warnings

New Features:

  • Add get_entries, get_entry, and entry_choices functions for direct and ordered access to registered wizards

Bug Fixes:

  • Defer wizard instantiation to enable lazy initialization and avoid database access during app startup
  • Restore wizard availability on app-hooked pages by simplifying toolbar disabling logic

Enhancements:

  • Implement lazy-loading of CMS wizards via a cached_property in CMSCoreExtensions
  • Deprecate cms.wizards.helpers and WizardPool modules with RemovedInDjangoCMS51Warning ahead of Django CMS 5.1

Tests:

  • Update tests for lazy wizard loading, cached property 8000 teardown, and configuration error handling

Copy link
Contributor
sourcery-ai bot commented Jun 27, 2025

Reviewer's Guide

This PR refactors wizard initialization to be lazy via a cached property in CMSCoreExtensions, centralizes and exposes core wizard helper functions in wizard_base, deprecates legacy helper and pool modules with warnings ahead of Django CMS 5.1, simplifies toolbar disabling logic to restore wizard availability on app-hooked pages, and updates tests to align with lazy loading and reset logic.

Sequence diagram for lazy wizard access via get_entries

sequenceDiagram
    participant User
    participant Toolbar
    participant CMSCoreExtensions
    participant Wizard
    User->>Toolbar: Request wizard actions
    Toolbar->>CMSCoreExtensions: Access wizards (via get_entries)
    CMSCoreExtensions->>CMSCoreExtensions: Compute wizards (cached_property)
    CMSCoreExtensions->>Wizard: Return sorted wizard instances
    Toolbar->>User: Show available wizards
Loading

Class diagram for lazy wizard initialization in CMSCoreExtensions

classDiagram
    class CMSCoreExtensions {
        +list lazy_wizards
        +dict toolbar_enabled_models
        +dict model_groupers
        +list toolbar_mixins
        +configure_wizards(cms_config)
        +configure_toolbar_enabled_models(cms_config)
        +wizards: dict[str, Wizard]  <<cached_property>>
    }
    class Wizard {
        +id
        +weight
        +title
        +user_has_add_permission(user, page)
        +get_model()
    }
    CMSCoreExtensions "1" o-- "*" Wizard : manages
Loading

Class diagram for new and deprecated wizard helper functions

classDiagram
    class wizard_base {
        +get_entries()
        +get_entry(entry_key)
        +entry_choices(user, page)
    }
    class helpers {
        +get_entries()  <<deprecated>>
        +get_entry(entry_key)  <<deprecated>>
    }
    class wizard_pool {
        +WizardPool  <<deprecated>>
        +entry_choices(user, page)  <<deprecated>>
    }
    helpers ..> wizard_base : re-exports (deprecated)
    wizard_pool ..> helpers : re-exports (deprecated)
    wizard_pool ..> wizard_base : re-exports (deprecated)
Loading

File-Level Changes

Change Details Files
Implement lazy wizard initialization using a cached_property
  • Replace direct wizards dict in CMSCoreExtensions.init with lazy_wizards list
  • Add a @cached_property wizards to aggregate, validate, and cache wizard instances
  • Update configure_wizards to append configurations to lazy_wizards instead of instantiating immediately
cms/cms_config.py
Centralize wizard access helpers in wizard_base
  • Introduce get_entries to retrieve and sort wizard instances by weight
  • Add get_entry for direct lookup by ID
  • Implement entry_choices to yield permitted wizard choices based on user and page
cms/wizards/wizard_base.py
Deprecate legacy helpers and wizard_pool modules with warnings
  • Remove old implementations in cms.wizards.helpers, import new base helpers, and emit RemovedInDjangoCMS51Warning
  • Import new helpers in wizard_pool, add deprecation warning in WizardPool.register
cms/wizards/helpers.py
cms/wizards/wizard_pool.py
Simplify toolbar logic to restore wizard availability on app-hooked pages
  • Consolidate page existence branches into a single disabled flag using entry_choices
  • Remove special case that always disabled toolbar when no page
cms/cms_toolbars.py
Update tests for lazy loading behavior and reset logic
  • Trigger configuration exception by accessing extensions.wizards instead of duplicate calls
  • Remove obsolete duplicate-wizard warning test
  • In teardown, delete cached wizards property to reset state
  • Add import and assertion for backward-compatibility wizard retrieval test
cms/tests/test_cms_config_wizards.py
cms/tests/test_wizards.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!

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 main!

Do not SQUASH commits to preserve history for the changelog.

@fsbraun fsbraun requested a review from vinitkumar June 27, 2025 20:14
@fsbraun
Copy link
Member Author
fsbraun commented Jun 27, 2025

@sourcery-ai review

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:

  • The new wizards cached_property silently merges duplicate wizard IDs—add a conflict check or warning to prevent unintended overrides of existing entries.
  • Misconfigured (non-Wizard) entries are only caught when accessing wizards; consider validating input immediately in configure_wizards to fail fast at startup.
  • In the wizards property the variable name wizard is reused in both the comprehension and the next call—rename one to avoid confusion and shadowing.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `wizards` cached_property silently merges duplicate wizard IDs—add a conflict check or warning to prevent unintended overrides of existing entries.
- Misconfigured (non-Wizard) entries are only caught when accessing `wizards`; consider validating input immediately in `configure_wizards` to fail fast at startup.
- In the `wizards` property the variable name `wizard` is reused in both the comprehension and the `next` call—rename one to avoid confusion and shadowing.

## Individual Comments

### Comment 1
<location> `cms/wizards/helpers.py:7` </location>
<code_context>
-    Returns a wizard object based on its :attr:`~.cms.wizards.wizard_base.Wizard.id`.
-    """
-    return apps.get_app_config('cms').cms_extension.wizards[entry_key]
+warnings.warn(
+    "The cms.wizards.helpers module is deprecated and will be removed in django CMS 5.1. "
+    "Use cms.wizards.wizard_base.get_entries and cms.wizards.wizard_pool.get_entry instead.",
</code_context>

<issue_to_address>
Deprecation warning message references an incorrect replacement.

'wizard_pool.get_entry' does not exist in the codebase. Please update the warning to reference only valid alternatives to avoid user confusion.
</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.

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Copy link

👋 Hi there!

Please remember to MERGE COMMIT pull requests from main!

Do not SQUASH commits to preserve history for the changelog.

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Copy link

👋 Hi there!

Please remember to MERGE COMMIT pull requests from main!

Do not SQUASH commits to preserve history for the changelog.

@fsbraun fsbraun merged commit 3651858 into release/5.0.x Jun 28, 2025
66 checks passed
fsbraun added a commit that referenced this pull request Jun 28, 2025
* fix: Allow lazy wizard initialization

* Update cms/cms_toolbars.py

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

* Update cms/cms_config.py

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@fsbraun fsbraun deleted the feat/ext-wizards branch June 28, 2025 13:57
fsbraun added a commit that referenced this pull request Jun 28, 2025
* fix: Allow lazy wizard initialization

* Update cms/cms_toolbars.py

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

* Update cms/cms_config.py

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
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
0