8000 fix: Fail silently when rendering a placeholder on a missing toolbar … · django-cms/django-cms@0f81cea · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f81cea

Browse files
authored
fix: Fail silently when rendering a placeholder on a missing toolbar object (#7954)
* Fix: Fail silently when rendering a placeholder on a missing toolbar object * Fix new contributor message
1 parent 302c1b5 commit 0f81cea

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

.github/workflows/new_contributor_pr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
needs: new
3030
steps:
3131
- name: Send Discord Webhook
32+
continue-on-error: true
3233
env:
3334
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL }}
3435
DISCORD_EMBEDS: |

cms/plugin_rendering.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.utils.translation import override
1414

1515
from cms.cache.placeholder import get_placeholder_cache, set_placeholder_cache
16+
from cms.exceptions import PlaceholderNotFound
1617
from cms.models import PageContent, Placeholder
1718
from cms.toolbar.utils import (
1819
get_placeholder_toolbar_js,
@@ -343,6 +344,8 @@ def render_obj_placeholder(self, slot, context, inherit,
343344
# Not page, therefore we will use toolbar object as
344345
# the current object and render the placeholder
345346
current_obj = self.toolbar.get_object()
347+
if current_obj is None:
348+
raise PlaceholderNotFound(f"No object found for placeholder '{slot}'")
346349
rescan_placeholders_for_obj(current_obj)
347350
placeholder = Placeholder.objects.get_for_obj(current_obj).get(slot=slot)
348351
content = self.render_placeholder(

cms/tests/test_placeholder.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from cms import constants
1313
from cms.api import add_plugin, create_page, create_page_content
14-
from cms.exceptions import DuplicatePlaceholderWarning
14+
from cms.exceptions import DuplicatePlaceholderWarning, PlaceholderNotFound
1515
from cms.models.fields import PlaceholderField
1616
from cms.models.placeholdermodel import Placeholder
1717
from cms.models.pluginmodel import CMSPlugin
@@ -141,6 +141,17 @@ def test_placeholder_scanning_var(self):
141141
phs = sorted(node.get_declaration().slot for node in _scan_placeholders(t.nodelist))
142142
self.assertListEqual(phs, sorted(['two', 'new_one', 'base_outside']))
143143

144+
def test_placeholder_scanning_no_object(self):
145+
"""Placeholder scanning for a template without a toolbar object raises PlaceholderNotFound"""
146+
147+
context = SekizaiContext()
148+
request = self.get_request(language="en", page=None)
149+
toolbar = get_toolbar_from_request(request)
150+
renderer = toolbar.get_content_renderer()
151+
with self.assertRaises(PlaceholderNotFound):
152+
renderer.render_obj_placeholder("someslot", context, False)
153+
154+
144155
def test_fieldsets_requests(self):
145156
response = self.client.get(admin_reverse('placeholderapp_example1_add'))
146157
self.assertEqual(response.status_code, 200)

cms/utils/placeholder.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import operator
22
import warnings
33
from collections import OrderedDict
4+
from typing import Union
45

56
from django.conf import settings
67
from django.core.exceptions import ImproperlyConfigured
@@ -406,11 +407,13 @@ def rescan_placeholders_for_obj(obj):
406407
return existing
407408

408409

409-
def get_declared_placeholders_for_obj(obj):
410+
def get_declared_placeholders_for_obj(obj: Union[models.Model, None]) -> list[Placeholder]:
410411
"""Returns declared placeholders for an object. The object is supposed to have a method ``get_template``
411412
which returns the template path as a string that renders the object. ``get_declared_placeholders`` returns
412413
a list of placeholders used in the template by the ``{% placeholder %}`` template tag.
413414
"""
415+
if obj is None:
416+
return []
414417
if not hasattr(obj, "get_template"):
415418
raise NotImplementedError(
416419
"%s should implement get_template" % obj.__class__.__name__

0 commit comments

Comments
 (0)
0