Skip to content

Commit

Permalink
Merge pull request #7 from lucaswiman/master
Browse files Browse the repository at this point in the history
Clear ThreadLocal request atttribute after response is processed

Thanks!
  • Loading branch information
jedie committed Aug 10, 2015
2 parents d3f784f + c60c9c8 commit bb84dbf
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ PRIMARY AUTHORS are and/or have been (alphabetic order):


CONTRIBUTORS are and/or have been (alphabetic order):
- Lucas Wiman
-


Expand Down
2 changes: 1 addition & 1 deletion django_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# coding: utf-8

__version__ = "0.29.2"
__version__ = "0.30.0"

9 changes: 7 additions & 2 deletions django_tools/middlewares/ThreadLocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Get the current request object:
request = ThreadLocal.get_current_request()
# You can get the current user directy with:
# You can get the current user directly with:
user = ThreadLocal.get_current_user()
--------------------------------------------------------------------------
Expand All @@ -47,7 +47,7 @@


def get_current_request():
""" returns the request object for this thead """
""" returns the request object for this thread """
return getattr(_thread_locals, "request", None)


Expand All @@ -62,3 +62,8 @@ class ThreadLocalMiddleware(object):
""" Simple middleware that adds the request object in thread local storage."""
def process_request(self, request):
_thread_locals.request = request

def process_response(self, request, response):
if hasattr(_thread_locals, 'request'):
del _thread_locals.request
return response
5 changes: 5 additions & 0 deletions django_tools_test_project/django_tools_test_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@


from django_tools_test_project.django_tools_test_app.views import display_site
from django_tools_test_project.django_tools_test_app.views import get_current_get_parameters
from django_tools_test_project.django_tools_test_app.views import raise_exception
from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
url(r'^display_site/$', display_site),
url(r'^get_current_get_parameters/$', get_current_get_parameters),
url(r'^raise_exception/$', raise_exception),

url(r'^admin/', include(admin.site.urls)),
)
16 changes: 16 additions & 0 deletions django_tools_test_project/django_tools_test_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

from __future__ import absolute_import, division, print_function

import json

from django.http import HttpResponse
from django.conf import settings
from django.contrib.sites.models import Site

from django_tools.middlewares.ThreadLocal import get_current_request



def display_site(request):
Expand All @@ -30,3 +33,16 @@ def display_site(request):



def raise_exception(request):
"""
This view just raises an exception as a way to test middleware exception
handling.
"""
raise Exception()


def get_current_get_parameters(request):
"""
Returns a JSON version of request.GET from the current request
"""
return HttpResponse(json.dumps(get_current_request().GET))
1 change: 1 addition & 0 deletions django_tools_test_project/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

MIDDLEWARE_CLASSES = (
'django_tools.dynamic_site.middleware.DynamicSiteMiddleware',
'django_tools.middlewares.ThreadLocal.ThreadLocalMiddleware',
)

INSTALLED_APPS = (
Expand Down
24 changes: 24 additions & 0 deletions tests/test_ThreadLocal_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json

from django.test import TestCase

from django_tools.middlewares.ThreadLocal import get_current_request


class TestGetCurrentRequest(TestCase):
def test_current_request_receives_current_request(self):
response = self.client.get('/get_current_get_parameters/?')
current_get_parameters = json.loads(response.content.decode('utf-8'))
self.assertEqual(current_get_parameters, {})
response = self.client.get('/get_current_get_parameters/?foo=bar')
current_get_parameters = json.loads(response.content.decode('utf-8'))
self.assertEqual(current_get_parameters, {'foo': 'bar'})

def test_current_request_is_cleared_after_request_is_finished(self):
response = self.client.get('/get_current_get_parameters/')
self.assertEqual(get_current_request(), None)

def test_current_request_is_cleared_when_exception_is_raised(self):
with self.assertRaises(Exception):
response = self.client.get('/raise_exception/')
self.assertEqual(get_current_request(), None)

0 comments on commit bb84dbf

Please sign in to comment.