-
-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use new style middleware and catch exceptions to be sure to clear req…
…uest in context (#1188) * use new style middleware also, catch exceptions to be sure to clear request in context * Raise exception variable for explicitness * Test HistoricalRecords.context.request is deleted * Updated docs for #1188 Added a changelog entry, and removed the "Using django-webtest with Middleware" section in `common_issues.rst`, as it's not an issue anymore now that `HistoricalRecords.context.request` is deleted in all cases (due to using a `finally` block). * Removed deleting request variable in admin tests This code was added in 341aec1 - when `HistoryRequestMiddleware` didn't delete the `HistoricalRecords.context.request` variable. The middleware class does now, so this can be removed. * Refactored HistoricalRecords.context.request test Now it also tests that the `request` attribute of `HistoricalRecords.context` actually exists while handling each request. --------- Co-authored-by: Anders <[email protected]>
- Loading branch information
Showing
7 changed files
with
56 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
from django.utils.deprecation import MiddlewareMixin | ||
|
||
from .models import HistoricalRecords | ||
|
||
|
||
class HistoryRequestMiddleware(MiddlewareMixin): | ||
class HistoryRequestMiddleware: | ||
"""Expose request to HistoricalRecords. | ||
This middleware sets request as a local context/thread variable, making it | ||
available to the model-level utilities to allow tracking of the authenticated user | ||
making a change. | ||
""" | ||
|
||
def process_request(self, request): | ||
HistoricalRecords.context.request = request | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def process_response(self, request, response): | ||
if hasattr(HistoricalRecords.context, "request"): | ||
def __call__(self, request): | ||
HistoricalRecords.context.request = request | ||
try: | ||
response = self.get_response(request) | ||
except Exception as e: | ||
raise e | ||
finally: | ||
del HistoricalRecords.context.request | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,12 +60,6 @@ class AdminSiteTest(TestCase): | |
def setUp(self): | ||
self.user = User.objects.create_superuser("user_login", "[email protected]", "pass") | ||
|
||
def tearDown(self): | ||
try: | ||
del HistoricalRecords.context.request | ||
except AttributeError: | ||
pass | ||
|
||
def login(self, user=None, superuser=None): | ||
user = user or self.user | ||
if superuser is not None: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters