From df678b750026db2f9bc0108dddead4c35a9cfba3 Mon Sep 17 00:00:00 2001 From: Hiroki KIYOHARA Date: Sat, 26 Jul 2014 12:06:48 +0200 Subject: [PATCH] [1.7.x] Fixed code to solve #23070 problem Added a class to wrap callable in settings: * Not to call in the debug page (#21345). * Not to break the debug page if the callable forbidding to set attributes (#23070). Thanks @bmispelon for giving me some advice. Backport of d0889863de50d65659f56f0b9ea0672a8b6482a1 from master. --- django/views/debug.py | 16 +++++++++++++++- tests/view_tests/tests/test_debug.py | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/django/views/debug.py b/django/views/debug.py index efb3079d7eb..13992e9f982 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -31,6 +31,19 @@ def linebreak_iter(template_source): yield len(template_source) + 1 +class CallableSettingWrapper(object): + """ Object to wrap callable appearing in settings + + * Not to call in the debug page (#21345). + * Not to break the debug page if the callable forbidding to set attributes (#23070). + """ + def __init__(self, callable_setting): + self._wrapped = callable_setting + + def __repr__(self): + return repr(self._wrapped) + + def cleanse_setting(key, value): """Cleanse an individual setting key/value of sensitive content. @@ -50,7 +63,8 @@ def cleanse_setting(key, value): cleansed = value if callable(cleansed): - cleansed.do_not_call_in_templates = True + # For fixing #21345 and #23070 + cleansed = CallableSettingWrapper(cleansed) return cleansed diff --git a/tests/view_tests/tests/test_debug.py b/tests/view_tests/tests/test_debug.py index f799325ae40..79c0d3fd753 100644 --- a/tests/view_tests/tests/test_debug.py +++ b/tests/view_tests/tests/test_debug.py @@ -21,7 +21,7 @@ from django.test.utils import ( setup_test_template_loader, restore_template_loaders) from django.utils.encoding import force_text, force_bytes from django.utils import six -from django.views.debug import ExceptionReporter +from django.views.debug import CallableSettingWrapper, ExceptionReporter from .. import BrokenException, except_args from ..views import (sensitive_view, non_sensitive_view, paranoid_view, @@ -30,6 +30,21 @@ from ..views import (sensitive_view, non_sensitive_view, paranoid_view, multivalue_dict_key_error) +class CallableSettingWrapperTests(TestCase): + """ Unittests for CallableSettingWrapper + """ + def test_repr(self): + class WrappedCallable(object): + def __repr__(self): + return "repr from the wrapped callable" + + def __call__(self): + pass + + actual = repr(CallableSettingWrapper(WrappedCallable())) + self.assertEqual(actual, "repr from the wrapped callable") + + @override_settings(DEBUG=True, TEMPLATE_DEBUG=True) class DebugViewTests(TestCase): urls = "view_tests.urls"