Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Applied changes to the send_email procedures

Mikail KOCAKeb3159fbe1a4
diff --git a/saleor/account/emails.py b/saleor/account/emails.py
index b9afea2c23..155d7069da 100644
--- a/saleor/account/emails.py
+++ b/saleor/account/emails.py
@@ -1,13 +1,12 @@
from urllib.parse import urlencode, urlsplit
-from django.conf import settings
from django.contrib.auth.tokens import default_token_generator
from django.urls import reverse
from templated_email import send_templated_mail
from ..account import events as account_events
from ..celeryconf import app
-from ..core.emails import get_email_base_context
+from ..core.emails import get_email_bases
from ..core.utils import build_absolute_uri
@@ -42,13 +41,13 @@ def _send_password_reset_email_with_url(recipient_email, redirect_url, user_id,
def _send_password_reset_email(recipient_email, reset_url, user_id):
- context = get_email_base_context()
- context["reset_url"] = reset_url
+ send_kwargs, ctx = get_email_bases()
+ ctx["reset_url"] = reset_url
send_templated_mail(
template_name="account/password_reset",
- from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[recipient_email],
- context=context,
+ context=ctx,
+ **send_kwargs,
)
account_events.customer_password_reset_link_sent_event(user_id=user_id)
@@ -88,11 +87,11 @@ def _send_account_delete_confirmation_email(recipient_email, token):
def _send_delete_confirmation_email(recipient_email, delete_url):
- ctx = get_email_base_context()
+ send_kwargs, ctx = get_email_bases()
ctx["delete_url"] = delete_url
send_templated_mail(
template_name="account/account_delete",
- from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[recipient_email],
context=ctx,
+ **send_kwargs,
)
diff --git a/saleor/core/emails.py b/saleor/core/emails.py
index 9e132f6406..2cfec51b31 100644
--- a/saleor/core/emails.py
+++ b/saleor/core/emails.py
@@ -4,7 +4,13 @@ from django.templatetags.static import static
from ..core.utils import build_absolute_uri
-def get_email_base_context():
- site = Site.objects.get_current()
+def get_email_bases():
+ site: Site = Site.objects.get_current()
logo_url = build_absolute_uri(static("images/logo-light.svg"))
- return {"domain": site.domain, "logo_url": logo_url, "site_name": site.name}
+ send_email_kwargs = {"from_email": site.settings.default_from_email}
+ email_template_context = {
+ "domain": site.domain,
+ "logo_url": logo_url,
+ "site_name": site.name,
+ }
+ return send_email_kwargs, email_template_context
diff --git a/saleor/dashboard/emails.py b/saleor/dashboard/emails.py
index bb6bfb726a..318fe7fb03 100644
--- a/saleor/dashboard/emails.py
+++ b/saleor/dashboard/emails.py
@@ -1,6 +1,5 @@
from urllib.parse import urlencode, urlsplit
-from django.conf import settings
from django.contrib.auth.tokens import default_token_generator
from django.urls import reverse
from django.utils.encoding import force_bytes
@@ -9,7 +8,7 @@ from templated_email import send_templated_mail
from ..account.models import User
from ..celeryconf import app
-from ..core.emails import get_email_base_context
+from ..core.emails import get_email_bases
from ..core.utils import build_absolute_uri
@@ -53,24 +52,24 @@ def _send_set_user_password_email(recipient_email, user_pk, token, template_name
def _send_set_password_email(recipient_email, password_set_url, template_name):
- ctx = get_email_base_context()
+ send_kwargs, ctx = get_email_bases()
ctx["password_set_url"] = password_set_url
send_templated_mail(
template_name=template_name,
- from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[recipient_email],
context=ctx,
+ **send_kwargs,
)
@app.task
def send_promote_customer_to_staff_email(staff_pk):
staff = User.objects.get(pk=staff_pk)
- ctx = get_email_base_context()
+ send_kwargs, ctx = get_email_bases()
ctx["dashboard_url"] = build_absolute_uri(reverse("dashboard:index"))
send_templated_mail(
template_name="dashboard/staff/promote_customer",
- from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[staff.email],
context=ctx,
+ **send_kwargs,
)
diff --git a/saleor/graphql/shop/types.py b/saleor/graphql/shop/types.py
index 9807d6c1a8..eae4e5f617 100644
--- a/saleor/graphql/shop/types.py
+++ b/saleor/graphql/shop/types.py
@@ -285,7 +285,7 @@ class Shop(graphene.ObjectType):
def resolve_default_mail_sender_address(_, info):
return (
info.context.site.settings.default_mail_sender_address
- or settings.EMAIL_HOST_USER
+ or settings.DEFAULT_FROM_EMAIL
)
@staticmethod
diff --git a/saleor/order/emails.py b/saleor/order/emails.py
index 979e3abc6f..2108700212 100644
--- a/saleor/order/emails.py
+++ b/saleor/order/emails.py
@@ -1,9 +1,8 @@
-from django.conf import settings
from django.urls import reverse
from templated_email import send_templated_mail
from ..celeryconf import app
-from ..core.emails import get_email_base_context
+from ..core.emails import get_email_bases
from ..core.utils import build_absolute_uri
from ..seo.schema.email import get_order_confirmation_markup
from . import events
@@ -25,7 +24,7 @@ def collect_data_for_email(order_pk, template):
"""
order = Order.objects.get(pk=order_pk)
recipient_email = order.get_customer_email()
- email_context = get_email_base_context()
+ send_kwargs, email_context = get_email_bases()
email_context["order_details_url"] = build_absolute_uri(
reverse("order:details", kwargs={"token": order.token})
)
@@ -40,7 +39,7 @@ def collect_data_for_email(order_pk, template):
"recipient_list": [recipient_email],
"template_name": template,
"context": email_context,
- "from_email": settings.ORDER_FROM_EMAIL,
+ **send_kwargs,
}
diff --git a/saleor/settings.py b/saleor/settings.py
index 992e25438c..df3e7bf330 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -139,7 +139,6 @@ if ENABLE_SSL:
SECURE_SSL_REDIRECT = not DEBUG
DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL", EMAIL_HOST_USER)
-ORDER_FROM_EMAIL = os.getenv("ORDER_FROM_EMAIL", DEFAULT_FROM_EMAIL)
MEDIA_ROOT = os.path.join(PROJECT_ROOT, "media")
MEDIA_URL = os.environ.get("MEDIA_URL", "/media/")
diff --git a/saleor/site/migrations/0023_auto_20191007_0835.py b/saleor/site/migrations/0023_auto_20191007_0835.py
index 5d3234605b..c77b22f08c 100644
--- a/saleor/site/migrations/0023_auto_20191007_0835.py
+++ b/saleor/site/migrations/0023_auto_20191007_0835.py
@@ -23,9 +23,10 @@ class Migration(migrations.Migration):
null=True,
validators=[
django.core.validators.RegexValidator(
- "^[^\\n\\r]*$",
+ "[\\n\\r]",
code="forbidden_character",
- message="New lines are forbidden.",
+ inverse_match=True,
+ message="New lines are not allowed.",
)
],
),
diff --git a/saleor/site/models.py b/saleor/site/models.py
index e18435bf48..60b37eae16 100644
--- a/saleor/site/models.py
+++ b/saleor/site/models.py
@@ -85,7 +85,7 @@ class SiteSettings(models.Model):
sender_name: Optional[str] = self.default_mail_sender_name
sender_address: Optional[
str
- ] = self.default_mail_sender_address or settings.EMAIL_HOST_USER
+ ] = self.default_mail_sender_address or settings.DEFAULT_FROM_EMAIL
# If no name was provided, only return the email address
if not sender_name:
diff --git a/tests/api/test_shop.py b/tests/api/test_shop.py
index 7ea0ab629b..d790300e05 100644
--- a/tests/api/test_shop.py
+++ b/tests/api/test_shop.py
@@ -291,7 +291,7 @@ def test_query_default_mail_sender_settings_not_set(
update_fields=["default_mail_sender_name", "default_mail_sender_address"]
)
- settings.EMAIL_HOST_USER = "[email protected]"
+ settings.DEFAULT_FROM_EMAIL = "[email protected]"
query = QUERY_RETRIEVE_DEFAULT_MAIL_SENDER_SETTINGS
diff --git a/tests/conftest.py b/tests/conftest.py
index e67916f310..d7d8e21b7c 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -73,7 +73,11 @@ def site_settings(db, settings) -> SiteSettings:
should never exist without a matching settings object.
"""
site = Site.objects.get_or_create(name="mirumee.com", domain="mirumee.com")[0]
- obj = SiteSettings.objects.get_or_create(site=site)[0]
+ obj = SiteSettings.objects.get_or_create(
+ site=site,
+ default_mail_sender_name="Mirumee Labs",
+ default_mail_sender_address="[email protected]",
+ )[0]
settings.SITE_ID = site.pk
main_menu = Menu.objects.get_or_create(
diff --git a/tests/dashboard/test_customer.py b/tests/dashboard/test_customer.py
index 2aa4322a10..be4d42b21a 100644
--- a/tests/dashboard/test_customer.py
+++ b/tests/dashboard/test_customer.py
@@ -17,7 +17,6 @@ from saleor.dashboard.customer.forms import (
CustomerForm,
CustomerNoteForm,
)
-from saleor.settings import DEFAULT_FROM_EMAIL
def test_ajax_users_list(admin_client, admin_user, customer_user):
@@ -279,7 +278,7 @@ def test_send_set_password_customer_email(customer_user, site_settings):
}
send_templated_mail(
template_name="dashboard/customer/set_password",
- from_email=DEFAULT_FROM_EMAIL,
+ from_email=site_settings.default_from_email,
recipient_list=[customer_user.email],
context=ctx,
)
diff --git a/tests/dashboard/test_staff.py b/tests/dashboard/test_staff.py
index e5a7b8ef62..547028ac56 100644
--- a/tests/dashboard/test_staff.py
+++ b/tests/dashboard/test_staff.py
@@ -10,7 +10,6 @@ from saleor.account.models import User
from saleor.core.utils import build_absolute_uri
from saleor.dashboard.staff.forms import StaffForm
from saleor.dashboard.staff.utils import remove_staff_member
-from saleor.settings import DEFAULT_FROM_EMAIL
def test_remove_staff_member_with_orders(staff_user, permission_manage_products, order):
@@ -155,7 +154,7 @@ def test_send_set_password_email(staff_user, site_settings):
}
send_templated_mail(
template_name="dashboard/staff/set_password",
- from_email=DEFAULT_FROM_EMAIL,
+ from_email=site_settings.default_from_email,
recipient_list=[staff_user.email],
context=ctx,
)
diff --git a/tests/test_emails.py b/tests/test_emails.py
index 8cce55db47..41ab750950 100644
--- a/tests/test_emails.py
+++ b/tests/test_emails.py
@@ -5,7 +5,7 @@ from django.templatetags.static import static
from templated_email import get_connection
import saleor.order.emails as emails
-from saleor.core.emails import get_email_base_context
+from saleor.core.emails import get_email_bases
from saleor.core.utils import build_absolute_uri
from saleor.order.utils import add_variant_to_order
@@ -13,13 +13,16 @@ from saleor.order.utils import add_variant_to_order
def test_get_email_base_context(site_settings):
site = site_settings.site
logo_url = build_absolute_uri(static("images/logo-light.svg"))
+
+ expected_send_kwargs = {"from_email": site_settings.default_from_email}
proper_context = {
"domain": site.domain,
"logo_url": logo_url,
"site_name": site.name,
}
- received_context = get_email_base_context()
+ send_kwargs, received_context = get_email_bases()
+ assert send_kwargs == expected_send_kwargs
assert proper_context == received_context
@@ -62,7 +65,9 @@ def test_collect_data_for_email(order):
],
)
@mock.patch("saleor.order.emails.send_templated_mail")
-def test_send_emails(mocked_templated_email, order, template, send_email, settings):
+def test_send_emails(
+ mocked_templated_email, order, template, send_email, site_settings
+):
send_email(order.pk)
email_data = emails.collect_data_for_email(order.pk, template)
@@ -70,7 +75,7 @@ def test_send_emails(mocked_templated_email, order, template, send_email, settin
expected_call_kwargs = {
"context": email_data["context"],
- "from_email": settings.ORDER_FROM_EMAIL,
+ "from_email": site_settings.default_from_email,
"template_name": template,
}
@@ -92,7 +97,7 @@ def test_send_emails(mocked_templated_email, order, template, send_email, settin
)
@mock.patch("saleor.order.emails.send_templated_mail")
def test_send_confirmation_emails_without_addresses(
- mocked_templated_email, order, template, send_email, settings, digital_content
+ mocked_templated_email, order, template, send_email, site_settings, digital_content
):
assert not order.lines.count()
@@ -110,7 +115,7 @@ def test_send_confirmation_emails_without_addresses(
expected_call_kwargs = {
"context": email_data["context"],
- "from_email": settings.ORDER_FROM_EMAIL,
+ "from_email": site_settings.default_from_email,
"template_name": template,
}
@@ -135,7 +140,7 @@ def test_send_confirmation_emails_without_addresses(
)
@mock.patch("saleor.order.emails.send_templated_mail")
def test_send_fulfillment_emails(
- mocked_templated_email, template, send_email, fulfilled_order, settings
+ mocked_templated_email, template, send_email, fulfilled_order, site_settings
):
fulfillment = fulfilled_order.fulfillments.first()
send_email(order_pk=fulfilled_order.pk, fulfillment_pk=fulfillment.pk)
@@ -147,7 +152,7 @@ def test_send_fulfillment_emails(
expected_call_kwargs = {
"context": email_data["context"],
- "from_email": settings.ORDER_FROM_EMAIL,
+ "from_email": site_settings.default_from_email,
"template_name": template,
}