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