From cb5bcaa788a254b25403875dc87f07a62e0b6eef Mon Sep 17 00:00:00 2001 From: George J Padayatti Date: Thu, 30 Nov 2023 14:25:10 +0530 Subject: [PATCH] Upd: Delete by mobile number Signed-off-by: George J Padayatti --- otp_auth/user.py | 22 +++++++++++++--------- otp_auth/views.py | 31 ++++++++++--------------------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/otp_auth/user.py b/otp_auth/user.py index 4c3abfc..fd3768f 100644 --- a/otp_auth/user.py +++ b/otp_auth/user.py @@ -1,15 +1,17 @@ -import typing import string +import typing +from datetime import timedelta +from hashlib import sha256 + from django.contrib.auth import get_user_model from django.contrib.auth.models import AbstractBaseUser from django.utils import timezone from django.utils.crypto import get_random_string -from otp_auth.settings import api_settings -from otp_auth.models import OTP -from hashlib import sha256 -from datetime import timedelta from rest_framework.authtoken.models import Token +from otp_auth.models import OTP +from otp_auth.settings import api_settings + def generate_otp(max_length: int): """ @@ -93,12 +95,14 @@ def delete_otp(user: AbstractBaseUser): """Delete OTP of the user""" OTP.objects.filter(user=user).delete() -def delete_user(token: str) -> bool: +def delete_user(mobile_number: str) -> bool: """Delete user""" try: - user = Token.objects.get(key=token).user - user.delete() - return True + user, exists = get_user_by_mobile_number(mobile_number) + if exists: + user.delete() + return True + return False except Token.DoesNotExist: return False diff --git a/otp_auth/views.py b/otp_auth/views.py index 7a60f19..62d85d9 100644 --- a/otp_auth/views.py +++ b/otp_auth/views.py @@ -1,24 +1,15 @@ -from rest_framework import status, authentication +from rest_framework import authentication, status from rest_framework.decorators import api_view, authentication_classes from rest_framework.response import Response -from otp_auth.serializers import ( - RegisterUserSerializer, - LoginUserSerializer, - VerifyOTPSerializer, - ValidateMobileNumberSerializer -) from twilio.base.exceptions import TwilioRestException -from otp_auth.user import ( - get_user_by_mobile_number, - is_user_active, - send_otp_verification_code, - get_otp_by_otp_hash, - is_otp_expired, - issue_token, - delete_token, - delete_otp, - delete_user -) + +from otp_auth.serializers import (LoginUserSerializer, RegisterUserSerializer, + ValidateMobileNumberSerializer, + VerifyOTPSerializer) +from otp_auth.user import (delete_otp, delete_token, delete_user, + get_otp_by_otp_hash, get_user_by_mobile_number, + is_otp_expired, is_user_active, issue_token, + send_otp_verification_code) @api_view(['POST']) @@ -133,10 +124,8 @@ def validate_mobile_number(request): return Response(response_data, status=status.HTTP_200_OK) @api_view(['POST']) -@authentication_classes([authentication.TokenAuthentication]) def delete_user_account(request): - token = request.headers.get("Authorization").split("Bearer Token ")[1] - is_deleted = delete_user(token) + is_deleted = delete_user(request.data.get("mobile_number")) if is_deleted: return Response(status=status.HTTP_204_NO_CONTENT) else: