feat: ADRF async views (phase1) + native async serializers (phase2) + async cache infra

This commit is contained in:
async-upgrade
2026-09-06 14:26:17 +08:00
parent 9a6577f71e
commit 8f488fcaaa
55 changed files with 2224 additions and 1513 deletions
+16 -13
View File
@@ -1,7 +1,8 @@
from rest_framework.views import APIView
from adrf.views import APIView
from rest_framework.response import Response
from rest_framework import status
from django.db.models import Q
from asgiref.sync import sync_to_async
from ..models import Blacklist, FUser
from ..serializers.user_serializers import BlacklistSerializer
from utils.response_codes import (
@@ -26,7 +27,7 @@ class BlacklistListAPIView(APIView):
],
responses={200: success_response, 401: unauthorized_response},
)
def get(self, request):
async def get(self, request):
queryset = Blacklist.objects.filter(user=request.user)
search = request.query_params.get('search', '')
@@ -39,11 +40,12 @@ class BlacklistListAPIView(APIView):
page = int(request.query_params.get('page', 1))
page_size = int(request.query_params.get('page_size', 10))
total = queryset.count()
total = await queryset.acount()
start = (page - 1) * page_size
end = start + page_size
serializer = BlacklistSerializer(queryset[start:end], many=True)
items = [item async for item in queryset[start:end]]
serializer = BlacklistSerializer(items, many=True)
return create_standardized_response(
data={
@@ -66,13 +68,14 @@ class BlacklistAddAPIView(APIView):
request_body=BlacklistSerializer,
responses={201: success_response, 400: error_response, 401: unauthorized_response},
)
def post(self, request):
async def post(self, request):
serializer = BlacklistSerializer(
data=request.data,
context={'request': request}
)
if not serializer.is_valid():
# 校验器内部含同步 ORM 查询(validate_blocked_user_id / validate),线程池兜底
if not await sync_to_async(serializer.is_valid)():
errors = serializer.errors
first_error = ''
for field, msgs in errors.items():
@@ -87,7 +90,7 @@ class BlacklistAddAPIView(APIView):
status_code=status.HTTP_400_BAD_REQUEST
)
blacklist = serializer.save()
blacklist = await sync_to_async(serializer.save)()
result_serializer = BlacklistSerializer(blacklist)
return create_standardized_response(
@@ -107,11 +110,11 @@ class BlacklistCheckAPIView(APIView):
],
responses={200: success_response, 401: unauthorized_response},
)
def get(self, request, user_id):
is_blocked = Blacklist.objects.filter(
async def get(self, request, user_id):
is_blocked = await Blacklist.objects.filter(
user=request.user,
blocked_user_id=user_id
).exists()
).aexists()
return create_standardized_response(
data={'is_blocked': is_blocked},
code=ResponseCode.SUCCESS,
@@ -129,9 +132,9 @@ class BlacklistRemoveAPIView(APIView):
],
responses={200: success_response, 401: unauthorized_response, 404: not_found_response},
)
def delete(self, request, pk):
async def delete(self, request, pk):
try:
blacklist = Blacklist.objects.get(id=pk, user=request.user)
blacklist = await Blacklist.objects.aget(id=pk, user=request.user)
except Blacklist.DoesNotExist:
return create_standardized_error_response(
code=ResponseCode.NOT_FOUND,
@@ -139,7 +142,7 @@ class BlacklistRemoveAPIView(APIView):
status_code=status.HTTP_404_NOT_FOUND
)
blacklist.delete()
await blacklist.adelete()
return create_standardized_response(
data={'deleted': True},