Skip to content

Commit fed52fe

Browse files
committed
Day 11: Mail Triggering part and fix the profile issue and signal has now working
1 parent 23bb89c commit fed52fe

File tree

4 files changed

+39
-36
lines changed

4 files changed

+39
-36
lines changed

backend/settings.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"rest_framework",
4545
"rest_framework_simplejwt.token_blacklist", # For token blacklisting on logout
4646
"corsheaders", # For handling CORS - required for React frontend
47-
"users",
47+
"users.apps.UsersConfig",
4848
"core",
4949
"rbac",
5050
"easyaudit",
@@ -207,12 +207,12 @@
207207
],
208208
"DEFAULT_PERMISSION_CLASSES": [
209209
"rest_framework.permissions.IsAuthenticated",
210-
"core.permissions.DynamicPagePermission",
210+
"rbac.permissions.DynamicPagePermission",
211211
],
212212
# Disable CSRF for API endpoints (we use JWT)
213-
"DEFAULT_RENDERER_CLASSES": [
214-
"rest_framework.renderers.JSONRenderer",
215-
],
213+
# "DEFAULT_RENDERER_CLASSES": [
214+
# "rest_framework.renderers.JSONRenderer",
215+
# ],
216216
"DEFAULT_PAGINATION_CLASS": "core.pagination.DefaultPagination",
217217
"DeFAULT_THROTTLE_CLASSES": (
218218
"core.throttling.AnonBurstRateThrottle",

users/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default_app_config = "users.apps.UsersConfig"

users/apps.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
class UsersConfig(AppConfig):
55
default_auto_field = "django.db.models.BigAutoField"
66
name = "users"
7+
8+
def ready(self):
9+
pass

users/signals.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
1-
from django.conf import settings
2-
from django.core.mail import send_mail
1+
from django.contrib.auth import get_user_model
32
from django.core.signing import TimestampSigner
43
from django.db.models.signals import post_save
54
from django.dispatch import receiver
65

76
from users.models import Profile
87

8+
User = get_user_model()
99
signer = TimestampSigner()
1010

1111

12-
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
12+
@receiver(post_save, sender=User)
1313
def create_profile_and_send_email(sender, instance, created, **kwargs):
14-
if created:
15-
# Create the profile (safe even if mail fails)
16-
Profile.objects.get_or_create(user=instance)
17-
18-
# Generate a signed token for deletion link
19-
token = signer.sign(instance.pk) # securely signs user ID with timestamp
20-
21-
# Build delete URL
22-
delete_url = f"http://127.0.0.1:8000/users/delete-account/?token={token}"
23-
24-
# Send confirmation mail
25-
try:
26-
send_mail(
27-
subject="Welcome to Our Instagram App 🎉",
28-
message=(
29-
f"Hi {instance.username},\n\n"
30-
f"Your account has been created successfully.\n"
31-
f"If you did NOT create this account, click below to delete it:\n"
32-
f"{delete_url}\n\n"
33-
f"This link is valid for 24 hours."
34-
),
35-
from_email=settings.DEFAULT_FROM_EMAIL,
36-
recipient_list=[instance.email],
37-
fail_silently=False,
38-
)
39-
except Exception as e:
40-
# Even if mail sending fails, the profile remains
41-
print(f"⚠️ Failed to send welcome email: {e}")
14+
if not created:
15+
return
16+
17+
# ✅ Create profile safely
18+
Profile.objects.get_or_create(user=instance)
19+
20+
# ✅ Generate signed token
21+
token = signer.sign(instance.pk)
22+
23+
delete_url = f"http://127.0.0.1:8000/users/delete-account/?token={token}"
24+
print("🔥 SIGNAL FIRED")
25+
print("Delete URL:", delete_url)
26+
27+
# OPTIONAL: send mail
28+
# send_mail(
29+
# subject="Welcome to Our Instagram App 🎉",
30+
# message=(
31+
# f"Hi {instance.username},\n\n"
32+
# f"Your account has been created successfully.\n\n"
33+
# f"If you did NOT create this account, click below to delete it:\n"
34+
# f"{delete_url}\n\n"
35+
# f"This link is valid for 24 hours."
36+
# ),
37+
# from_email=settings.DEFAULT_FROM_EMAIL,
38+
# recipient_list=[instance.email],
39+
# fail_silently=False,
40+
# )

0 commit comments

Comments
 (0)