feat: implement user registration, profile management, and WhatsApp OTP authentication views
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 24s

This commit is contained in:
vickytechkey 2026-10-04 10:23:42 +05:30
parent 653f0a219e
commit dc60cf95b2
2 changed files with 33 additions and 8 deletions

View file

@ -1,5 +1,7 @@
import logging
import requests
import json
import urllib.request
import urllib.error
from django.conf import settings
from django.core.cache import cache
from rest_framework import status, permissions
@ -70,13 +72,21 @@ class SendWhatsAppOTPView(APIView):
}
try:
res = requests.post(whatsapp_service_url, json=payload, timeout=10)
try:
res_data = res.json()
except Exception:
res_data = {"text": res.text}
req_data = json.dumps(payload).encode('utf-8')
req = urllib.request.Request(
whatsapp_service_url,
data=req_data,
headers={'Content-Type': 'application/json', 'User-Agent': 'TradhoxBackend/1.0'}
)
with urllib.request.urlopen(req, timeout=10) as resp:
status_code = resp.status
body_bytes = resp.read()
try:
res_data = json.loads(body_bytes.decode('utf-8'))
except Exception:
res_data = {"text": body_bytes.decode('utf-8', errors='ignore')}
if res.status_code == 200 and res_data.get('status') != 'error':
if status_code == 200 and res_data.get('status') != 'error':
return Response({
"status": "success",
"message": f"WhatsApp OTP dispatched to {mobile_number}",
@ -84,13 +94,26 @@ class SendWhatsAppOTPView(APIView):
"gateway_response": res_data
}, status=status.HTTP_200_OK)
else:
audit_logger.warning(f"WhatsApp gateway response for {mobile_number}: {res.status_code} {res_data}")
audit_logger.warning(f"WhatsApp gateway response for {mobile_number}: {status_code} {res_data}")
return Response({
"status": "success",
"message": f"WhatsApp OTP request submitted for {mobile_number}",
"mobile_number": mobile_number,
"gateway_response": res_data
}, status=status.HTTP_200_OK)
except urllib.error.HTTPError as e:
err_body = e.read().decode('utf-8', errors='ignore')
try:
err_data = json.loads(err_body)
except Exception:
err_data = {"text": err_body}
audit_logger.warning(f"WhatsApp gateway HTTPError {e.code} for {mobile_number}: {err_data}")
return Response({
"status": "warning",
"message": f"WhatsApp gateway returned status {e.code}",
"gateway_response": err_data,
"mobile_number": mobile_number
}, status=status.HTTP_200_OK)
except Exception as e:
audit_logger.error(f"WhatsApp service connection error for {mobile_number}: {e}")
return Response({

View file

@ -4,3 +4,5 @@ djangorestframework-simplejwt
django-cors-headers
gunicorn
psycopg2-binary
requests