feat: ADRF async views (phase1) + native async serializers (phase2) + async cache infra
This commit is contained in:
+14
-14
@@ -3,7 +3,7 @@ import re
|
||||
from django.http import HttpResponseRedirect, HttpResponseNotFound, HttpResponseGone
|
||||
from django.utils import timezone
|
||||
from rest_framework.permissions import AllowAny, IsAuthenticated
|
||||
from rest_framework.views import APIView
|
||||
from adrf.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
@@ -54,7 +54,7 @@ class ShortUrlShortenView(APIView):
|
||||
),
|
||||
responses={200: success_response, 400: error_response, 409: error_response}
|
||||
)
|
||||
def post(self, request):
|
||||
async def post(self, request):
|
||||
url = request.data.get('url', '').strip()
|
||||
custom_code = request.data.get('custom_code', '').strip() or None
|
||||
expire_days = request.data.get('expire_days')
|
||||
@@ -71,17 +71,17 @@ class ShortUrlShortenView(APIView):
|
||||
{"code": 400, "message": "自定义短码仅允许字母、数字、连字符、下划线,长度3-20字符"},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
if ShortUrl.objects.filter(code=custom_code).exists():
|
||||
if await ShortUrl.objects.filter(code=custom_code).aexists():
|
||||
return Response(
|
||||
{"code": 409, "message": "该短码已被使用,请更换"},
|
||||
status=status.HTTP_409_CONFLICT
|
||||
)
|
||||
code = custom_code
|
||||
else:
|
||||
last = ShortUrl.objects.order_by('-id').first()
|
||||
last = await ShortUrl.objects.order_by('-id').afirst()
|
||||
next_id = (last.id + 1) if last else 1
|
||||
code = encode_base62(next_id)
|
||||
while ShortUrl.objects.filter(code=code).exists():
|
||||
while await ShortUrl.objects.filter(code=code).aexists():
|
||||
next_id += 1
|
||||
code = encode_base62(next_id)
|
||||
|
||||
@@ -96,7 +96,7 @@ class ShortUrlShortenView(APIView):
|
||||
|
||||
user = request.user if request.user.is_authenticated else None
|
||||
|
||||
short_url = ShortUrl.objects.create(
|
||||
short_url = await ShortUrl.objects.acreate(
|
||||
code=code,
|
||||
original_url=url,
|
||||
custom_code=custom_code,
|
||||
@@ -129,9 +129,9 @@ class ShortUrlInfoView(APIView):
|
||||
operation_description='通过短码查询短链接的详细信息',
|
||||
responses={200: success_response, 404: error_response}
|
||||
)
|
||||
def get(self, request, code):
|
||||
async def get(self, request, code):
|
||||
try:
|
||||
short_url = ShortUrl.objects.get(code=code)
|
||||
short_url = await ShortUrl.objects.aget(code=code)
|
||||
except ShortUrl.DoesNotExist:
|
||||
return Response(
|
||||
{"code": 404, "message": "短链接不存在"},
|
||||
@@ -157,16 +157,16 @@ class ShortUrlInfoView(APIView):
|
||||
class ShortUrlRedirectView(APIView):
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
def get(self, request, code):
|
||||
async def get(self, request, code):
|
||||
try:
|
||||
short_url = ShortUrl.objects.get(code=code)
|
||||
short_url = await ShortUrl.objects.aget(code=code)
|
||||
except ShortUrl.DoesNotExist:
|
||||
return HttpResponseNotFound('<h1>404 - 短链接不存在</h1>')
|
||||
|
||||
if short_url.expire_at and short_url.expire_at < timezone.now():
|
||||
return HttpResponseGone('<h1>410 - 短链接已过期</h1>')
|
||||
|
||||
ShortUrl.objects.filter(pk=short_url.pk).update(click_count=short_url.click_count + 1)
|
||||
await ShortUrl.objects.filter(pk=short_url.pk).aupdate(click_count=short_url.click_count + 1)
|
||||
|
||||
return HttpResponseRedirect(short_url.original_url)
|
||||
|
||||
@@ -180,7 +180,7 @@ class ShortUrlListView(APIView):
|
||||
operation_description='分页返回当前登录用户创建的所有短链接',
|
||||
responses={200: success_response, 401: unauthorized_response}
|
||||
)
|
||||
def get(self, request):
|
||||
async def get(self, request):
|
||||
queryset = ShortUrl.objects.filter(creator=request.user).order_by('-created_at')
|
||||
|
||||
page = int(request.GET.get('page', 1))
|
||||
@@ -189,8 +189,8 @@ class ShortUrlListView(APIView):
|
||||
|
||||
start = (page - 1) * page_size
|
||||
end = start + page_size
|
||||
total = queryset.count()
|
||||
results = queryset[start:end]
|
||||
total = await queryset.acount()
|
||||
results = [item async for item in queryset[start:end]]
|
||||
|
||||
data = [{
|
||||
"code": item.code,
|
||||
|
||||
Reference in New Issue
Block a user