8000 Merge pull request #36 from StackTipsLab/feature/layout-improvements · StackTipsLab/bloggy@b669208 · GitHub
[go: up one dir, main page]

Skip to content

Commit b669208

Browse files
authored
Merge pull request #36 from StackTipsLab/feature/layout-improvements
feature/account-activation-email-and-sending-email-reminders
2 parents 9334937 + 5621a92 commit b669208

File tree

16 files changed

+1389
-530
lines changed

16 files changed

+1389
-530
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from django.core.management.base import BaseCommand
2+
from django.urls import reverse
3+
4+
from bloggy import settings
5+
from bloggy.services import email_service
6+
from bloggy.services.token_service import create_token
7+
from bloggy.models import User
8+
9+
10+
class Command(BaseCommand):
11+
help = 'Send email reminders to users who have not activated their accounts'
12+
13+
def handle(self, *args, **kwargs):
14+
# Get a list of users who have not activated their accounts
15+
inactive_users = User.objects.filter(is_active=False)
16+
17+
email_count = 0
18+
for user in inactive_users:
19+
subject = 'Reminder: Verify your email to activate your account'
20+
21+
verification_token = create_token(user=user, token_type="signup")
22+
verification_link = reverse("activate_account", args=[
23+
verification_token.uuid,
24+
verification_token.token
25+
])
26+
27+
args = {
28+
"email_subject": subject,
29+
"app_name": settings.SITE_TITLE,
30+
"verification_link": settings.SITE_URL + verification_link
31+
}
32+
33+
try:
34+
email_service.send_custom_email(
35+
subject,
36+
[user.email],
37+
"email/account_activation_reminder_email.html",
38+
args)
39+
print('Success: Account activation reminder mail sent to {}', user.email)
40+
41+
except Exception as ex:
42+
print('Error: sending email to {}: {}', user.email, ex)
43+
44+
finally:
45+
email_count = email_count + 1
46+
47+
self.stdout.write(self.style.SUCCESS(f"Reminder sent to {email_count} users"))
Lines changed: 51 additions & 0 deletions
< 2D16 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from django.core.management.base import BaseCommand
2+
from bloggy import settings
3+
from bloggy.models.subscriber import Subscribers
4+
from bloggy.services import email_service
5+
from bloggy.models import User
6+
from itertools import chain
7+
8+
9+
# python3 manage.py send_card --cardImgUrl="https://media.stacktips.com/media/emails/christmas-card.gif" --subject="Merry Christmas." --targetLink="https://stacktips.com"
10+
class Command(BaseCommand):
11+
help = 'Send wish card to users'
12+
13+
def add_arguments(self, parser):
14+
parser.add_argument('--cardImgUrl', type=str, required=True, help='URL of the card')
15+
parser.add_argument('--subject', type=str, required=True, help='Email subject')
16+
parser.add_argument('--targetLink', type=str, required=True, help='Target link')
17+
18+
def handle(self, *args, **options):
19+
card_img_url = options['cardImgUrl']
20+
subject = options['subject']
21+
target_link = options['targetLink']
22+
23+
if card_img_url is None or target_link is None or subject is None:
24+
self.stdout.write(
25+
self.style.ERROR(f"Missing mandatory arguments --cardImgUrl or --subject or --targetLink"))
26+
27+
else:
28+
users = chain(
29+
User.objects.all(),
30+
Subscribers.objects.all(),
31+
)
32+
33+
email_count = 0
34+
for user in users:
35+
args = {
36+
"user_name": user.name,
37+
"email_subject": subject,
38+
"app_name": settings.SITE_TITLE,
39+
"card_img_url": card_img_url,
40+
"card_target_link": target_link
41+
}
42+
43+
try:
44+
email_service.send_custom_email(subject, [user.email], "email/wish_card_email.html", args)
45+
print('Success: Card sent to {}', user.email)
46+
except Exception as ex:
47+
print('Error sending card to {}: {}', user.email, ex)
48+
finally:
49+
email_count = email_count + 1
50+
51+
self.stdout.write(self.style.SUCCESS(f"Reminder sent to {email_count} users"))

bloggy/services/email_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def email_verification_token(request, new_user, token):
4040
send_custom_email(subject, [new_user.email], "email/login_code_email.html", args)
4141

4242

43-
def email_registration_token(request, new_user, verification_token):
43+
def send_account_activation_email(request, new_user, verification_token):
4444
subject = f'Welcome to {settings.SITE_TITLE}!'
4545
args = {
4646
"email_subject": subject,
@@ -52,4 +52,4 @@ def email_registration_token(request, new_user, verification_token):
5252
]))
5353
}
5454

55-
send_custom_email(subject, [new_user.email], "email/acc_active_email.html", args)
55+
send_custom_email(subject, [new_user.email], "email/account_activation_email.html", args)

bloggy/services/token_service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
def create_token(user, token_type):
1313
"""
14-
Generate token:: Token and uuid will be generated automatically
14+
Generate token:: Token and uuid will be generated automatically,
15+
16+
if expired, it will automatically renew new token.
1517
"""
1618
token = VerificationToken.objects.filter(user=user, token_type=token_type).first()
1719
if token:

0 commit comments

Comments
 (0)
0