CineDev commited on
Commit
7e97fa2
·
1 Parent(s): 8fd0642

feat: update users views

Browse files
Files changed (1) hide show
  1. users/views.py +134 -35
users/views.py CHANGED
@@ -23,7 +23,7 @@ class UserProfileView(APIView):
23
 
24
  # Update any allowed fields
25
  allowed_fields = ['net_worth', 'cash_available', 'invested_amount',
26
- 'credit_used', 'credit_limit', 'phone_number']
27
 
28
  for field in allowed_fields:
29
  if field in request.data:
@@ -44,50 +44,68 @@ class SendEmailOTPView(APIView):
44
  def post(self, request):
45
  user = request.user
46
 
47
- # Ensure profile exists
48
  if not hasattr(user, 'profile'):
49
  FinancialProfile.objects.create(user=user)
50
 
51
- # Generate OTP
52
  otp = generate_otp()
53
  user.profile.email_otp = otp
54
  user.profile.save()
55
 
56
- # Send email using SendGrid
57
  try:
58
- subject = 'Aureon - Email Verification Code'
59
- message = f'''
60
- Hello {user.email.split('@')[0]},
61
-
62
- Your verification code is: {otp}
63
-
64
- This code will expire in 10 minutes.
65
-
66
- If you didn't request this code, please ignore this email.
67
-
68
- Best regards,
69
- Aureon Team
70
- '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  send_mail(
73
  subject=subject,
74
- message=message,
 
75
  from_email=settings.DEFAULT_FROM_EMAIL,
76
  recipient_list=[user.email],
77
  fail_silently=False,
78
  )
79
 
80
- return Response({
81
- 'message': 'OTP sent successfully to your email',
82
- 'email': user.email
83
- })
84
 
85
  except Exception as e:
86
- return Response({
87
- 'error': f'Failed to send email: {str(e)}'
88
- }, status=500)
89
 
90
- # --- 4. Verify Email OTP View ---
91
  class VerifyEmailOTPView(APIView):
92
  permission_classes = [IsAuthenticated]
93
 
@@ -95,27 +113,108 @@ class VerifyEmailOTPView(APIView):
95
  otp = request.data.get('otp')
96
 
97
  if not otp:
98
- return Response({
99
- 'error': 'OTP is required'
100
- }, status=400)
101
 
102
- profile = request.user.profile
 
103
 
104
  if profile.email_otp == otp:
 
 
 
105
  # Mark email as verified
106
  profile.is_email_verified = True
107
- profile.email_otp = None # Clear the OTP
108
  profile.save()
109
 
 
 
 
 
110
  return Response({
111
  'message': 'Email verified successfully',
112
  'is_email_verified': True
113
  })
114
  else:
115
- return Response({
116
- 'error': 'Invalid OTP'
117
- }, status=400)
118
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  class GetUserMeView(APIView):
120
  permission_classes = [IsAuthenticated]
121
 
 
23
 
24
  # Update any allowed fields
25
  allowed_fields = ['net_worth', 'cash_available', 'invested_amount',
26
+ 'credit_used', 'credit_limit', 'phone_number', 'is_onboarded']
27
 
28
  for field in allowed_fields:
29
  if field in request.data:
 
44
  def post(self, request):
45
  user = request.user
46
 
 
47
  if not hasattr(user, 'profile'):
48
  FinancialProfile.objects.create(user=user)
49
 
 
50
  otp = generate_otp()
51
  user.profile.email_otp = otp
52
  user.profile.save()
53
 
 
54
  try:
55
+ subject = 'Aureon - Verify Your Identity'
56
+ user_name = user.email.split('@')[0]
57
+
58
+ # OTP HTML Template
59
+ html_message = f"""
60
+ <!DOCTYPE html>
61
+ <html lang="en">
62
+ <head>
63
+ <meta charset="UTF-8">
64
+ <style>
65
+ body {{ margin: 0; padding: 0; font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; background-color: #0f172a; color: #e2e8f0; }}
66
+ .email-wrapper {{ width: 100%; background-color: #0f172a; padding: 40px 0; }}
67
+ .email-content {{ max-width: 500px; margin: 0 auto; background-color: #1e293b; border-radius: 12px; overflow: hidden; border: 1px solid #334155; }}
68
+ .header {{ background-color: #1e293b; padding: 30px 0; text-align: center; border-bottom: 1px solid #334155; }}
69
+ .logo-text {{ font-size: 24px; font-weight: 700; color: #10b981; letter-spacing: 2px; text-transform: uppercase; text-decoration: none; }}
70
+ .body-content {{ padding: 40px 30px; text-align: center; }}
71
+ .greeting {{ font-size: 18px; color: #f8fafc; margin-bottom: 16px; }}
72
+ .otp-box {{ background-color: #0f172a; border: 1px dashed #10b981; border-radius: 8px; padding: 20px; margin: 0 auto 30px; max-width: 200px; }}
73
+ .otp-code {{ font-family: 'Courier New', monospace; font-size: 32px; font-weight: 700; letter-spacing: 6px; color: #10b981; margin: 0; }}
74
+ .footer {{ padding: 24px; background-color: #0f172a; text-align: center; font-size: 12px; color: #64748b; border-top: 1px solid #334155; }}
75
+ </style>
76
+ </head>
77
+ <body>
78
+ <div class="email-wrapper">
79
+ <div class="email-content">
80
+ <div class="header"><div class="logo-text">AUREON</div></div>
81
+ <div class="body-content">
82
+ <h2 class="greeting">Verify your email</h2>
83
+ <p style="color: #94a3b8; margin-bottom: 30px;">Hello <strong>{user_name}</strong>,<br>Use the code below to complete your sign-in.</p>
84
+ <div class="otp-box"><div class="otp-code">{otp}</div></div>
85
+ <p style="font-size: 13px; color: #ef4444;">⏳ This code expires in 10 minutes.</p>
86
+ </div>
87
+ <div class="footer"><p>&copy; 2025 Aureon Financial.</p></div>
88
+ </div>
89
+ </div>
90
+ </body>
91
+ </html>
92
+ """
93
 
94
  send_mail(
95
  subject=subject,
96
+ message=f"Your Aureon verification code is: {otp}",
97
+ html_message=html_message,
98
  from_email=settings.DEFAULT_FROM_EMAIL,
99
  recipient_list=[user.email],
100
  fail_silently=False,
101
  )
102
 
103
+ return Response({'message': 'OTP sent successfully', 'email': user.email})
 
 
 
104
 
105
  except Exception as e:
106
+ return Response({'error': str(e)}, status=500)
 
 
107
 
108
+ # --- 4. Verify Email OTP View (Updated with Welcome Email) ---
109
  class VerifyEmailOTPView(APIView):
110
  permission_classes = [IsAuthenticated]
111
 
 
113
  otp = request.data.get('otp')
114
 
115
  if not otp:
116
+ return Response({'error': 'OTP is required'}, status=400)
 
 
117
 
118
+ user = request.user
119
+ profile = user.profile
120
 
121
  if profile.email_otp == otp:
122
+ # Check if this is the FIRST time they are verifying
123
+ is_first_verification = not profile.is_email_verified
124
+
125
  # Mark email as verified
126
  profile.is_email_verified = True
127
+ profile.email_otp = None
128
  profile.save()
129
 
130
+ # --- SEND WELCOME EMAIL IF FIRST TIME ---
131
+ if is_first_verification:
132
+ self.send_welcome_email(user)
133
+
134
  return Response({
135
  'message': 'Email verified successfully',
136
  'is_email_verified': True
137
  })
138
  else:
139
+ return Response({'error': 'Invalid OTP'}, status=400)
140
+
141
+ def send_welcome_email(self, user):
142
+ """Helper to send the welcome email"""
143
+ try:
144
+ subject = 'Welcome to Aureon! 🚀'
145
+ user_name = user.email.split('@')[0]
146
+
147
+ html_message = f"""
148
+ <!DOCTYPE html>
149
+ <html lang="en">
150
+ <head>
151
+ <meta charset="UTF-8">
152
+ <style>
153
+ body {{ margin: 0; padding: 0; font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; background-color: #0f172a; color: #e2e8f0; }}
154
+ .email-wrapper {{ width: 100%; background-color: #0f172a; padding: 40px 0; }}
155
+ .email-content {{ max-width: 550px; margin: 0 auto; background-color: #1e293b; border-radius: 12px; overflow: hidden; border: 1px solid #334155; }}
156
+ .header {{ background-color: #1e293b; padding: 30px 0; text-align: center; border-bottom: 1px solid #334155; }}
157
+ .logo-text {{ font-size: 24px; font-weight: 700; color: #10b981; letter-spacing: 2px; text-transform: uppercase; text-decoration: none; }}
158
+ .hero-image {{ width: 100%; height: 160px; background: linear-gradient(135deg, #10b981 0%, #0f172a 100%); display: flex; align-items: center; justify-content: center; }}
159
+ .hero-text {{ font-size: 28px; font-weight: bold; color: white; text-shadow: 0 2px 4px rgba(0,0,0,0.2); }}
160
+ .body-content {{ padding: 40px 30px; }}
161
+ .feature-list {{ margin: 25px 0; text-align: left; background: #0f172a; padding: 20px; border-radius: 8px; border: 1px solid #334155; }}
162
+ .feature-item {{ margin-bottom: 12px; color: #cbd5e1; display: flex; align-items: center; }}
163
+ .feature-icon {{ color: #10b981; margin-right: 10px; font-weight: bold; }}
164
+ .cta-button {{ display: inline-block; background-color: #10b981; color: #0f172a; padding: 14px 28px; border-radius: 6px; font-weight: bold; text-decoration: none; margin-top: 20px; }}
165
+ .footer {{ padding: 24px; background-color: #0f172a; text-align: center; font-size: 12px; color: #64748b; border-top: 1px solid #334155; }}
166
+ </style>
167
+ </head>
168
+ <body>
169
+ <div class="email-wrapper">
170
+ <div class="email-content">
171
+ <div class="header">
172
+ <div class="logo-text">AUREON</div>
173
+ </div>
174
+
175
+ <div class="hero-image">
176
+ <div class="hero-text">Welcome Aboard!</div>
177
+ </div>
178
+
179
+ <div class="body-content">
180
+ <h2 style="color: #f8fafc; margin-top: 0;">Hi {user_name},</h2>
181
+ <p style="color: #94a3b8; line-height: 1.6;">
182
+ We're thrilled to have you join <strong>Aureon</strong>. You've taken the first step toward smarter financial management.
183
+ </p>
184
+
185
+ <p style="color: #94a3b8;">Here is what you can do now:</p>
186
+
187
+ <div class="feature-list">
188
+ <div class="feature-item"><span class="feature-icon">✓</span> Track your net worth in real-time</div>
189
+ <div class="feature-item"><span class="feature-icon">✓</span> Analyze subscription costs</div>
190
+ <div class="feature-item"><span class="feature-icon">✓</span> Get AI-powered financial insights</div>
191
+ </div>
192
+
193
+ <div style="text-align: center;">
194
+ <a href="http://localhost:5173/dashboard" class="cta-button">Go to Dashboard</a>
195
+ </div>
196
+ </div>
197
+
198
+ <div class="footer">
199
+ <p>&copy; 2025 Aureon Financial. All rights reserved.</p>
200
+ </div>
201
+ </div>
202
+ </div>
203
+ </body>
204
+ </html>
205
+ """
206
+
207
+ send_mail(
208
+ subject=subject,
209
+ message=f"Welcome to Aureon, {user_name}! We are excited to have you.",
210
+ html_message=html_message,
211
+ from_email=settings.DEFAULT_FROM_EMAIL,
212
+ recipient_list=[user.email],
213
+ fail_silently=True, # Fail silently for welcome emails so it doesn't block the API response
214
+ )
215
+ except Exception as e:
216
+ print(f"Failed to send welcome email: {e}")
217
+
218
  class GetUserMeView(APIView):
219
  permission_classes = [IsAuthenticated]
220