Skip to content

Commit

Permalink
Added account activation email and sending email reminders
Browse files Browse the repository at this point in the history
  • Loading branch information
Nilanchal Panigrahy committed Dec 20, 2023
1 parent 0aaa1a3 commit c49bba1
Show file tree
Hide file tree
Showing 16 changed files with 1,390 additions and 530 deletions.
48 changes: 48 additions & 0 deletions bloggy/management/commands/send_account_activation_reminders.py
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"))
51 changes: 51 additions & 0 deletions bloggy/management/commands/send_card.py
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"))
4 changes: 2 additions & 2 deletions bloggy/services/email_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def email_verification_token(request, new_user, token):
send_custom_email(subject, [new_user.email], "email/login_code_email.html", args)


def email_registration_token(request, new_user, verification_token):
def send_account_activation_email(request, new_user, verification_token):
subject = f'Welcome to {settings.SITE_TITLE}!'
args = {
"email_subject": subject,
Expand All @@ -52,4 +52,4 @@ def email_registration_token(request, new_user, verification_token):
]))
}

send_custom_email(subject, [new_user.email], "email/acc_active_email.html", args)
send_custom_email(subject, [new_user.email], "email/account_activation_email.html", args)
4 changes: 3 additions & 1 deletion bloggy/services/token_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

def create_token(user, token_type):
"""
Generate token:: Token and uuid will be generated automatically
Generate token:: Token and uuid will be generated automatically,
if expired, it will automatically renew new token.
"""
token = VerificationToken.objects.filter(user=user, token_type=token_type).first()
if token:
Expand Down
Loading

0 comments on commit c49bba1

Please sign in to comment.