From 88a2b8ead21017c3c05f9695cc87f1535bd75d62 Mon Sep 17 00:00:00 2001 From: i701 Date: Thu, 17 Apr 2025 14:11:46 +0500 Subject: [PATCH] Enhance CreateTemporaryUserView to check for existing users in both TemporaryUser and User models --- api/views.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/api/views.py b/api/views.py index 012b360..0870262 100644 --- a/api/views.py +++ b/api/views.py @@ -103,24 +103,30 @@ class CreateTemporaryUserView(generics.CreateAPIView): firstname = request.data.get("firstname") lastname = request.data.get("lastname") - if TemporaryUser.objects.filter(t_mobile=mobile).exists(): + if ( + TemporaryUser.objects.filter(t_mobile=mobile).exists() + or User.objects.filter(mobile=mobile).exists() + ): return Response({"message": ErrorMessages.MOBILE_EXISTS}, status=400) - - if TemporaryUser.objects.filter(t_username=username).exists(): + if ( + TemporaryUser.objects.filter(t_username=username).exists() + or User.objects.filter(username=username).exists() + ): return Response({"message": ErrorMessages.USERNAME_EXISTS}, status=400) - - if TemporaryUser.objects.filter(t_id_card=id_card).exists(): + if ( + TemporaryUser.objects.filter(t_id_card=id_card).exists() + or User.objects.filter(id_card=id_card).exists() + ): + return Response({"message": "ID card already exists."}, status=400) + if ( + TemporaryUser.objects.filter(t_id_card=id_card).exists() + or User.objects.filter(id_card=id_card).exists() + ): return Response({"message": ErrorMessages.ID_CARD_EXISTS}, status=400) - if id_card and not re.match(ID_CARD_PATTERN, id_card): return Response({"message": ErrorMessages.INVALID_ID_CARD}, status=400) - - if TemporaryUser.objects.filter(t_id_card=id_card).exists(): - return Response({"message": "ID card already exists."}, status=400) - if mobile is None or not re.match(MOBILE_PATTERN, mobile): return Response({"message": ErrorMessages.INVALID_MOBILE}, status=400) - if acc_no is None or not re.match(ACCOUNT_NUMBER_PATTERN, acc_no): return Response({"message": ErrorMessages.INVALID_ACCOUNT}, status=400)