From aa211238aed21fde0e548d8f9a2e68f1d693ecb6 Mon Sep 17 00:00:00 2001 From: Lucas Wiman Date: Fri, 7 Aug 2015 15:25:17 -0700 Subject: [PATCH 1/8] Correct typos in documentation --- django_tools/middlewares/ThreadLocal.py | 4 ++-- tests/test_ThreadLocal_middleware.py | 0 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 tests/test_ThreadLocal_middleware.py diff --git a/django_tools/middlewares/ThreadLocal.py b/django_tools/middlewares/ThreadLocal.py index 917fbe1..854d554 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) diff --git a/tests/test_ThreadLocal_middleware.py b/tests/test_ThreadLocal_middleware.py new file mode 100644 index 0000000..e69de29 From c569dc016acf816626a6a1d736188e73c1d9f1d5 Mon Sep 17 00:00:00 2001 From: Lucas Wiman Date: Fri, 7 Aug 2015 15:27:24 -0700 Subject: [PATCH 2/8] Add failing test that _thread_locals.current_request is cleared after request-response cycle completes --- .../django_tools_test_app/urls.py | 3 +++ .../django_tools_test_app/views.py | 10 ++++++++++ django_tools_test_project/test_settings.py | 1 + tests/test_ThreadLocal_middleware.py | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+) 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..aabd53b 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,13 @@ 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.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'^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..539036c 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,10 @@ def display_site(request): + + +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 index e69de29..c14c496 100644 --- a/tests/test_ThreadLocal_middleware.py +++ b/tests/test_ThreadLocal_middleware.py @@ -0,0 +1,19 @@ +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) + self.assertEqual(current_get_parameters, {}) + response = self.client.get('/get_current_get_parameters/?foo=bar') + current_get_parameters = json.loads(response.content) + 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) From 2d651914bfcb0ae939e743c4746da03a0d65f924 Mon Sep 17 00:00:00 2001 From: Lucas Wiman Date: Fri, 7 Aug 2015 15:32:02 -0700 Subject: [PATCH 3/8] Add failing test for when exceptions are raised in the view --- django_tools_test_project/django_tools_test_app/urls.py | 2 ++ django_tools_test_project/django_tools_test_app/views.py | 6 ++++++ tests/test_ThreadLocal_middleware.py | 5 +++++ 3 files changed, 13 insertions(+) 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 aabd53b..49fc482 100644 --- a/django_tools_test_project/django_tools_test_app/urls.py +++ b/django_tools_test_project/django_tools_test_app/urls.py @@ -13,12 +13,14 @@ 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 539036c..2179fac 100644 --- a/django_tools_test_project/django_tools_test_app/views.py +++ b/django_tools_test_project/django_tools_test_app/views.py @@ -33,6 +33,12 @@ 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): diff --git a/tests/test_ThreadLocal_middleware.py b/tests/test_ThreadLocal_middleware.py index c14c496..b256855 100644 --- a/tests/test_ThreadLocal_middleware.py +++ b/tests/test_ThreadLocal_middleware.py @@ -17,3 +17,8 @@ def test_current_request_receives_current_request(self): 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) From ff7e61eb40652bfa3841f63d729fcdd135c70b52 Mon Sep 17 00:00:00 2001 From: Lucas Wiman Date: Fri, 7 Aug 2015 15:32:34 -0700 Subject: [PATCH 4/8] Fix failing tests by removing the _thread_locals.request variable when the response is processed --- django_tools/middlewares/ThreadLocal.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/django_tools/middlewares/ThreadLocal.py b/django_tools/middlewares/ThreadLocal.py index 854d554..4627b99 100644 --- a/django_tools/middlewares/ThreadLocal.py +++ b/django_tools/middlewares/ThreadLocal.py @@ -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 From 86328c672a8b9074d3e8c1e43c4411f94fb30227 Mon Sep 17 00:00:00 2001 From: Lucas Wiman Date: Fri, 7 Aug 2015 15:37:31 -0700 Subject: [PATCH 5/8] Bump version to reflect changed behavior --- django_tools/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" From 8c8f5b4da451705051871449e5187c7ca2519030 Mon Sep 17 00:00:00 2001 From: Lucas Wiman Date: Fri, 7 Aug 2015 15:56:11 -0700 Subject: [PATCH 6/8] Fix bytes to str conversion for python 3 compatibility --- tests/test_ThreadLocal_middleware.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_ThreadLocal_middleware.py b/tests/test_ThreadLocal_middleware.py index b256855..0c87b0a 100644 --- a/tests/test_ThreadLocal_middleware.py +++ b/tests/test_ThreadLocal_middleware.py @@ -8,10 +8,10 @@ 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) + 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) + 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): From 116df9f964b3be61d76e00ec38aea46e0e1b6723 Mon Sep 17 00:00:00 2001 From: Lucas Wiman Date: Fri, 7 Aug 2015 16:17:16 -0700 Subject: [PATCH 7/8] Fix weird capitalization in test name --- tests/test_ThreadLocal_middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_ThreadLocal_middleware.py b/tests/test_ThreadLocal_middleware.py index 0c87b0a..28eb6fa 100644 --- a/tests/test_ThreadLocal_middleware.py +++ b/tests/test_ThreadLocal_middleware.py @@ -18,7 +18,7 @@ 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): + 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) From c60c9c805510a8fb6b055cb915df9c0f1d271718 Mon Sep 17 00:00:00 2001 From: Lucas Wiman Date: Sat, 8 Aug 2015 14:37:36 -0700 Subject: [PATCH 8/8] Add myself to authors for https://github.com/jedie/django-tools/pull/7 --- AUTHORS | 1 + 1 file changed, 1 insertion(+) 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 -