Skip to content

fix: Ensure correct placeholder retrieval for PageContent instances #8088

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 5 commits into from
Dec 9, 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
6 changes: 3 additions & 3 deletions cms/app_registration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import inspect
from functools import lru_cache
from functools import cache
from importlib import import_module

from django.apps import apps
Expand Down Expand Up @@ -98,7 +98,7 @@ def autodiscover_cms_configs():
"CMSAppExtension")


@lru_cache(maxsize=None)
@cache
def get_cms_extension_apps():
"""
Returns django app configs of apps with a cms extension
Expand All @@ -110,7 +110,7 @@ def get_cms_extension_apps():
return cms_apps


@lru_cache(maxsize=None)
@cache
def get_cms_config_apps():
"""
Returns django app configs of apps with a cms config
Expand Down
4 changes: 2 additions & 2 deletions cms/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from functools import lru_cache
from functools import cache

from django.utils.functional import lazy

Expand All @@ -12,7 +12,7 @@ def cms_settings(request):
"""
from menus.menu_pool import MenuRenderer

@lru_cache(maxsize=None)
@cache
def _get_menu_renderer():
# We use lru_cache to avoid getting the manager
# every time this function is called.
Expand Down
17 changes: 8 additions & 9 deletions cms/plugin_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,25 +558,24 @@ def _get_cached_placeholder_content(self, placeholder, language):
return language_cache.get(placeholder.pk)

def _get_content_object(self, page, slots=None):
if self.toolbar.get_object() == page:
toolbar_obj = self.toolbar.get_object()
if isinstance(toolbar_obj, PageContent) and toolbar_obj.page == page:
# Current object belongs to the page itself
page_content = self.toolbar.get_object()
placeholders = Placeholder.objects.get_for_obj(page_content)
return Placeholder.objects.get_for_obj(toolbar_obj)
elif slots:
# If looking for inherited placeholders, slots is specified
if self.toolbar.preview_mode_active or self.toolbar.edit_mode_active:
page_content = (page.pagecontent_set(manager="admin_manager")
.current_content(language=self.request_language).first())
else:
page_content = page.pagecontent_set.filter(language=self.request_language).first()
placeholders = Placeholder.objects.get_for_obj(page_content) if page_content else Placeholder.objects.none()
else:
page_content = page.get_content_obj(self.request_language, fallback=False)

return Placeholder.objects.get_for_obj(page_content) if page_content else Placeholder.objects.none()
elif page_content := page.get_content_obj(self.request_language, fallback=False):
PageContent.page.field.set_cached_value(page_content, page)
# Creates any placeholders missing on the page
placeholders = page_content.rescan_placeholders().values()
return placeholders
return page_content.rescan_placeholders().values()
else:
return Placeholder.objects.none()

def _preload_placeholders_for_page(self, page, slots=None, inherit=False):
"""
Expand Down
13 changes: 13 additions & 0 deletions cms/tests/test_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,19 @@ def test_details_view(self):
r = self.strip_rendered(response.content.decode('utf8'))
self.assertEqual(r, '|' + self.test_data['text_main'] + '|' + self.test_data['text_sub'] + '|')

def test_getting_placeholders(self):
"""ContentRenderer._get_content_object uses toolbar to return placeholders of a page"""
request = self.get_request(page=self.test_page)
request.toolbar = CMSToolbar(request)
wrong_content_obj = self.test_page2.get_content_obj("en")
wrong_content_obj.page = self.test_page
request.toolbar.set_object(wrong_content_obj)
content_renderer = self.get_content_renderer(request)
placeholders = content_renderer._get_content_object(self.test_page)
wrong_content_obj.page = self.test_page2

self.assertEqual(len(placeholders), 2)

@override_settings(
CMS_PLUGIN_PROCESSORS=('cms.tests.test_rendering.sample_plugin_processor',),
CMS_PLUGIN_CONTEXT_PROCESSORS=('cms.tests.test_rendering.sample_plugin_context_processor',),
Expand Down
4 changes: 2 additions & 2 deletions cms/utils/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from collections import OrderedDict, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from functools import cache, lru_cache
from itertools import starmap
from operator import itemgetter

Expand All @@ -20,7 +20,7 @@
logger = logging.getLogger(__name__)


@lru_cache(maxsize=None)
@cache
def get_plugin_class(plugin_type: str) -> CMSPluginBase:
"""Returns the plugin class for a given plugin_type (str)"""
return plugin_pool.get_plugin(plugin_type)
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ extend-ignore = [
"PLW0603",
"PLW0602",
"PLW2901",
"UP006", # Use `list` instead of `typing.List` for type annotation
"UP028",
"UP030",
"UP031",
"UP035", # UP035 `typing.List` is deprecated, use `list` instead
]

[tool.ruff.lint.per-file-ignores]
Expand Down
Loading