2020-02-13 06:15:00 +08:00
|
|
|
import asyncio
|
2017-02-18 08:45:34 +08:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
2013-05-16 07:14:28 +08:00
|
|
|
from django.core.exceptions import SuspiciousOperation
|
2013-05-19 23:55:12 +08:00
|
|
|
from django.db import connection, transaction
|
2013-03-06 18:12:24 +08:00
|
|
|
from django.http import HttpResponse, StreamingHttpResponse
|
2014-11-22 04:47:46 +08:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2013-03-06 18:12:24 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
def regular(request):
|
|
|
|
return HttpResponse(b"regular content")
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2018-11-24 08:19:02 +08:00
|
|
|
def no_response(request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NoResponse:
|
|
|
|
def __call__(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
def streaming(request):
|
|
|
|
return StreamingHttpResponse([b"streaming", b" ", b"content"])
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
def in_transaction(request):
|
|
|
|
return HttpResponse(str(connection.in_atomic_block))
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-05-19 23:55:12 +08:00
|
|
|
@transaction.non_atomic_requests
|
2013-03-06 18:12:24 +08:00
|
|
|
def not_in_transaction(request):
|
|
|
|
return HttpResponse(str(connection.in_atomic_block))
|
2013-05-16 07:14:28 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-05-16 07:14:28 +08:00
|
|
|
def suspicious(request):
|
|
|
|
raise SuspiciousOperation('dubious')
|
2014-11-22 04:47:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def malformed_post(request):
|
|
|
|
request.POST
|
|
|
|
return HttpResponse()
|
2016-04-27 01:43:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
def httpstatus_enum(request):
|
|
|
|
return HttpResponse(status=HTTPStatus.OK)
|
2020-02-13 06:15:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
async def async_regular(request):
|
|
|
|
return HttpResponse(b'regular content')
|
|
|
|
|
|
|
|
|
2020-04-08 02:32:54 +08:00
|
|
|
class CoroutineClearingView:
|
|
|
|
def __call__(self, request):
|
|
|
|
"""Return an unawaited coroutine (common error for async views)."""
|
|
|
|
# Store coroutine to suppress 'unawaited' warning message
|
|
|
|
self._unawaited_coroutine = asyncio.sleep(0)
|
|
|
|
return self._unawaited_coroutine
|
|
|
|
|
2020-04-09 17:56:01 +08:00
|
|
|
def __del__(self):
|
|
|
|
self._unawaited_coroutine.close()
|
|
|
|
|
2020-04-08 02:32:54 +08:00
|
|
|
|
|
|
|
async_unawaited = CoroutineClearingView()
|