Update OTP generation interval, enhance SMS sending functionality, and add age validation for temporary user registration
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 1m50s

This commit is contained in:
2025-04-19 16:18:45 +05:00
parent f77779a84f
commit c006525aaa
5 changed files with 92 additions and 15 deletions

View File

@ -19,6 +19,8 @@ from api.serializers import (
TemporaryUserSerializer,
)
from django.shortcuts import get_object_or_404
from django.utils import timezone
from datetime import timedelta
# knox imports
from knox.views import LoginView as KnoxLoginView
@ -52,6 +54,7 @@ class ErrorMessages:
ID_CARD_EXISTS = "ID card already exists."
INVALID_MOBILE = "Please enter a valid mobile number."
INVALID_ACCOUNT = "Please enter a valid account number."
UNDERAGE_ERROR = "You must be 18 and above to signup."
class UpdateUserWalletView(generics.UpdateAPIView):
@ -105,6 +108,23 @@ class CreateTemporaryUserView(generics.CreateAPIView):
firstname = request.data.get("firstname")
lastname = request.data.get("lastname")
current_date = timezone.now()
try:
dob = timezone.datetime.strptime(dob, "%Y-%m-%d").date()
except ValueError:
return Response(
{"message": "Invalid date format for DOB. Use YYYY-MM-DD."}, status=400
)
age_from_dob = (
current_date.year
- dob.year
- ((current_date.month, current_date.day) < (dob.month, dob.day))
)
if age_from_dob < 18:
return Response({"message": ErrorMessages.UNDERAGE_ERROR}, status=400)
if (
TemporaryUser.objects.filter(t_mobile=mobile).exists()
or User.objects.filter(mobile=mobile).exists()
@ -162,11 +182,13 @@ class CreateTemporaryUserView(generics.CreateAPIView):
t_terms_accepted=terms_accepted,
t_policy_accepted=policy_accepted,
)
otp_expiry = timezone.now() + timedelta(minutes=3)
formatted_time = otp_expiry.strftime("%d/%m/%Y %H:%M:%S")
otp = temp_user.generate_otp()
send_otp(
temp_user.t_mobile,
otp,
f"Your Registration SARLink OTP is: {otp}, valid for 30 seconds. Please do not share it with anyone.",
f"Your Registration SARLink OTP: {otp}. \nExpires at {formatted_time}. \n\n- SAR Link",
)
serializer = self.get_serializer(temp_user)
headers = self.get_success_headers(serializer.data)