Skip to content

Commit

Permalink
[1.8.x] Refs #24836 -- Reverted "Simplified the lazy CSRF token imple…
Browse files Browse the repository at this point in the history
…mentation in csrf context processor."

This reverts commit 8099d33 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().
  • Loading branch information
timgraham committed May 27, 2015
1 parent 1e3741b commit b16f84f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions django/template/context_processors.py
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/1.8.3.txt
Expand Up @@ -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`).
15 changes: 15 additions & 0 deletions 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"')

0 comments on commit b16f84f

Please sign in to comment.