Refactor email sending to use reusable connections
Updated email sending logic across multiple modules to utilize `django.core.mail.get_connection` for better email connection management and efficiency. Adjusted filters in `conditions.py` to correctly handle dictionary inputs when checking attribute lengths.
This commit is contained in:
parent
6fd0a48c0c
commit
ce66513488
3 changed files with 21 additions and 2 deletions
|
|
@ -6,4 +6,11 @@ register = template.Library()
|
||||||
@register.filter
|
@register.filter
|
||||||
def attributes_length(value, arg):
|
def attributes_length(value, arg):
|
||||||
"""Returns True if the value length is more than the argument."""
|
"""Returns True if the value length is more than the argument."""
|
||||||
return len(value) > arg
|
if isinstance(value, dict):
|
||||||
|
count = int()
|
||||||
|
for attribute, _value in value.items():
|
||||||
|
if attribute.endswith("_system"):
|
||||||
|
continue
|
||||||
|
count += 1
|
||||||
|
return count > arg
|
||||||
|
return False
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ from datetime import datetime
|
||||||
|
|
||||||
from celery.app import shared_task
|
from celery.app import shared_task
|
||||||
from constance import config
|
from constance import config
|
||||||
|
from django.core import mail
|
||||||
from django.core.mail import EmailMessage
|
from django.core.mail import EmailMessage
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from django.utils.translation import activate
|
from django.utils.translation import activate
|
||||||
|
|
@ -14,6 +15,7 @@ from core.utils.constance import set_email_settings
|
||||||
@shared_task
|
@shared_task
|
||||||
def contact_us_email(contact_info):
|
def contact_us_email(contact_info):
|
||||||
set_email_settings()
|
set_email_settings()
|
||||||
|
connection = mail.get_connection()
|
||||||
|
|
||||||
email = EmailMessage(
|
email = EmailMessage(
|
||||||
_(f"{config.PROJECT_NAME} | contact us initiated"),
|
_(f"{config.PROJECT_NAME} | contact us initiated"),
|
||||||
|
|
@ -30,6 +32,7 @@ def contact_us_email(contact_info):
|
||||||
),
|
),
|
||||||
to=[config.EMAIL_HOST_USER],
|
to=[config.EMAIL_HOST_USER],
|
||||||
from_email=f"{config.PROJECT_NAME} <{config.EMAIL_FROM}>",
|
from_email=f"{config.PROJECT_NAME} <{config.EMAIL_FROM}>",
|
||||||
|
connection=connection,
|
||||||
)
|
)
|
||||||
email.content_subtype = "html"
|
email.content_subtype = "html"
|
||||||
email.send()
|
email.send()
|
||||||
|
|
@ -47,6 +50,7 @@ def send_order_created_email(order_pk: str) -> tuple[bool, str]:
|
||||||
activate(order.user.language)
|
activate(order.user.language)
|
||||||
|
|
||||||
set_email_settings()
|
set_email_settings()
|
||||||
|
connection = mail.get_connection()
|
||||||
|
|
||||||
if not order.is_whole_digital:
|
if not order.is_whole_digital:
|
||||||
email = EmailMessage(
|
email = EmailMessage(
|
||||||
|
|
@ -62,6 +66,7 @@ def send_order_created_email(order_pk: str) -> tuple[bool, str]:
|
||||||
),
|
),
|
||||||
to=[order.user.email],
|
to=[order.user.email],
|
||||||
from_email=f"{config.PROJECT_NAME} <{config.EMAIL_FROM}>",
|
from_email=f"{config.PROJECT_NAME} <{config.EMAIL_FROM}>",
|
||||||
|
connection=connection,
|
||||||
)
|
)
|
||||||
email.content_subtype = "html"
|
email.content_subtype = "html"
|
||||||
email.send()
|
email.send()
|
||||||
|
|
@ -78,13 +83,14 @@ def send_order_finished_email(order_pk: str) -> tuple[bool, str]:
|
||||||
activate(order.user.language)
|
activate(order.user.language)
|
||||||
|
|
||||||
set_email_settings()
|
set_email_settings()
|
||||||
|
connection = mail.get_connection()
|
||||||
|
|
||||||
email = EmailMessage(
|
email = EmailMessage(
|
||||||
_(f"{config.PROJECT_NAME} | order delivered"),
|
_(f"{config.PROJECT_NAME} | order delivered"),
|
||||||
render_to_string(
|
render_to_string(
|
||||||
template_name="digital_order_delivered_email.html",
|
template_name="digital_order_delivered_email.html",
|
||||||
context={
|
context={
|
||||||
"order_uuid": order.uuid,
|
"order_uuid": order.human_readable_id,
|
||||||
"user_first_name": order.user.first_name,
|
"user_first_name": order.user.first_name,
|
||||||
"order_products": ops,
|
"order_products": ops,
|
||||||
"project_name": config.PROJECT_NAME,
|
"project_name": config.PROJECT_NAME,
|
||||||
|
|
@ -96,6 +102,7 @@ def send_order_finished_email(order_pk: str) -> tuple[bool, str]:
|
||||||
),
|
),
|
||||||
to=[order.user.email],
|
to=[order.user.email],
|
||||||
from_email=f"{config.PROJECT_NAME} <{config.EMAIL_FROM}>",
|
from_email=f"{config.PROJECT_NAME} <{config.EMAIL_FROM}>",
|
||||||
|
connection=connection,
|
||||||
)
|
)
|
||||||
email.content_subtype = "html"
|
email.content_subtype = "html"
|
||||||
email.send()
|
email.send()
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
from celery.app import shared_task
|
from celery.app import shared_task
|
||||||
from constance import config
|
from constance import config
|
||||||
from django.contrib.auth.tokens import PasswordResetTokenGenerator
|
from django.contrib.auth.tokens import PasswordResetTokenGenerator
|
||||||
|
from django.core import mail
|
||||||
from django.core.mail import EmailMessage
|
from django.core.mail import EmailMessage
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from django.utils.encoding import force_bytes
|
from django.utils.encoding import force_bytes
|
||||||
|
|
@ -21,6 +22,7 @@ def send_verification_email_task(user_pk: str) -> tuple[bool, str]:
|
||||||
activate(user.language)
|
activate(user.language)
|
||||||
|
|
||||||
set_email_settings()
|
set_email_settings()
|
||||||
|
connection = mail.get_connection()
|
||||||
|
|
||||||
email_subject = _(f"{config.PROJECT_NAME} | Activate Account")
|
email_subject = _(f"{config.PROJECT_NAME} | Activate Account")
|
||||||
email_body = render_to_string(
|
email_body = render_to_string(
|
||||||
|
|
@ -38,6 +40,7 @@ def send_verification_email_task(user_pk: str) -> tuple[bool, str]:
|
||||||
body=email_body,
|
body=email_body,
|
||||||
from_email=f"{config.PROJECT_NAME} <{config.EMAIL_FROM}>",
|
from_email=f"{config.PROJECT_NAME} <{config.EMAIL_FROM}>",
|
||||||
to=[user.email],
|
to=[user.email],
|
||||||
|
connection=connection,
|
||||||
)
|
)
|
||||||
email.content_subtype = "html"
|
email.content_subtype = "html"
|
||||||
email.send()
|
email.send()
|
||||||
|
|
@ -61,6 +64,7 @@ def send_reset_password_email_task(user_pk: str) -> tuple[bool, str]:
|
||||||
activate(user.language)
|
activate(user.language)
|
||||||
|
|
||||||
set_email_settings()
|
set_email_settings()
|
||||||
|
connection = mail.get_connection()
|
||||||
|
|
||||||
email_subject = _(f"{config.PROJECT_NAME} | Reset Password")
|
email_subject = _(f"{config.PROJECT_NAME} | Reset Password")
|
||||||
email_body = render_to_string(
|
email_body = render_to_string(
|
||||||
|
|
@ -79,6 +83,7 @@ def send_reset_password_email_task(user_pk: str) -> tuple[bool, str]:
|
||||||
body=email_body,
|
body=email_body,
|
||||||
from_email=f"{config.PROJECT_NAME} <{config.EMAIL_FROM}>",
|
from_email=f"{config.PROJECT_NAME} <{config.EMAIL_FROM}>",
|
||||||
to=[user.email],
|
to=[user.email],
|
||||||
|
connection=connection,
|
||||||
)
|
)
|
||||||
email.content_subtype = "html"
|
email.content_subtype = "html"
|
||||||
email.send()
|
email.send()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue