def post(self, request): res_message = "Registration failed" res_detail = "" res_token = "" res_status = status.HTTP_400_BAD_REQUEST user = request.data otp = str(randint(1000, 9999)) password = user['password'] if password is None or password=='': res_detail+='Error in field:Password-This field must not be empty' else: user['password'] = make_password(password) user['otp'] = otp serializer = self.serializer_class(data=user) try: serializer.is_valid(raise_exception=True) except Exception as e: error = serializer.errors error_msg = "" for err in error: error_msg += "Error in field: " + \ str(err) + "- " + str(error[err][0]) + " " res_detail = error_msg else: serializer.save() user = serializer.validated_data payload = { 'email': serializer.validated_data['email'] } otp = send_otp(serializer.validated_data['contact'], otp=otp) token = jwt.encode( payload, settings.SECRET_KEY, algorithm='HS256').decode('UTF-8') res_message = "Registration Successful" res_token = token res_status = status.HTTP_200_OK user_obj = CustomUser.objects.get(email=user['email']) return Response({ "message": res_message, "detail": res_detail, "token": res_token, 'first_name' : user['first_name'], 'last_name' : user['last_name'], 'email' : user['email'], 'verified' : False, 'contact' : user['contact'], 'bquiz_score' : 0, 'user_type' : 'GST', 'linkedin' : 0, 'facebook' : 0, 'applied' : 0, 'id': user_obj.id }, status=res_status)
def resend_otp(request): res_status = status.HTTP_400_BAD_REQUEST user = request.ecelluser otp = user.otp contact = user.contact if otp: duration = user.last_modified if duration<=1200: otp = send_otp(contact, otp=otp) else: otp = send_otp(contact) user.otp = otp user.save() message = "An otp has been sent to your mobile no to reset your password" res_status = status.HTTP_200_OK return Response({ "message": message, }, status=res_status)
def change_contact(request): res_status = status.HTTP_400_BAD_REQUEST req_data = request.data new_contact = req_data['contact'] user = request.ecelluser otp = send_otp(new_contact) user.otp = otp user.contact = new_contact user.verified = False user.save() message = "An otp has been sent to new mobile no." res_status = status.HTTP_200_OK return Response({ "message": message, }, status=res_status)
def post(self, request): res_message = "Registration failed! " res_detail = "" res_token = "" res_status = status.HTTP_400_BAD_REQUEST user = request.data otp = str(randint(1000, 9999)) password = user['password'] if password is None or password == '': res_detail += 'Error in field:Password-This field must not be empty' else: user['password'] = make_password(password) user['otp'] = otp serializer = self.serializer_class(data=user) try: serializer.is_valid(raise_exception=True) except Exception as e: error = serializer.errors error_msg = "" for err in error: error_msg += "Error in field: " + \ str(err) + "- " + str(error[err][0]) + " " res_detail = error_msg else: serializer.save() payload = {'email': serializer.validated_data['email']} otp = send_otp(serializer.validated_data['contact'], otp=otp) token = jwt.encode(payload, settings.SECRET_KEY, algorithm='HS256').decode('UTF-8') res_message = "Registration Successful!" res_token = token res_status = status.HTTP_200_OK return Response( { "message": res_message, "detail": res_detail, "token": res_token }, status=res_status)
def forgot_password(request): res_status = status.HTTP_400_BAD_REQUEST req_data = request.data email = req_data['email'] try: user = CustomUser.objects.get(email=email) print(user) except: message = "Account with this email id doesn't exists. Kindly signup." else: contact = user.contact otp = send_otp(contact) user.otp = otp user.save() message = "An otp has been sent to your mobile no to reset your password" res_status = status.HTTP_200_OK return Response({ "message": message, }, status=res_status)