# backend/users/views.py import random from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.permissions import IsAuthenticated from django.core.mail import send_mail from django.conf import settings from .serializers import UserSerializer from .models import FinancialProfile # --- 1. User Profile View --- class UserProfileView(APIView): permission_classes = [IsAuthenticated] def get(self, request): serializer = UserSerializer(request.user) return Response(serializer.data) def patch(self, request): """Update user profile information""" user = request.user profile = user.profile # Update any allowed fields allowed_fields = ['net_worth', 'cash_available', 'invested_amount', 'credit_used', 'credit_limit', 'phone_number', 'is_onboarded'] for field in allowed_fields: if field in request.data: setattr(profile, field, request.data[field]) profile.save() serializer = UserSerializer(user) return Response(serializer.data) # --- 2. OTP Helper Function --- def generate_otp(): return str(random.randint(100000, 999999)) # --- 3. Send Email OTP View (Bypassed) --- class SendEmailOTPView(APIView): permission_classes = [IsAuthenticated] def post(self, request): user = request.user if not hasattr(user, 'profile'): FinancialProfile.objects.create(user=user) # Auto-verify email user.profile.is_email_verified = True user.profile.save() return Response({ 'message': 'Email verification bypassed. Auto-verified.', 'email': user.email, 'is_email_verified': True }) # --- 4. Verify Email OTP View (Bypassed) --- class VerifyEmailOTPView(APIView): permission_classes = [IsAuthenticated] def post(self, request): user = request.user profile = user.profile profile.is_email_verified = True profile.email_otp = None profile.save() return Response({ 'message': 'Email verified successfully (bypassed)', 'is_email_verified': True }) def send_welcome_email(self, user): """Helper to send the welcome email""" try: subject = 'Welcome to Aureon! 🚀' user_name = user.email.split('@')[0] html_message = f"""
We're thrilled to have you join Aureon. You've taken the first step toward smarter financial management.
Here is what you can do now: