From b16f84f15b1344d2a3df8149565cfc8de803eb77 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 27 May 2015 09:19:19 -0400 Subject: [PATCH] [1.8.x] Refs #24836 -- Reverted "Simplified the lazy CSRF token implementation in csrf context processor." This reverts commit 8099d33b6553c9ee7de779ae9d191a1bf22adbda as it caused a regression that cannot be solved without changing force_text() which has a small risk of introducing regressions. This change will remain in master along with an update to force_text(). --- django/template/context_processors.py | 6 ++++-- docs/releases/1.8.3.txt | 3 +++ tests/csrf_tests/test_context_processor.py | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/csrf_tests/test_context_processor.py diff --git a/django/template/context_processors.py b/django/template/context_processors.py index a81fe71829d2a..dcd737f4ae6ab 100644 --- a/django/template/context_processors.py +++ b/django/template/context_processors.py @@ -11,8 +11,9 @@ from django.conf import settings from django.middleware.csrf import get_token +from django.utils import six from django.utils.encoding import smart_text -from django.utils.functional import SimpleLazyObject, lazy +from django.utils.functional import lazy def csrf(request): @@ -29,8 +30,9 @@ def _get_val(): return 'NOTPROVIDED' else: return smart_text(token) + _get_val = lazy(_get_val, six.text_type) - return {'csrf_token': SimpleLazyObject(_get_val)} + return {'csrf_token': _get_val()} def debug(request): diff --git a/docs/releases/1.8.3.txt b/docs/releases/1.8.3.txt index 1155ac1cb6107..d965630c77ec6 100644 --- a/docs/releases/1.8.3.txt +++ b/docs/releases/1.8.3.txt @@ -19,3 +19,6 @@ Bugfixes ``Count()`` (:ticket:`24835`). * Corrected ``HStoreField.has_changed()`` (:ticket:`24844`). + +* Reverted an optimization to the CSRF template context processor which caused + a regression (:ticket:`24836`). diff --git a/tests/csrf_tests/test_context_processor.py b/tests/csrf_tests/test_context_processor.py new file mode 100644 index 0000000000000..270b3e4771ab8 --- /dev/null +++ b/tests/csrf_tests/test_context_processor.py @@ -0,0 +1,15 @@ +import json + +from django.http import HttpRequest +from django.template.context_processors import csrf +from django.test import SimpleTestCase +from django.utils.encoding import force_text + + +class TestContextProcessor(SimpleTestCase): + + def test_force_text_on_token(self): + request = HttpRequest() + request.META['CSRF_COOKIE'] = 'test-token' + token = csrf(request).get('csrf_token') + self.assertEqual(json.dumps(force_text(token)), '"test-token"')