电话验证后保存用户详细信息

时间:2019-12-03 14:21:10

标签: python django django-rest-framework django-views

我的项目中有电话验证。它将otp发送给用户,他们需要进行验证。发送并验证otp后,应将用户数据保存在DB中。但是我在核实他们的电话之前正在保存,但我不知道该怎么做。有人可以帮忙吗?

这是我的代码

views.py

class RegisterApi(views.APIView):
    permission_classes = (AllowAny,)

    def post(self, request, *args, **kwargs):
        phone_number = request.data.get('phone', False)
        first_name = request.data.get('first_name', False)
        last_name = request.data.get('last_name', False)
        password = request.data.get('password', False)

        if phone_number and first_name and last_name and password:
            phone = str(phone_number)
            user = User.objects.filter(phone__iexact=phone)
            if user.exists():
                return Response({
                    'status': 'Fail',
                    'detail': 'Phone number is already existed'
                })
            else:
                key = send_otp(phone)
                if key:
                    old = OTPModel.objects.filter(phone__iexact=phone)
                    if old.exists():
                        old = old.first()
                        count = old.count
                        if count > 5:
                            return Response({
                                'status': 'Fail',
                                'detail': 'Limit Exceeded'
                            })

                        old.count = count + 1
                        old.save()
                        return Response({
                            'status': 'True',
                            'detail': 'OTP resent',
                            'response': response
                        })


                    else:
                        OTPModel.objects.create(
                            phone=phone,
                            otp=key
                        )


                        serializer = UserSerializer(data=request.data)
                        serializer.is_valid(raise_exception=True)
                        serializer.save()
                        return Response({
                            'status': 'True',
                            'detail': 'OTP sent successfully',
                            'account': serializer.data
                        })

                else:
                    return Response({
                        'status': 'Fail',
                        'detail': 'Error in sending otp'
                    })

        else:
            return Response({
                'status': False,
                'detail': 'Phone number is not entered'
            })

要验证otp,请使用此代码。

class ValidateOTP(views.APIView):

    def post(self, request, *args, **kwargs):
        phone = request.data.get('phone', False)
        otp_sent = request.data.get('otp', False)

        if phone and otp_sent:
            old = OTPModel.objects.filter(phone__iexact=phone)
            if old.exists():
                old = old.first()
                otp = old.otp

                if str(otp_sent) == str(otp):
                    old.verified = True
                    old.save()
                    return Response({
                        'status': 'True',
                        'detail': 'OTP verified successfully'
                    })
                else:
                    return Response({
                        'status': 'False',
                        'detail': 'OTP is incorrect'
                    })

一切正常,但是可以在验证之前保存用户详细信息。验证后我需要保存。我该如何解决这个问题?预先感谢!

1 个答案:

答案 0 :(得分:0)

您将通过if语句返回。也许可以:

if condition:

    store details
    return

而不是:

if condition:

    return