8000 feature/account-activation-email-and-sending-email-reminders by nilandev · Pull Request #36 · StackTipsLab/bloggy · GitHub
[go: up one dir, main page]

Skip to content

feature/account-activation-email-and-sending-email-reminders #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions bloggy/management/commands/send_account_activation_reminders.py
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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)

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