2022-12-20 11:10:48 +01:00
|
|
|
from asgiref.sync import iscoroutinefunction, markcoroutinefunction
|
2021-04-06 08:03:43 +02:00
|
|
|
|
2016-06-14 09:41:58 +02:00
|
|
|
from django.http import Http404, HttpResponse
|
|
|
|
from django.template import engines
|
2016-07-19 10:45:32 +02:00
|
|
|
from django.template.response import TemplateResponse
|
2020-02-12 23:15:00 +01:00
|
|
|
from django.utils.decorators import (
|
|
|
|
async_only_middleware,
|
|
|
|
sync_and_async_middleware,
|
|
|
|
sync_only_middleware,
|
|
|
|
)
|
2015-06-05 15:30:03 +02:00
|
|
|
|
2016-06-14 09:41:58 +02:00
|
|
|
log = []
|
2015-06-05 15:30:03 +02:00
|
|
|
|
2015-11-07 16:12:37 +01:00
|
|
|
|
2017-01-19 08:39:46 +01:00
|
|
|
class BaseMiddleware:
|
2016-06-14 09:41:58 +02:00
|
|
|
def __init__(self, get_response):
|
|
|
|
self.get_response = get_response
|
2022-12-20 11:10:48 +01:00
|
|
|
if iscoroutinefunction(self.get_response):
|
|
|
|
markcoroutinefunction(self)
|
2016-06-14 09:41:58 +02:00
|
|
|
|
|
|
|
def __call__(self, request):
|
|
|
|
return self.get_response(request)
|
|
|
|
|
|
|
|
|
|
|
|
class ProcessExceptionMiddleware(BaseMiddleware):
|
2015-06-05 15:30:03 +02:00
|
|
|
def process_exception(self, request, exception):
|
|
|
|
return HttpResponse("Exception caught")
|
2016-06-14 09:41:58 +02:00
|
|
|
|
|
|
|
|
2020-02-12 23:15:00 +01:00
|
|
|
@async_only_middleware
|
|
|
|
class AsyncProcessExceptionMiddleware(BaseMiddleware):
|
|
|
|
async def process_exception(self, request, exception):
|
|
|
|
return HttpResponse("Exception caught")
|
|
|
|
|
|
|
|
|
2016-06-14 09:41:58 +02:00
|
|
|
class ProcessExceptionLogMiddleware(BaseMiddleware):
|
|
|
|
def process_exception(self, request, exception):
|
|
|
|
log.append("process-exception")
|
|
|
|
|
|
|
|
|
|
|
|
class ProcessExceptionExcMiddleware(BaseMiddleware):
|
|
|
|
def process_exception(self, request, exception):
|
|
|
|
raise Exception("from process-exception")
|
|
|
|
|
|
|
|
|
|
|
|
class ProcessViewMiddleware(BaseMiddleware):
|
|
|
|
def process_view(self, request, view_func, view_args, view_kwargs):
|
|
|
|
return HttpResponse("Processed view %s" % view_func.__name__)
|
|
|
|
|
|
|
|
|
2020-02-12 23:15:00 +01:00
|
|
|
@async_only_middleware
|
|
|
|
class AsyncProcessViewMiddleware(BaseMiddleware):
|
|
|
|
async def process_view(self, request, view_func, view_args, view_kwargs):
|
|
|
|
return HttpResponse("Processed view %s" % view_func.__name__)
|
|
|
|
|
|
|
|
|
2016-06-14 09:41:58 +02:00
|
|
|
class ProcessViewNoneMiddleware(BaseMiddleware):
|
|
|
|
def process_view(self, request, view_func, view_args, view_kwargs):
|
|
|
|
log.append("processed view %s" % view_func.__name__)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-07-19 10:45:32 +02:00
|
|
|
class ProcessViewTemplateResponseMiddleware(BaseMiddleware):
|
|
|
|
def process_view(self, request, view_func, view_args, view_kwargs):
|
|
|
|
template = engines["django"].from_string(
|
|
|
|
"Processed view {{ view }}{% for m in mw %}\n{{ m }}{% endfor %}"
|
|
|
|
)
|
|
|
|
return TemplateResponse(
|
|
|
|
request,
|
|
|
|
template,
|
|
|
|
{"mw": [self.__class__.__name__], "view": view_func.__name__},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-06-14 09:41:58 +02:00
|
|
|
class TemplateResponseMiddleware(BaseMiddleware):
|
|
|
|
def process_template_response(self, request, response):
|
2016-07-19 10:45:32 +02:00
|
|
|
response.context_data["mw"].append(self.__class__.__name__)
|
2016-06-14 09:41:58 +02:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
2020-02-12 23:15:00 +01:00
|
|
|
@async_only_middleware
|
|
|
|
class AsyncTemplateResponseMiddleware(BaseMiddleware):
|
|
|
|
async def process_template_response(self, request, response):
|
|
|
|
response.context_data["mw"].append(self.__class__.__name__)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2016-06-14 09:41:58 +02:00
|
|
|
class LogMiddleware(BaseMiddleware):
|
|
|
|
def __call__(self, request):
|
|
|
|
response = self.get_response(request)
|
|
|
|
log.append((response.status_code, response.content))
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2018-11-20 16:38:42 +01:00
|
|
|
class NoTemplateResponseMiddleware(BaseMiddleware):
|
|
|
|
def process_template_response(self, request, response):
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-02-12 23:15:00 +01:00
|
|
|
@async_only_middleware
|
|
|
|
class AsyncNoTemplateResponseMiddleware(BaseMiddleware):
|
|
|
|
async def process_template_response(self, request, response):
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-06-14 09:41:58 +02:00
|
|
|
class NotFoundMiddleware(BaseMiddleware):
|
|
|
|
def __call__(self, request):
|
|
|
|
raise Http404("not found")
|
2020-02-12 23:15:00 +01:00
|
|
|
|
|
|
|
|
2020-03-31 12:09:17 +02:00
|
|
|
class PaymentMiddleware(BaseMiddleware):
|
2020-02-12 23:15:00 +01:00
|
|
|
def __call__(self, request):
|
|
|
|
response = self.get_response(request)
|
2020-03-31 12:09:17 +02:00
|
|
|
response.status_code = 402
|
2020-02-12 23:15:00 +01:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@async_only_middleware
|
2020-03-31 12:09:17 +02:00
|
|
|
def async_payment_middleware(get_response):
|
2020-02-12 23:15:00 +01:00
|
|
|
async def middleware(request):
|
|
|
|
response = await get_response(request)
|
2020-03-31 12:09:17 +02:00
|
|
|
response.status_code = 402
|
2020-02-12 23:15:00 +01:00
|
|
|
return response
|
|
|
|
|
|
|
|
return middleware
|
|
|
|
|
|
|
|
|
|
|
|
@sync_and_async_middleware
|
|
|
|
class SyncAndAsyncMiddleware(BaseMiddleware):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@sync_only_middleware
|
2020-03-31 12:09:17 +02:00
|
|
|
class DecoratedPaymentMiddleware(PaymentMiddleware):
|
2020-02-12 23:15:00 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NotSyncOrAsyncMiddleware(BaseMiddleware):
|
|
|
|
"""Middleware that is deliberately neither sync or async."""
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2020-02-12 23:15:00 +01:00
|
|
|
sync_capable = False
|
|
|
|
async_capable = False
|
|
|
|
|
|
|
|
def __call__(self, request):
|
|
|
|
return self.get_response(request)
|