Skip to content

Commit

Permalink
use new style middleware
Browse files Browse the repository at this point in the history
also, catch exceptions to be sure to clear request in context
  • Loading branch information
manelclos authored Jun 12, 2023
1 parent 705a2e1 commit ec41355
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions simple_history/middleware.py
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

0 comments on commit ec41355

Please sign in to comment.