Skip to content

Fix multi site #479

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Fix multi site #479

wants to merge 2 commits into from

Conversation

stefanw
Copy link
Contributor

@stefanw stefanw commented Aug 5, 2025

Description

This builds on django-cms#8303 and copies the changes to page admin change form template.
The URL for published content can now be behind get_full_url in order to access a separate domain when the versioned object is associated with a different site.

Related resources

Checklist

  • I have opened this pull request against master
  • 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 #workgroup-pr-review on
    Slack to find a “pr review buddy” who is going to review my pull request.

Summary by Sourcery

Fix multi-site support by using full URLs for versioned content and preserving site context in the page admin interface

Enhancements:

  • Prefer get_full_url over get_absolute_url for publish redirects and CMS toolbar buttons
  • Add site query parameter to the page admin change form action to retain multi-site context

Copy link
Contributor

sourcery-ai bot commented Aug 5, 2025

Reviewer's Guide

This PR extends multi-site compatibility by leveraging get_full_url in both the publish view redirect and CMS toolbar, and by preserving the site query parameter in the page admin change form.

Sequence diagram for publish view redirect with multi-site support

sequenceDiagram
    participant Admin as actor Admin User
    participant VersioningAdmin as VersioningAdmin
    participant VersionContent as Version Content
    participant Site as Site

    Admin->>VersioningAdmin: publish_view(request, object_id)
    VersioningAdmin->>VersionContent: check for get_full_url
    alt get_full_url exists and returns URL
        VersionContent-->>VersioningAdmin: full_url
        VersioningAdmin->>Admin: redirect(full_url)
    else get_absolute_url exists
        VersionContent-->>VersioningAdmin: get_absolute_url
        VersioningAdmin->>Admin: internal_redirect(requested_redirect)
    else
        VersioningAdmin->>Admin: internal_redirect(requested_redirect)
    end
Loading

Sequence diagram for CMS toolbar 'View Published' button with multi-site support

sequenceDiagram
    participant User as actor CMS User
    participant Toolbar as Toolbar
    participant PublishedVersion as Published Version
    participant Site as Site

    User->>Toolbar: Open toolbar
    Toolbar->>PublishedVersion: check for get_full_url
    alt get_full_url exists
        PublishedVersion-->>Toolbar: full_url
        Toolbar->>User: Show 'View Published' button (full_url)
    else get_absolute_url exists
        PublishedVersion-->>Toolbar: get_absolute_url
        Toolbar->>User: Show 'View Published' button (absolute_url)
    end
Loading

File-Level Changes

Change Details Files
Enhance publish_view redirect to prefer get_full_url
  • Guard against existing requested_redirect
  • Use get_full_url() and redirect to it if available
  • Fallback to get_absolute_url() otherwise
djangocms_versioning/admin.py
Update toolbar published view button to use get_full_url
  • Prefer get_full_url() for button URL
  • Fallback to get_absolute_url() if full URL missing
djangocms_versioning/cms_toolbars.py
Add site parameter to page change form action
  • Inject site query parameter into form action URL
djangocms_versioning/templates/admin/djangocms_versioning/page/change_form.html

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

  • Extract the logic for choosing between get_full_url and get_absolute_url into a reusable helper to eliminate duplication in admin.py and cms_toolbars.py.
  • When performing an immediate redirect on full_url, ensure that all user messages and context are preserved; centralizing the redirect logic could help maintain consistency.
  • The form action now includes many inline query parameters—consider refactoring this into a template tag or helper to improve readability and maintainability.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Extract the logic for choosing between get_full_url and get_absolute_url into a reusable helper to eliminate duplication in admin.py and cms_toolbars.py.
- When performing an immediate redirect on full_url, ensure that all user messages and context are preserved; centralizing the redirect logic could help maintain consistency.
- The form action now includes many inline query parameters—consider refactoring this into a template tag or helper to improve readability and maintainability.

## Individual Comments

### Comment 1
<location> `djangocms_versioning/admin.py:1107` </location>
<code_context>
+        if not requested_redirect and conf.ON_PUBLISH_REDIRECT == "published":
+            if hasattr(version.content, "get_full_url"):
+                full_url = version.content.get_full_url()
+                if full_url:
+                    # Can't resolve full_url, redirect directly to it
+                    return redirect(full_url)
+            elif hasattr(version.content, "get_absolute_url"):
</code_context>

<issue_to_address>
Redirecting directly to 'full_url' may bypass internal URL resolution.

Validating 'full_url' before redirecting can help prevent open redirect vulnerabilities. Ensure the URL is within the expected domain or document why this approach is secure.
</issue_to_address>

### Comment 2
<location> `djangocms_versioning/cms_toolbars.py:261` </location>
<code_context>
             return

-        url = published_version.get_absolute_url() if hasattr(published_version, "get_absolute_url") else None
+        url = published_version.get_full_url() if hasattr(published_version, "get_full_url") else None
+        if not url and hasattr(published_version, "get_absolute_url"):
+            url = published_version.get_absolute_url()
         if url and (self.toolbar.edit_mode_active or self.toolbar.preview_mode_active):
</code_context>

<issue_to_address>
The fallback logic for URL resolution could be simplified.

If 'get_full_url' exists but returns a falsy value, the code falls back to 'get_absolute_url'. Confirm this is intentional, as it could hide problems with 'get_full_url' implementations.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
        url = published_version.get_full_url() if hasattr(published_version, "get_full_url") else None
        if not url and hasattr(published_version, "get_absolute_url"):
            url = published_version.get_absolute_url()
=======
        if hasattr(published_version, "get_full_url"):
            url = published_version.get_full_url()
        elif hasattr(published_version, "get_absolute_url"):
            url = published_version.get_absolute_url()
        else:
            url = None
>>>>>>> REPLACE

</suggested_fix>

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.

Comment on lines +1107 to +1108
if full_url:
# Can't resolve full_url, redirect directly to it
Copy link
Contributor

Choose a reason for hiding this comment

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

🚨 issue (security): Redirecting directly to 'full_url' may bypass internal URL resolution.

Validating 'full_url' before redirecting can help prevent open redirect vulnerabilities. Ensure the URL is within the expected domain or document why this approach is secure.

Comment on lines +261 to +263
url = published_version.get_full_url() if hasattr(published_version, "get_full_url") else None
if not url and hasattr(published_version, "get_absolute_url"):
url = published_version.get_absolute_url()
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: The fallback logic for URL resolution could be simplified.

If 'get_full_url' exists but returns a falsy value, the code falls back to 'get_absolute_url'. Confirm this is intentional, as it could hide problems with 'get_full_url' implementations.

Suggested change
url = published_version.get_full_url() if hasattr(published_version, "get_full_url") else None
if not url and hasattr(published_version, "get_absolute_url"):
url = published_version.get_absolute_url()
if hasattr(published_version, "get_full_url"):
url = published_version.get_full_url()
elif hasattr(published_version, "get_absolute_url"):
url = published_version.get_absolute_url()
else:
url = None

Comment on lines +1106 to +1107
full_url = version.content.get_full_url()
if full_url:
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (code-quality): Use named expression to simplify assignment and conditional (use-named-expression)

Suggested change
full_url = version.content.get_full_url()
if full_url:
if full_url := version.content.get_full_url():

Copy link

codecov bot commented Aug 5, 2025

Codecov Report

❌ Patch coverage is 70.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.56%. Comparing base (a132204) to head (712f59c).
⚠️ Report is 9 commits behind head on master.

Files with missing lines Patch % Lines
djangocms_versioning/admin.py 57.14% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #479      +/-   ##
==========================================
+ Coverage   90.55%   93.56%   +3.01%     
==========================================
  Files          72       76       +4     
  Lines        2732     2705      -27     
  Branches      322        0     -322     
==========================================
+ Hits         2474     2531      +57     
+ Misses        182      174       -8     
+ Partials       76        0      -76     

☔ 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.

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