Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add event to sending email with link to download csv file
IKarbowiak91e5c70481b4
diff --git a/saleor/csv/emails.py b/saleor/csv/emails.pyindex abc7d781e9..beb54dc251 100644--- a/saleor/csv/emails.py+++ b/saleor/csv/emails.py@@ -5,6 +5,7 @@ from templated_email import send_templated_mailfrom ..celeryconf import appfrom ..core.emails import get_email_contextfrom ..core.utils import build_absolute_uri+from . import eventsif 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.emailsend_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.pyindex 3dc57c8ec7..24cf2271f9 100644--- a/tests/test_emails.py+++ b/tests/test_emails.py@@ -4,13 +4,17 @@ from urllib.parse import urlencodeimport pytestfrom django.core import mailfrom django.core.exceptions import ImproperlyConfigured+from django.core.files import Filefrom django.templatetags.static import staticfrom templated_email import get_connectionimport saleor.account.emails as account_emails+import saleor.csv.emails as csv_emailsimport saleor.order.emails as emailsfrom saleor.core.emails import get_email_context, prepare_urlfrom saleor.core.utils import build_absolute_uri+from saleor.csv import ExportEvents+from saleor.csv.models import ExportEventfrom 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 erroremail_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,+ )