Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Drop html2text library (#16845)
Maciej Korycinski99f9e28f8b6f
diff --git a/poetry.lock b/poetry.lockindex 5567e6b362..13b1e1e889 100644--- a/poetry.lock+++ b/poetry.lock@@ -1898,16 +1898,6 @@ lxml = "*"[package.extras]dev = ["mock", "pre-commit", "pytest"]-[[package]]-name = "html2text"-version = "2024.2.26"-description = "Turn HTML into equivalent Markdown-structured text."-optional = false-python-versions = ">=3.8"-files = [- {file = "html2text-2024.2.26.tar.gz", hash = "sha256:05f8e367d15aaabc96415376776cdd11afd5127a77fce6e36afc60c563ca2c32"},-]-[[package]]name = "httptools"version = "0.6.1"@@ -5165,4 +5155,4 @@ multidict = ">=4.0"[metadata]lock-version = "2.0"python-versions = "~3.12"-content-hash = "d8ff6a6f5c04755340631236626ec6d9da3d53fe2819e757bdebe9f51da67093"+content-hash = "b4ebf16a70f2f9c1f45ae28c17d54d6a8b978247ad9acef63b4d99e7446638dc"diff --git a/pyproject.toml b/pyproject.tomlindex af76924b19..8f7e10b083 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -48,7 +48,6 @@ documentation = "https://docs.saleor.io/"graphql-core = "^2.3.2"graphql-relay = "^2.0.1"gunicorn = "^22.0.0"- html2text = "^2024.2.26"html-to-draftjs = "^1.0.1"jaeger-client = "^4.5.0"lxml = "^4.9.3"diff --git a/saleor/plugins/email_common.py b/saleor/plugins/email_common.pyindex 65052f3ebe..eb69c21921 100644--- a/saleor/plugins/email_common.py+++ b/saleor/plugins/email_common.py@@ -8,7 +8,6 @@ from email.headerregistry import Addressfrom typing import TYPE_CHECKING, Any, Callable, Optionalimport dateutil.parser-import html2textimport i18naddressimport pybarsfrom babel.numbers import format_currency@@ -17,6 +16,8 @@ from django.core.mail import send_mailfrom django.core.mail.backends.smtp import EmailBackendfrom django.core.validators import EmailValidatorfrom django_prices.utils.locale import get_locale_data+from lxml import etree+from lxml import html as lxml_htmlfrom ..thumbnail.utils import get_thumbnail_sizefrom .base_plugin import ConfigurationTypeField@@ -185,6 +186,22 @@ def price(this, net_amount, gross_amount, currency, display_gross=False):return pybars.strlist([formatted_price])+def get_plain_text_message_for_email(message: str) -> str:+ try:+ html_message = lxml_html.fromstring(message)+ except etree.ParserError:+ html_message = None++ plain_text = ""+ if html_message is not None:+ html_message_to_parse = html_message.find("body")+ if html_message_to_parse is None:+ html_message_to_parse = html_message++ plain_text = " ".join(str(html_message_to_parse.text_content()).split())+ return plain_text++def send_email(config: EmailConfig, recipient_list, context, subject="", template_str=""):@@ -216,7 +233,7 @@ def send_email(subject_message = subject_template(context, helpers)send_mail(subject_message,- html2text.html2text(message),+ get_plain_text_message_for_email(message),from_email,recipient_list,html_message=message,diff --git a/saleor/plugins/tests/test_email_common.py b/saleor/plugins/tests/test_email_common.pyindex 6bdf971731..c36e2643cd 100644--- a/saleor/plugins/tests/test_email_common.py+++ b/saleor/plugins/tests/test_email_common.py@@ -1,5 +1,5 @@import json-from unittest.mock import patch+from unittest.mock import ANY, patchimport pytestfrom django.core.exceptions import ValidationError@@ -7,7 +7,10 @@ from django.core.exceptions import ValidationErrorfrom ...order.notifications import get_image_payloadfrom ..email_common import (DEFAULT_EMAIL_CONFIGURATION,+ EmailConfig,+ get_plain_text_message_for_email,get_product_image_thumbnail,+ send_email,validate_default_email_configuration,)from ..error_codes import PluginErrorCode@@ -125,3 +128,63 @@ def test_get_product_image_thumbnail_image_missing(product_with_image):# thenassert thumbnail is None+++ ("html_message", "expected_output"),+ [+ (+ "<html><head>"+ '<style type="text/css">#outlook a { padding:0; }body { margin:0;padding:0;'+ "-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%; }table, td { "+ "border-collapse:collapse;mso-table-lspace:0pt;mso-table-rspace:0pt; }img "+ "{ border:0;height:auto;line-height:100%; outline:none;text-decoration:"+ "none;-ms-interpolation-mode:bicubic; }p { display:block;margin:13px 0; }"+ "</style></head><body><p>Hello World!</p></body></html>",+ "Hello World!",+ ),+ ("<p>Hello World!</p>", "Hello World!"),+ ("<html>Hello World!</html>", "Hello World!"),+ ("", ""),+ ("Hello World!", "Hello World!"),+ ],+)+def test_get_plain_text_message_for_email(html_message, expected_output):+ # when+ output = get_plain_text_message_for_email(+ html_message,+ )++ # then+ assert output == expected_output+++@patch(+ "saleor.plugins.email_common.get_plain_text_message_for_email",+ wraps=get_plain_text_message_for_email,+)+@patch("saleor.plugins.email_common.send_mail")+def test_send_email(mocked_send_mail, mocked_get_plain_text):+ # given+ sender_address = "[email protected]"+ sender_name = "dummy"+ config = EmailConfig(sender_name=sender_name, sender_address=sender_address)+ recipment_list = ["[email protected]"]++ email_content = "<html>Hello World!</html>"+ email_plain_text = "Hello World!"+ email_subject = "Email subject"++ # when+ send_email(config, recipment_list, "", email_subject, email_content)++ # then+ mocked_get_plain_text.assert_called_once_with(email_content)+ mocked_send_mail.assert_called_once_with(+ email_subject,+ email_plain_text,+ f"{sender_name} <{sender_address}>",+ recipment_list,+ html_message=email_content,+ connection=ANY,+ )