8000 fix: Respect ContentAdminManager pattern for frontend-editable models by fsbraun · Pull Request #7998 · django-cms/django-cms · GitHub
[go: up one dir, main page]

Skip to content

fix: Respect ContentAdminManager pattern for frontend-editable models #7998

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
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test_startcmsproject.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: false
matrix:
django-version: [
'4.2', '5.0',
'4.2', '5.0', '5.1'
]
python-version: ['3.11']
requirements-file: ['requirements_base.txt']
Expand Down
10 changes: 8 additions & 2 deletions cms/admin/placeholderadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,15 @@ def _get_object_for_single_field(self, object_id, language):
# Quick and dirty way to retrieve objects for django-hvad
# Cleaner implementation will extend this method in a child mixin
try:
return self.model.objects.language(language).get(pk=object_id)
# First see if the model uses the admin manager pattern from cms.models.manager.ContentAdminManager
manager = self.model.admin_manager
except AttributeError:
return self.model.objects.get(pk=object_id)
# If not, use the default manager
manager = self.model.objects
try:
return manager.language(language).get(pk=object_id)
except AttributeError:
return manager.get(pk=object_id)

def edit_field(self, request, object_id, language):
obj = self._get_object_for_single_field(object_id, language)
Expand Down
2 changes: 1 addition & 1 deletion cms/cms_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def get_page_content(self):
# Toolbar object already set (e.g., in edit or preview mode)
return self.obj
# Get from db
page_content = self.page.get_content_obj(language=self.current_lang, fallback=False)
page_content = self.page.get_admin_content(language=self.current_lang, fallback=False)
return page_content or None

def has_page_change_permission(self):
Expand Down
3 changes: 0 additions & 3 deletions cms/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ class Post(models.Model):
@cached_property
def content(self):
return get_placeholder_from_slot(self.placeholders, "content") # A specific placeholder



"""
default_checks = []

Expand Down
2 changes: 1 addition & 1 deletion cms/templates/admin/cms/page/plugin/error_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% load i18n l10n static %}

{% block title %}{% trans "Edit model" %}{% endblock %}

{% block breadcrumbs %}{% endblock %}
{% block content %}
<form action="." method="post" id="cmsplugin_form">
{% csrf_token %}
Expand Down
2 changes: 1 addition & 1 deletion cms/templatetags/cms_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def _get_content(self, context, instance, attribute, language, filters):
if not attr_value:
attr_value = getattr(instance, attribute, '')
extra_context['content'] = attr_value
# This allow the requested item to be a method, a property or an
# This allows the requested item to be a method, a property or an
# attribute
if callable(extra_context['content']):
if isinstance(instance, Page):
Expand Down
4 changes: 4 additions & 0 deletions cms/test_utils/project/placeholderapp/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models
from django.urls import reverse

from cms.models import ContentAdminManager
from cms.models.fields import PlaceholderField
from cms.utils import get_language_from_request
from cms.utils.urlutils import admin_reverse
Expand All @@ -26,6 +27,9 @@ class Example1(models.Model):
max_digits=5, decimal_places=1,
blank=True, null=True,)

admin_manager = ContentAdminManager()
objects = models.Manager()

static_admin_url = ''

def __init__(self, *args, **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions cms/tests/test_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,19 @@ def test_view_method(self):
self.assertContains(
response, "edit_plugin: '/admin/placeholderapp/example1/edit-field/%s/en/" % ex1.pk)

def test_edit_field_respects_content_admin_mixin(self):
user = self.get_staff()
ex1 = self._get_example_obj()
edit_url = admin_reverse('placeholderapp_example1_edit_field', args=(ex1.pk, "en"))

with patch('cms.test_utils.project.placeholderapp.models.Example1.admin_manager.get') as get_mock:
with self.login_user_context(user):
get_mock.return_value = ex1
self.client.get(edit_url + "?edit_fields=char_1")

self.assertEqual(edit_url, f"/admin/placeholderapp/example1/edit-field/{ex1.pk}/en/")
Example1.admin_manager.get.assert_called_once_with(pk=str(ex1.pk))

def test_view_url(self):
user = self.get_staff()
page = create_page('Test', 'col_two.html', 'en')
Expand Down
Loading
0