feat: 100% async - native async serializers (adata/asave/acreate), async cache (redis.asyncio), view call-site migration
This commit is contained in:
@@ -90,11 +90,11 @@ class BlacklistAddAPIView(APIView):
|
||||
status_code=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
blacklist = await sync_to_async(serializer.save)()
|
||||
blacklist = await serializer.asave()
|
||||
result_serializer = BlacklistSerializer(blacklist)
|
||||
|
||||
return create_standardized_response(
|
||||
data=result_serializer.data,
|
||||
data=await result_serializer.adata,
|
||||
code=ResponseCode.SUCCESS,
|
||||
status_code=status.HTTP_201_CREATED
|
||||
)
|
||||
|
||||
+3
-3
@@ -102,7 +102,7 @@ class SendChangeEmailCodeAPIView(APIView):
|
||||
|
||||
try:
|
||||
# save() 内部触发验证码邮件发送(Celery/cache/SMTP 链路),线程池兜底
|
||||
email = await sync_to_async(serializer.save)()
|
||||
email = await serializer.asave()
|
||||
await sync_to_async(reset_failures)(operation, identifier)
|
||||
return create_standardized_response(
|
||||
data={'email_sent': True},
|
||||
@@ -157,11 +157,11 @@ class ChangeEmailAPIView(APIView):
|
||||
)
|
||||
|
||||
try:
|
||||
updated_user = await sync_to_async(serializer.save)()
|
||||
updated_user = await serializer.asave()
|
||||
user_serializer = UserSerializer(updated_user)
|
||||
|
||||
return create_standardized_response(
|
||||
data={'user': user_serializer.data},
|
||||
data={'user': await user_serializer.adata},
|
||||
code=ResponseCode.EMAIL_CHANGED,
|
||||
status_code=status.HTTP_200_OK
|
||||
)
|
||||
|
||||
@@ -76,7 +76,7 @@ class LoginRecordListAPIView(APIView):
|
||||
|
||||
return create_standardized_response(
|
||||
data={
|
||||
'records': serializer.data,
|
||||
'records': await serializer.adata,
|
||||
'total': total,
|
||||
'page': page,
|
||||
'page_size': page_size,
|
||||
|
||||
+3
-3
@@ -41,7 +41,7 @@ class SendPhoneCodeAPIView(APIView):
|
||||
)
|
||||
|
||||
try:
|
||||
phone = await sync_to_async(serializer.save)()
|
||||
phone = await serializer.asave()
|
||||
return create_standardized_response(
|
||||
data={'phone_sent': True},
|
||||
code=ResponseCode.SUCCESS,
|
||||
@@ -81,11 +81,11 @@ class ChangePhoneAPIView(APIView):
|
||||
)
|
||||
|
||||
try:
|
||||
updated_user = await sync_to_async(serializer.save)()
|
||||
updated_user = await serializer.asave()
|
||||
user_serializer = UserSerializer(updated_user)
|
||||
|
||||
return create_standardized_response(
|
||||
data={'user': user_serializer.data},
|
||||
data={'user': await user_serializer.adata},
|
||||
code=ResponseCode.SUCCESS,
|
||||
status_code=status.HTTP_200_OK
|
||||
)
|
||||
|
||||
@@ -81,6 +81,6 @@ class RegionListView(APIView):
|
||||
regions = [r async for r in queryset]
|
||||
serializer = RegionSimpleSerializer(regions, many=True)
|
||||
return Response(
|
||||
{"code": 200, "message": "success", "data": serializer.data},
|
||||
{"code": 200, "message": "success", "data": await serializer.adata},
|
||||
status=status.HTTP_200_OK
|
||||
)
|
||||
|
||||
+18
-17
@@ -2,6 +2,7 @@ import os
|
||||
import uuid
|
||||
import logging
|
||||
|
||||
from utils.async_cache import aget_cache, aset_cache, adelete_cache
|
||||
from asgiref.sync import sync_to_async
|
||||
from utils.email_utils import validate_email_mx
|
||||
from rest_framework.decorators import permission_classes
|
||||
@@ -93,7 +94,7 @@ class SendUserEmailAPIView(APIView):
|
||||
|
||||
if not user_exists_result:
|
||||
code = RandCode.get_digit_characters_code_8()
|
||||
await sync_to_async(default_cache.set)(f"register_{to_email}", code, timeout=600)
|
||||
await aset_cache(f"register_{to_email}", code, timeout=600)
|
||||
|
||||
result = await sync_to_async(submit_task)(send_verification_email_task, to_email, code, 'register')
|
||||
if result is None:
|
||||
@@ -108,7 +109,7 @@ class SendUserEmailAPIView(APIView):
|
||||
)
|
||||
else:
|
||||
code = RandCode.get_digit_characters_code_8()
|
||||
await sync_to_async(default_cache.set)(f"login_{to_email}", code, timeout=600)
|
||||
await aset_cache(f"login_{to_email}", code, timeout=600)
|
||||
|
||||
result = await sync_to_async(submit_task)(send_verification_email_task, to_email, code, 'login')
|
||||
if result is None:
|
||||
@@ -162,7 +163,7 @@ class UserLoginOrRegisterAPIView(APIView):
|
||||
|
||||
if user is None:
|
||||
# Registration flow
|
||||
vcode = await sync_to_async(default_cache.get)(f"register_{to_email}")
|
||||
vcode = await aget_cache(f"register_{to_email}")
|
||||
|
||||
if vcode is None:
|
||||
return create_standardized_error_response(
|
||||
@@ -173,9 +174,9 @@ class UserLoginOrRegisterAPIView(APIView):
|
||||
if code == vcode:
|
||||
user_serializer = UserSerializer(data=request.data)
|
||||
if await sync_to_async(user_serializer.is_valid)():
|
||||
user = await sync_to_async(user_serializer.create_by_email)(request.data)
|
||||
user = await user_serializer.acreate_by_email(request.data)
|
||||
refresh = RefreshToken.for_user(user)
|
||||
user_data = await sync_to_async(_serialize_user)(user)
|
||||
user_data = await UserSerializer(user).adata
|
||||
|
||||
# Prepare response data
|
||||
response_data = {
|
||||
@@ -205,7 +206,7 @@ class UserLoginOrRegisterAPIView(APIView):
|
||||
)
|
||||
else:
|
||||
# Login flow
|
||||
vcode = await sync_to_async(default_cache.get)(f"login_{to_email}")
|
||||
vcode = await aget_cache(f"login_{to_email}")
|
||||
|
||||
if vcode is None:
|
||||
return create_standardized_error_response(
|
||||
@@ -215,7 +216,7 @@ class UserLoginOrRegisterAPIView(APIView):
|
||||
|
||||
if code == vcode:
|
||||
refresh = RefreshToken.for_user(user)
|
||||
user_data = await sync_to_async(_serialize_user)(user)
|
||||
user_data = await UserSerializer(user).adata
|
||||
|
||||
# Prepare response data
|
||||
response_data = {
|
||||
@@ -322,7 +323,7 @@ class ForgotPasswordSendCodeAPIView(APIView):
|
||||
)
|
||||
|
||||
code = RandCode.get_digit_characters_code_8()
|
||||
await sync_to_async(default_cache.set)(f"reset_password_{to_email}", code, timeout=600)
|
||||
await aset_cache(f"reset_password_{to_email}", code, timeout=600)
|
||||
|
||||
result = await sync_to_async(submit_task)(send_reset_password_email_task, to_email, code)
|
||||
if result is None:
|
||||
@@ -433,7 +434,7 @@ class ForgotPasswordResetAPIView(APIView):
|
||||
)
|
||||
|
||||
try:
|
||||
vcode = await sync_to_async(default_cache.get)(f"reset_password_{to_email}")
|
||||
vcode = await aget_cache(f"reset_password_{to_email}")
|
||||
|
||||
if vcode is None:
|
||||
await sync_to_async(record_failure)(operation, to_email)
|
||||
@@ -461,7 +462,7 @@ class ForgotPasswordResetAPIView(APIView):
|
||||
await sync_to_async(user.set_password)(new_password)
|
||||
await user.asave()
|
||||
|
||||
await sync_to_async(default_cache.delete)(f"reset_password_{to_email}")
|
||||
await adelete_cache(f"reset_password_{to_email}")
|
||||
|
||||
await sync_to_async(reset_failures)(operation, to_email)
|
||||
return create_standardized_response(
|
||||
@@ -487,7 +488,7 @@ class UserUpdateAPIView(APIView):
|
||||
)
|
||||
async def get(self, request):
|
||||
user = request.user
|
||||
user_data = await sync_to_async(_serialize_user)(user)
|
||||
user_data = await UserSerializer(user).adata
|
||||
|
||||
return create_standardized_response(
|
||||
data={'user': user_data},
|
||||
@@ -526,8 +527,8 @@ class UserUpdateAPIView(APIView):
|
||||
status_code=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
updated_user = await sync_to_async(serializer.save)()
|
||||
user_data = await sync_to_async(_serialize_user)(updated_user)
|
||||
updated_user = await serializer.asave()
|
||||
user_data = await UserSerializer(updated_user).adata
|
||||
|
||||
try:
|
||||
await sync_to_async(log_event)(
|
||||
@@ -632,8 +633,8 @@ class ChangePasswordAPIView(APIView):
|
||||
status_code=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
user = await sync_to_async(serializer.save)()
|
||||
user_data = await sync_to_async(_serialize_user)(user)
|
||||
user = await serializer.asave()
|
||||
user_data = await UserSerializer(user).adata
|
||||
|
||||
is_new_set = not request.user.has_usable_password() or request.data.get('old_password', '') == ''
|
||||
|
||||
@@ -682,7 +683,7 @@ class UserLoginAPIView(APIView):
|
||||
if user is not None:
|
||||
if user.is_active:
|
||||
refresh = RefreshToken.for_user(user)
|
||||
user_data = await sync_to_async(_serialize_user)(user)
|
||||
user_data = await UserSerializer(user).adata
|
||||
|
||||
response_data = {
|
||||
'user': user_data,
|
||||
@@ -797,7 +798,7 @@ class LoginView(APIView):
|
||||
if user.is_active:
|
||||
await sync_to_async(reset_failures)(operation, identifier)
|
||||
refresh = RefreshToken.for_user(user)
|
||||
user_data = await sync_to_async(_serialize_user)(user)
|
||||
user_data = await UserSerializer(user).adata
|
||||
|
||||
response_data = {
|
||||
'user': user_data,
|
||||
|
||||
@@ -81,7 +81,7 @@ class WalletTransactionsAPIView(APIView):
|
||||
|
||||
return create_standardized_response(
|
||||
data={
|
||||
'transactions': serializer.data,
|
||||
'transactions': await serializer.adata,
|
||||
'total': total,
|
||||
'page': page,
|
||||
'page_size': page_size,
|
||||
|
||||
Reference in New Issue
Block a user