2020-02-12 22:15:00 +00:00
|
|
|
import asyncio
|
2017-02-18 00:45:34 +00:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
2020-09-07 11:33:47 +00:00
|
|
|
from django.core.exceptions import BadRequest, SuspiciousOperation
|
2013-05-19 15:55:12 +00:00
|
|
|
from django.db import connection, transaction
|
2013-03-06 10:12:24 +00:00
|
|
|
from django.http import HttpResponse, StreamingHttpResponse
|
2014-11-21 20:47:46 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2013-03-06 10:12:24 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-03-06 10:12:24 +00:00
|
|
|
def regular(request):
|
|
|
|
return HttpResponse(b"regular content")
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2018-11-24 00:19:02 +00:00
|
|
|
def no_response(request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NoResponse:
|
|
|
|
def __call__(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-03-06 10:12:24 +00:00
|
|
|
def streaming(request):
|
|
|
|
return StreamingHttpResponse([b"streaming", b" ", b"content"])
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-03-06 10:12:24 +00:00
|
|
|
def in_transaction(request):
|
|
|
|
return HttpResponse(str(connection.in_atomic_block))
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-05-19 15:55:12 +00:00
|
|
|
@transaction.non_atomic_requests
|
2013-03-06 10:12:24 +00:00
|
|
|
def not_in_transaction(request):
|
|
|
|
return HttpResponse(str(connection.in_atomic_block))
|
2013-05-15 23:14:28 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2022-12-03 02:04:42 +00:00
|
|
|
@transaction.non_atomic_requests(using=None)
|
|
|
|
def not_in_transaction_using_none(request):
|
|
|
|
return HttpResponse(str(connection.in_atomic_block))
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.non_atomic_requests(using="incorrect")
|
|
|
|
def not_in_transaction_using_text(request):
|
|
|
|
return HttpResponse(str(connection.in_atomic_block))
|
|
|
|
|
|
|
|
|
2020-09-07 11:33:47 +00:00
|
|
|
def bad_request(request):
|
|
|
|
raise BadRequest()
|
|
|
|
|
|
|
|
|
2013-05-15 23:14:28 +00:00
|
|
|
def suspicious(request):
|
2022-02-03 19:24:19 +00:00
|
|
|
raise SuspiciousOperation("dubious")
|
2014-11-21 20:47:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def malformed_post(request):
|
|
|
|
request.POST
|
|
|
|
return HttpResponse()
|
2016-04-26 17:43:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def httpstatus_enum(request):
|
|
|
|
return HttpResponse(status=HTTPStatus.OK)
|
2020-02-12 22:15:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_regular(request):
|
2022-02-03 19:24:19 +00:00
|
|
|
return HttpResponse(b"regular content")
|
2020-02-12 22:15:00 +00:00
|
|
|
|
|
|
|
|
2023-02-15 23:16:51 +00:00
|
|
|
async def async_streaming(request):
|
|
|
|
async def async_streaming_generator():
|
|
|
|
yield b"streaming"
|
|
|
|
yield b" "
|
|
|
|
yield b"content"
|
|
|
|
|
|
|
|
return StreamingHttpResponse(async_streaming_generator())
|
|
|
|
|
|
|
|
|
2020-04-07 18:32:54 +00: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 09:56:01 +00:00
|
|
|
def __del__(self):
|
2022-02-15 09:31:32 +00:00
|
|
|
try:
|
|
|
|
self._unawaited_coroutine.close()
|
|
|
|
except AttributeError:
|
|
|
|
# View was never called.
|
|
|
|
pass
|
2020-04-09 09:56:01 +00:00
|
|
|
|
2020-04-07 18:32:54 +00:00
|
|
|
|
|
|
|
async_unawaited = CoroutineClearingView()
|