diff --git a/bd_api/apps/account/signals.py b/bd_api/apps/account/signals.py index 5b919bf6..73b6e4be 100644 --- a/bd_api/apps/account/signals.py +++ b/bd_api/apps/account/signals.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +from datetime import datetime + from django.conf import settings from django.core.mail import EmailMultiAlternatives from django.db.models.signals import post_save @@ -7,7 +9,7 @@ from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_encode -from bd_api.apps.account.models import Account +from bd_api.apps.account.models import Account, Subscription from bd_api.apps.account.token import token_generator from bd_api.custom.environment import get_frontend_url @@ -47,3 +49,37 @@ def send_activation_email_signal(sender, instance, created, raw, **kwargs): """ if created and not raw and not instance.is_active: send_activation_email(instance) + + +def send_welcome_email(account: Account): + """Send welcome email to account""" + to_email = account.email + from_email = settings.EMAIL_HOST_USER + subject = "Bem Vindo à Base dos Dados!" + + token = token_generator.make_token(account) + uid = urlsafe_base64_encode(force_bytes(account.pk)) + + content = render_to_string( + "account/welcome_email.html", + { + "name": account.get_full_name(), + "domain": get_frontend_url(), + "uid": uid, + "token": token, + }, + ) + + msg = EmailMultiAlternatives(subject, "", from_email, [to_email]) + msg.attach_alternative(content, "text/html") + msg.send() + + +def send_welcome_email_bulk(): + """Send welcome email to all previous subscribers""" + + for subscription in Subscription.objects.filter( + subscription__status="active", + subscription__created__lte=datetime(2023, 12, 1), + ).all(): + send_welcome_email(subscription.admin)