✨ Feat: [#25] usetype에 따라 인증 발송 및 인증번호 체크 로직 구현
This commit is contained in:
@@ -1,3 +1,56 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import get_object_or_404
|
||||||
|
|
||||||
# Create your views here.
|
from rest_framework.views import APIView
|
||||||
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
||||||
|
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from django.core.mail import EmailMessage
|
||||||
|
from django.db import transaction
|
||||||
|
|
||||||
|
from .serializers import *
|
||||||
|
from .services import *
|
||||||
|
from common.models.choiceModels import CertificateCodeUseType
|
||||||
|
from common.utils.codeManger import generate_code
|
||||||
|
|
||||||
|
|
||||||
|
certificate_use_type = {
|
||||||
|
"phone": SmsService,
|
||||||
|
# "email": EmailService
|
||||||
|
}
|
||||||
|
|
||||||
|
class CertificationAPIView(APIView):
|
||||||
|
permission_classes = [AllowAny]
|
||||||
|
|
||||||
|
# 인증 발송
|
||||||
|
@transaction.atomic
|
||||||
|
def post(self, request):
|
||||||
|
use_type = request.query_params.get("type")
|
||||||
|
serv = certificate_use_type[use_type]
|
||||||
|
serializer = CertificateCodeSerializer(data=request.data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
create_code = generate_code(6)
|
||||||
|
if serv.save_certificate_info(use_type, create_code, serializer.validated_data['identifier']):
|
||||||
|
if serv.send(create_code, serializer.validated_data['identifier']):
|
||||||
|
return Response({'message': "success send and save"})
|
||||||
|
else: # 전송 실패
|
||||||
|
return Response({"message": "failed send"})
|
||||||
|
else: # 코드 저장 실패
|
||||||
|
return Response({'message': "failed save"}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
||||||
|
# 인증 확인
|
||||||
|
@transaction.atomic
|
||||||
|
def patch(self, request):
|
||||||
|
use_type = request.query_params.get("type")
|
||||||
|
serv = certificate_use_type[use_type]
|
||||||
|
code = request.data.get('code', None)
|
||||||
|
if not code:
|
||||||
|
return Response({"message": "no code"}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
serializer = CertificateCodeSerializer(data=request.data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
if serv.check_code(use_type, code, serializer.validated_data['identifier']):
|
||||||
|
return Response({"message": "certificated successfully"}, status=status.HTTP_200_OK)
|
||||||
|
return Response({"message": "wrong code, please retry"}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|||||||
Reference in New Issue
Block a user