Skip to content

Conversation

@fsbraun
Copy link
Member

@fsbraun fsbraun commented Oct 28, 2025

Description

Fix alias creation and detachment flows by adding draft content support, streamlining plugin population logic, and correcting the create_alias_view handler to prevent errors.

Bug Fixes:

  • Remove invalid source_plugin handling in create_alias_view to prevent render_replace_response errors

Enhancements:

  • Extend get_plugins to accept show_draft_content and forward it to placeholder retrieval
  • Simplify populate method by directly handling replaced_placeholder and cleaned-up plugin deletion
  • Refine detach_alias_plugin to capture plugin position and parent, delete the alias plugin, shift positions correctly, and copy source plugins using copy_plugins_to_placeholder

Fixes #306

Related resources

  • #...
  • #...

Checklist

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 28, 2025

Reviewer's Guide

This PR refactors alias plugin handling by adding draft-content context propagation, streamlining placeholder and plugin tree operations during alias creation, improving plugin detachment logic with accurate position and hierarchy preservation, and cleaning up unused view parameters.

Sequence diagram for improved alias plugin detachment

sequenceDiagram
    participant User
    participant "AliasPlugin"
    participant "Alias"
    participant "TargetPlaceholder"
    participant "SourcePlugins"
    User->>AliasPlugin: Request detach_alias_plugin(plugin, language)
    AliasPlugin->>Alias: get_plugins(language, show_draft_content=True)
    Alias->>SourcePlugins: Return source_plugins
    AliasPlugin->>TargetPlaceholder: delete_plugin(plugin)
    alt SourcePlugins exist
        TargetPlaceholder->>TargetPlaceholder: get_last_plugin(language)
        TargetPlaceholder->>TargetPlaceholder: _shift_plugin_positions(language, start=plugin.position, offset=...)
        AliasPlugin->>TargetPlaceholder: copy_plugins_to_placeholder(source_plugins, root_plugin=plugin.parent, start_positions={language: plugin.position})
    end
Loading

Class diagram for updated Alias and plugin handling

classDiagram
    class Alias {
        +get_placeholder(language=None, show_draft_content=False)
        +get_plugins(language=None, show_draft_content=False)
    }
    class AliasPlugin {
        +detach_alias_plugin(plugin, language)
    }
    class Placeholder {
        +delete_plugin(plugin)
        +get_last_plugin(language)
        +_shift_plugin_positions(language, start, offset)
    }
    class CMSPlugin {
        +_get_descendants_ids()
        +delete()
    }
    AliasPlugin --> Alias : uses
    AliasPlugin --> Placeholder : manipulates
    Alias --> Placeholder : retrieves
    Placeholder --> CMSPlugin : manages
    CMSPlugin --> Placeholder : belongs to
Loading

Class diagram for updated populate logic in Alias

classDiagram
    class Alias {
        +populate(replaced_placeholder=None, replaced_plugin=None, plugins=None)
    }
    class Placeholder {
        +cmsplugin_set
        +_recalculate_plugin_positions(language)
    }
    class CMSPlugin {
        +pk
        +placeholder
        +position
        +parent
        +_get_descendants_ids()
        +delete()
    }
    Alias --> Placeholder : updates
    Alias --> CMSPlugin : copies/deletes
    CMSPlugin --> Placeholder : belongs to
Loading

File-Level Changes

Change Details Files
Enhanced get_plugins to support draft content context
  • Add show_draft_content parameter to get_plugins signature
  • Pass show_draft_content through to get_placeholder when populating cache
  • Maintain separate plugin cache entries based on draft-content flag
djangocms_alias/models.py
Refactored populate method for alias creation
  • Early-return branch for replaced_placeholder with bulk cmsplugin_set update and alias addition
  • Consolidate replaced_plugin flow: fetch subtree, copy to new placeholder, delete original plugin, then add alias
  • Remove legacy manual deletion, position recalculation, and update_fields save steps
djangocms_alias/models.py
Overhauled detach_alias_plugin for correct plugin reattachment
  • Switch to alias.get_plugins with draft-content flag instead of get_placeholder
  • Capture original plugin's position and parent before deletion
  • Use delete_plugin and dynamic position shifting based on calculated offset
  • Include root_plugin and explicit start_positions when copying plugins
djangocms_alias/cms_plugins.py
Cleaned create_alias_view by removing unused plugin references
  • Eliminate plugin variable assignment from form data
  • Comment out source_plugin parameter in render_replace_response
djangocms_alias/views.py

Assessment against linked issues

Issue Objective Addressed Explanation
#306 Fix the IntegrityError when using 'Create Alias' with 'Replace Current Plugin' in djangocms-alias 2.0.5.
#306 Ensure that when replacing a plugin with an alias, the alias is created correctly and is not blank.

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

  • In get_plugins, the cache key only includes language but not show_draft_content, so consider incorporating draft flag into the key or invalidating the cache when draft mode changes to prevent stale plugin lists.
  • In detach_alias_plugin you call get_last_plugin with plugin.language but then use the language parameter for shifting—unify these to consistently use the passed-in language.
  • You commented out passing source_plugin to render_replace_response in create_alias_view; ensure the downstream handler no longer requires that argument or update its signature accordingly.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In get_plugins, the cache key only includes language but not show_draft_content, so consider incorporating draft flag into the key or invalidating the cache when draft mode changes to prevent stale plugin lists.
- In detach_alias_plugin you call get_last_plugin with plugin.language but then use the language parameter for shifting—unify these to consistently use the passed-in language.
- You commented out passing source_plugin to render_replace_response in create_alias_view; ensure the downstream handler no longer requires that argument or update its signature accordingly.

## Individual Comments

### Comment 1
<location> `djangocms_alias/models.py:196` </location>
<code_context>
         return getattr(content, "placeholder", None)

-    def get_plugins(self, language=None):
+    def get_plugins(self, language=None, show_draft_content=False):
         if not language:
             language = get_language()
</code_context>

<issue_to_address>
**issue (bug_risk):** Consider updating cache key to include show_draft_content.

The cache key should account for show_draft_content to prevent returning incorrect plugin lists when this parameter varies.
</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.

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.

2 participants