diff --git a/AUTHORS b/AUTHORS index fc2dbf0..40f7002 100644 --- a/AUTHORS +++ b/AUTHORS @@ -8,6 +8,7 @@ PRIMARY AUTHORS are and/or have been (alphabetic order): CONTRIBUTORS are and/or have been (alphabetic order): + - Lucas Wiman - diff --git a/django_tools/__init__.py b/django_tools/__init__.py index 6dab188..951ff47 100644 --- a/django_tools/__init__.py +++ b/django_tools/__init__.py @@ -1,4 +1,4 @@ # coding: utf-8 -__version__ = "0.29.2" +__version__ = "0.30.0" diff --git a/django_tools/middlewares/ThreadLocal.py b/django_tools/middlewares/ThreadLocal.py index 917fbe1..4627b99 100644 --- a/django_tools/middlewares/ThreadLocal.py +++ b/django_tools/middlewares/ThreadLocal.py @@ -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() -------------------------------------------------------------------------- @@ -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) @@ -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 diff --git a/django_tools_test_project/django_tools_test_app/urls.py b/django_tools_test_project/django_tools_test_app/urls.py index d396962..49fc482 100644 --- a/django_tools_test_project/django_tools_test_app/urls.py +++ b/django_tools_test_project/django_tools_test_app/urls.py @@ -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)), ) diff --git a/django_tools_test_project/django_tools_test_app/views.py b/django_tools_test_project/django_tools_test_app/views.py index b7d3035..2179fac 100644 --- a/django_tools_test_project/django_tools_test_app/views.py +++ b/django_tools_test_project/django_tools_test_app/views.py @@ -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): @@ -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)) diff --git a/django_tools_test_project/test_settings.py b/django_tools_test_project/test_settings.py index 64bb21b..cdb0a9d 100644 --- a/django_tools_test_project/test_settings.py +++ b/django_tools_test_project/test_settings.py @@ -26,6 +26,7 @@ MIDDLEWARE_CLASSES = ( 'django_tools.dynamic_site.middleware.DynamicSiteMiddleware', + 'django_tools.middlewares.ThreadLocal.ThreadLocalMiddleware', ) INSTALLED_APPS = ( diff --git a/tests/test_ThreadLocal_middleware.py b/tests/test_ThreadLocal_middleware.py new file mode 100644 index 0000000..28eb6fa --- /dev/null +++ b/tests/test_ThreadLocal_middleware.py @@ -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)