Clear ThreadLocal request atttribute after response is processed #7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@jedie This pull request updates the
ThreadLocal
middleware so that the thread local reference to therequest
object is cleared when the response is processed by the middleware.Issue Description
This is unlikely to matter in practice since a server will overwrite the previous
_thread_locals.request
when it receives a new request.The use case this is intended to fix is:
f()
is used on both the front end and back end of an application.get_current_{request,user}
is called, it creates a model entry with a foreign key referencing the change the user made.M
is created, or theUser
foreign key is NULL._thread_locals.request
variable. TheUser
object is created as part of the test, then destroyed when the database transaction is rolled back by thetearDown
method.Changes
ThreadLocalMiddleware.process_response
method, which is part of the Django middleware API: https://docs.djangoproject.com/en/1.8/topics/http/middleware/#process-response This method clears therequest
variable if it is defined.ThreadLocal
module's documentation.Testing
I added two views to the example app. One that returns the GET parameters from the current request, and one that simply raises an exception. I added tests that assert:
get_current_request
actually returns the current request). This passed on the master branch, and is just included for completeness.request
reference is cleared. This failed using the code on the master branch.raise_exception
view), therequest
reference is cleared. This failed using the code on the master branch.Thanks for maintaining this library!