-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added account activation email and sending email reminders
- Loading branch information
Nilanchal Panigrahy
committed
Dec 20, 2023
1 parent
0aaa1a3
commit c49bba1
Showing
16 changed files
with
1,390 additions
and
530 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
bloggy/management/commands/send_account_activation_reminders.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.urls import reverse | ||
|
||
from bloggy import settings | ||
from bloggy.services import email_service | ||
from bloggy.services.token_service import create_token | ||
from bloggy.models import User | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Send email reminders to users who have not activated their accounts' | ||
|
||
def handle(self, *args, **kwargs): | ||
# Get a list of users who have not activated their accounts | ||
inactive_users = User.objects.filter(is_active=False) | ||
# inactive_users = User.objects.filter(email="[email protected]") | ||
|
||
email_count = 0 | ||
for user in inactive_users: | ||
subject = 'Reminder: Verify your email to activate your account' | ||
|
||
verification_token = create_token(user=user, token_type="signup") | ||
verification_link = reverse("activate_account", args=[ | ||
verification_token.uuid, | ||
verification_token.token | ||
]) | ||
|
||
args = { | ||
"email_subject": subject, | ||
"app_name": settings.SITE_TITLE, | ||
"verification_link": settings.SITE_URL + verification_link | ||
} | ||
|
||
try: | ||
email_service.send_custom_email( | ||
subject, | ||
[user.email], | ||
"email/account_activation_reminder_email.html", | ||
args) | ||
print('Success: Account activation reminder mail sent to {}', user.email) | ||
|
||
except Exception as ex: | ||
print('Error: sending email to {}: {}', user.email, ex) | ||
|
||
finally: | ||
email_count = email_count + 1 | ||
|
||
self.stdout.write(self.style.SUCCESS(f"Reminder sent to {email_count} users")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from django.core.management.base import BaseCommand | ||
from bloggy import settings | ||
from bloggy.models.subscriber import Subscribers | ||
from bloggy.services import email_service | ||
from bloggy.models import User | ||
from itertools import chain | ||
|
||
|
||
# python3 manage.py send_card --cardImgUrl="https://media.stacktips.com/media/emails/christmas-card.gif" --subject="Merry Christmas." --targetLink="https://stacktips.com" | ||
class Command(BaseCommand): | ||
help = 'Send wish card to users' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('--cardImgUrl', type=str, required=True, help='URL of the card') | ||
parser.add_argument('--subject', type=str, required=True, help='Email subject') | ||
parser.add_argument('--targetLink', type=str, required=True, help='Target link') | ||
|
||
def handle(self, *args, **options): | ||
card_img_url = options['cardImgUrl'] | ||
subject = options['subject'] | ||
target_link = options['targetLink'] | ||
|
||
if card_img_url is None or target_link is None or subject is None: | ||
self.stdout.write( | ||
self.style.ERROR(f"Missing mandatory arguments --cardImgUrl or --subject or --targetLink")) | ||
|
||
else: | ||
users = chain( | ||
User.objects.all(), | ||
Subscribers.objects.all(), | ||
) | ||
|
||
email_count = 0 | ||
for user in users: | ||
args = { | ||
"user_name": user.name, | ||
"email_subject": subject, | ||
"app_name": settings.SITE_TITLE, | ||
"card_img_url": card_img_url, | ||
"card_target_link": target_link | ||
} | ||
|
||
try: | ||
email_service.send_custom_email(subject, [user.email], "email/wish_card_email.html", args) | ||
print('Success: Card sent to {}', user.email) | ||
except Exception as ex: | ||
print('Error sending card to {}: {}', user.email, ex) | ||
finally: | ||
email_count = email_count + 1 | ||
|
||
self.stdout.write(self.style.SUCCESS(f"Reminder sent to {email_count} users")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.