-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- remove EmailTemplate - async the sending of e-mails - create TXT & HTML templates
- Loading branch information
1 parent
395480c
commit e89b296
Showing
25 changed files
with
346 additions
and
216 deletions.
There are no files selected for viewing
Empty file.
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,74 @@ | ||
import logging | ||
from typing import Dict, List, Optional | ||
|
||
from django.conf import settings | ||
from django.core.mail import EmailMultiAlternatives | ||
from django.template.loader import get_template, render_to_string | ||
from django.utils.translation import gettext_lazy as _ | ||
from django_q.tasks import async_task | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def send_email( | ||
subject: str, | ||
to_emails: List[str], | ||
text_template: str, | ||
html_template: str, | ||
context: Dict, | ||
from_email: Optional[str] = None, | ||
): | ||
if settings.EMAIL_SEND_METHOD == "async": | ||
async_send_email(subject, to_emails, text_template, html_template, context, from_email) | ||
elif settings.EMAIL_SEND_METHOD == "sync": | ||
send_emails(to_emails, subject, text_template, html_template, context, from_email) | ||
else: | ||
raise ValueError(_("Invalid email send method. Must be 'async' or 'sync'.")) | ||
|
||
|
||
def async_send_email( | ||
subject: str, | ||
to_emails: List[str], | ||
text_template: str, | ||
html_template: str, | ||
html_context: Dict, | ||
from_email: Optional[str] = None, | ||
): | ||
logger.info(f"Asynchronously sending {len(to_emails)} emails with subject: {subject}.") | ||
|
||
async_task( | ||
send_emails, | ||
to_emails, | ||
subject, | ||
text_template, | ||
html_template, | ||
html_context, | ||
from_email, | ||
) | ||
|
||
|
||
def send_emails( | ||
user_emails: List[str], | ||
subject: str, | ||
text_template: str, | ||
html_template: str, | ||
html_context: Dict, | ||
from_email: Optional[str] = None, | ||
): | ||
logger.info(f"Sending emails to {len(user_emails)} users.") | ||
|
||
for email in user_emails: | ||
text_body = render_to_string(text_template, context=html_context) | ||
|
||
html = get_template(html_template) | ||
html_content = html.render(html_context) | ||
|
||
if not from_email: | ||
from_email = ( | ||
settings.DEFAULT_FROM_EMAIL if hasattr(settings, "DEFAULT_FROM_EMAIL") else settings.NO_REPLY_EMAIL | ||
) | ||
|
||
msg = EmailMultiAlternatives(subject, text_body, from_email, [email]) | ||
msg.attach_alternative(html_content, "text/html") | ||
|
||
msg.send(fail_silently=False) |
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
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
24 changes: 24 additions & 0 deletions
24
backend/hub/migrations/0049_delete_emailtemplate_and_more.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,24 @@ | ||
# Generated by Django 4.2.13 on 2024-07-05 12:52 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("hub", "0048_alter_featureflag_flag"), | ||
] | ||
|
||
operations = [ | ||
migrations.DeleteModel( | ||
name="EmailTemplate", | ||
), | ||
migrations.AlterModelOptions( | ||
name="candidateconfirmation", | ||
options={"verbose_name": "Candidate confirmation", "verbose_name_plural": "Candidate confirmations"}, | ||
), | ||
migrations.AlterModelOptions( | ||
name="candidatevote", | ||
options={"verbose_name": "Candidate vote", "verbose_name_plural": "Candidate votes"}, | ||
), | ||
] |
Oops, something went wrong.