diff --git a/bloggy/.DS_Store b/bloggy/.DS_Store index 8e1f43b..9ca3d85 100644 Binary files a/bloggy/.DS_Store and b/bloggy/.DS_Store differ diff --git a/bloggy/management/commands/send_account_activation_reminders.py b/bloggy/management/commands/send_account_activation_reminders.py new file mode 100644 index 0000000..e93e609 --- /dev/null +++ b/bloggy/management/commands/send_account_activation_reminders.py @@ -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")) diff --git a/bloggy/management/commands/send_card.py b/bloggy/management/commands/send_card.py new file mode 100644 index 0000000..9552874 --- /dev/null +++ b/bloggy/management/commands/send_card.py @@ -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")) diff --git a/bloggy/services/email_service.py b/bloggy/services/email_service.py index a0a556f..28c324c 100644 --- a/bloggy/services/email_service.py +++ b/bloggy/services/email_service.py @@ -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, @@ -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) \ No newline at end of file diff --git a/bloggy/services/token_service.py b/bloggy/services/token_service.py index 347f98a..ba08e71 100644 --- a/bloggy/services/token_service.py +++ b/bloggy/services/token_service.py @@ -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: diff --git a/bloggy/templates/auth/register.html b/bloggy/templates/auth/register.html index 13f0f70..226660f 100644 --- a/bloggy/templates/auth/register.html +++ b/bloggy/templates/auth/register.html @@ -39,7 +39,7 @@

Create your free account!

{{ form.password2|add_class:"form-control" }} {{ form.password2.errors }} - {{ form.honeypot|add_class:"hidden" }} + {{ form.honeypot|add_class:"d-none" }}