Skip to content

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

Add event to sending email with link to download csv file

IKarbowiak91e5c70481b4
diff --git a/saleor/csv/emails.py b/saleor/csv/emails.py
index abc7d781e9..beb54dc251 100644
--- a/saleor/csv/emails.py
+++ b/saleor/csv/emails.py
@@ -5,6 +5,7 @@ from templated_email import send_templated_mail
from ..celeryconf import app
from ..core.emails import get_email_context
from ..core.utils import build_absolute_uri
+from . import events
if TYPE_CHECKING:
from .models import ExportFile
@@ -14,13 +15,14 @@ EXPORT_TEMPLATES = {"export_products": "csv/export_products_csv"}
@app.task
-def send_email_with_link_to_download_csv(job: "ExportFile", template_name: str):
- recipient_email = job.created_by.email
+def send_email_with_link_to_download_csv(export_file: "ExportFile", template_name: str):
+ recipient_email = export_file.created_by.email
send_kwargs, ctx = get_email_context()
- ctx["csv_link"] = build_absolute_uri(job.content_file.url)
+ ctx["csv_link"] = build_absolute_uri(export_file.content_file.url)
send_templated_mail(
template_name=EXPORT_TEMPLATES[template_name],
recipient_list=[recipient_email],
context=ctx,
**send_kwargs,
)
+ events.export_file_sent_event(export_file=export_file, user=export_file.created_by)
diff --git a/tests/test_emails.py b/tests/test_emails.py
index 3dc57c8ec7..24cf2271f9 100644
--- a/tests/test_emails.py
+++ b/tests/test_emails.py
@@ -4,13 +4,17 @@ from urllib.parse import urlencode
import pytest
from django.core import mail
from django.core.exceptions import ImproperlyConfigured
+from django.core.files import File
from django.templatetags.static import static
from templated_email import get_connection
import saleor.account.emails as account_emails
+import saleor.csv.emails as csv_emails
import saleor.order.emails as emails
from saleor.core.emails import get_email_context, prepare_url
from saleor.core.utils import build_absolute_uri
+from saleor.csv import ExportEvents
+from saleor.csv.models import ExportEvent
from saleor.order.utils import add_variant_to_draft_order
@@ -359,3 +363,48 @@ def test_send_email_changed_notification(
# Render the email to ensure there is no error
email_connection = get_connection()
email_connection.get_email_message(to=recipients, **expected_call_kwargs)
+
+
[email protected]("saleor.csv.emails.send_templated_mail")
+def test_send_email_with_link_to_download_csv(
+ mocked_templated_email, site_settings, export_file, tmpdir
+):
+ from django.conf import settings
+
+ settings.MEDIA_ROOT = tmpdir
+
+ file_mock = mock.MagicMock(spec=File)
+ file_mock.name = "temp_file.csv"
+
+ export_file.content_file = file_mock
+ export_file.save()
+
+ csv_emails.send_email_with_link_to_download_csv(export_file, "export_products")
+ template = csv_emails.EXPORT_TEMPLATES["export_products"]
+ ctx = {
+ "csv_link": f"http://mirumee.com/media/csv_files/{file_mock.name}",
+ "domain": "mirumee.com",
+ "logo_url": "http://mirumee.com/static/images/logo-light.svg",
+ "site_name": "mirumee.com",
+ }
+ recipients = [export_file.created_by.email]
+ expected_call_kwargs = {
+ "context": ctx,
+ "from_email": site_settings.default_from_email,
+ "template_name": template,
+ }
+
+ # mocked_templated_email.assert_called_once()
+ mocked_templated_email.assert_called_once_with(
+ recipient_list=recipients, **expected_call_kwargs
+ )
+
+ # Render the email to ensure there is no error
+ email_connection = get_connection()
+ email_connection.get_email_message(to=recipients, **expected_call_kwargs)
+
+ assert ExportEvent.objects.filter(
+ export_file=export_file,
+ user=export_file.created_by,
+ type=ExportEvents.EXPORTED_FILE_SENT,
+ )