feat: 100% async - native async serializers (adata/asave/acreate), async cache (redis.asyncio), view call-site migration
This commit is contained in:
+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,
|
||||
|
||||
Reference in New Issue
Block a user