-
-
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.
also, catch exceptions to be sure to clear request in context
- Loading branch information
Showing
1 changed file
with
10 additions
and
7 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
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: | ||
raise | ||
finally: | ||
del HistoricalRecords.context.request | ||
return response |