Skip to content

Commit 2d3ea89

Browse files
committed
Day 9: Delete account if user is not cfreated that account
1 parent 2e38fe0 commit 2d3ea89

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

api/urls.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
AdminDashboardView,
55
AdminRegisterView,
66
AdminUserDetailView,
7+
DeleteAccountView,
78
LoginView,
89
ProductDetailView,
910
ProductListView,
1011
ProductReviewCreateUpdateView,
12+
ProfileView,
1113
TechnicianRegisterView,
1214
UserRegisterView,
1315
)
@@ -21,13 +23,21 @@
2123
),
2224
path("register/admin/", AdminRegisterView.as_view(), name="admin-register"),
2325
path("login/", LoginView.as_view(), name="login"),
24-
path("products/", ProductListView.as_view()),
25-
path("products/<int:product_id>/", ProductDetailView.as_view()),
26-
path("products/<int:product_id>/review/", ProductReviewCreateUpdateView.as_view()),
26+
path("profile/<str:username>/", ProfileView.as_view(), name="profile_detail"),
27+
path("products/", ProductListView.as_view(), name="product-list"),
28+
path(
29+
"products/<int:product_id>/", ProductDetailView.as_view(), name="product-detail"
30+
),
31+
path(
32+
"products/<int:product_id>/review/",
33+
ProductReviewCreateUpdateView.as_view(),
34+
name="product-review",
35+
),
2736
path("admin/dashboard/", AdminDashboardView.as_view(), name="admin-dashboard"),
2837
path(
2938
"admin/users/<int:user_id>/",
3039
AdminUserDetailView.as_view(),
3140
name="admin-user-detail",
3241
),
42+
path("delete-account/", DeleteAccountView.as_view(), name="delete-account"),
3343
]

users/signals.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from django.conf import settings
2+
from django.core.mail import send_mail
3+
from django.core.signing import TimestampSigner
4+
from django.db.models.signals import post_save
5+
from django.dispatch import receiver
6+
7+
from users.models import Profile
8+
9+
signer = TimestampSigner()
10+
11+
12+
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
13+
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"{settings.BASE_URL}/api/delete-account/?token={token}"
23+
24+
# Send confirmation mail
25+
try:
26+
send_mail(
27+
subject="Welcome to Our RO Purifier 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}")

0 commit comments

Comments
 (0)