From 8c4f45ac0d5a677f177f6d9dfb0622a0241b1cf2 Mon Sep 17 00:00:00 2001
From: Laetitia Getti
Date: Fri, 11 Aug 2023 11:21:22 +0200
Subject: [PATCH] add plain text alternative to mail
---
README.md | 2 +-
la_chariotte/helpers/fixtures.py | 2 ++
la_chariotte/mail/templates/mail/order_confirm_mail.html | 4 +---
la_chariotte/mail/test_mail.py | 5 +++--
la_chariotte/mail/utils.py | 8 +++++++-
requirements.txt | 2 ++
6 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/README.md b/README.md
index 52be3a1..dd334f3 100644
--- a/README.md
+++ b/README.md
@@ -62,7 +62,7 @@ source .venv/bin/activate
### Installation de la base de données
-Chariotte nécessite une base de données, et est actuellement compatible avec PostgreSQL ([instructions d'installation ici](https://www.digitalocean.com/community/tutorials/how-to-install-postgresql-on-ubuntu-20-04-quickstart)).
+La Chariotte nécessite une base de données, et est actuellement compatible avec PostgreSQL ([instructions d'installation ici](https://www.digitalocean.com/community/tutorials/how-to-install-postgresql-on-ubuntu-20-04-quickstart)).
Pour le développement, nous vous conseillons de créer une base de données nommée ```chariotte``` accessible par l'utilisateur et le mot de passe du même nom.
diff --git a/la_chariotte/helpers/fixtures.py b/la_chariotte/helpers/fixtures.py
index e7375b0..3498cd9 100644
--- a/la_chariotte/helpers/fixtures.py
+++ b/la_chariotte/helpers/fixtures.py
@@ -33,5 +33,7 @@ def simple_grouped_order(other_user):
delivery_date=date,
deadline=deadline,
)
+ grouped_order.create_code_from_pk()
+ grouped_order.save()
item = Item.objects.create(name="test item", grouped_order=grouped_order, price=2)
return grouped_order
diff --git a/la_chariotte/mail/templates/mail/order_confirm_mail.html b/la_chariotte/mail/templates/mail/order_confirm_mail.html
index b2b7405..2228e6e 100644
--- a/la_chariotte/mail/templates/mail/order_confirm_mail.html
+++ b/la_chariotte/mail/templates/mail/order_confirm_mail.html
@@ -20,9 +20,7 @@
Envoyer un mail
{% endblock %}
\ No newline at end of file
diff --git a/la_chariotte/mail/test_mail.py b/la_chariotte/mail/test_mail.py
index ef18e31..0166c37 100644
--- a/la_chariotte/mail/test_mail.py
+++ b/la_chariotte/mail/test_mail.py
@@ -17,7 +17,7 @@ def test_send_order_confirmation_mail(mailoutbox, simple_grouped_order, client):
order_url = reverse(
"order:order",
kwargs={
- "grouped_order_id": simple_grouped_order.pk,
+ "code": simple_grouped_order.code,
},
)
response = client.post(
@@ -34,7 +34,8 @@ def test_send_order_confirmation_mail(mailoutbox, simple_grouped_order, client):
assert len(mailoutbox) == 1
m = mailoutbox[0]
assert (
- m.subject == f"[{settings.PROJECT_NAME}] Votre commande pour Test grouped order"
+ m.subject
+ == f"[{settings.PROJECT_NAME}] Votre commande pour «Test grouped order»"
)
assert f"{settings.BASE_URL}" in m.alternatives[0][0]
assert m.alternatives[0][1] == "text/html"
diff --git a/la_chariotte/mail/utils.py b/la_chariotte/mail/utils.py
index 83564a1..1ceb3d7 100644
--- a/la_chariotte/mail/utils.py
+++ b/la_chariotte/mail/utils.py
@@ -1,3 +1,4 @@
+import html2text
from django.core import mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
@@ -14,7 +15,12 @@ def send_order_confirmation_mail(order):
html_message = render_to_string(
template_name, {"order": order, "base_url": settings.BASE_URL}
)
- plain_message = strip_tags(html_message)
+
+ # create plain message text from html
+ plain_message = html2text.html2text(html_message)
+ # do not render the logo link
+ plain_message = plain_message[plain_message.rfind("Merci,") :]
+
from_email = settings.DEFAULT_FROM_EMAIL
to = order.author.email
diff --git a/requirements.txt b/requirements.txt
index 53cc4c2..72d77a0 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -33,6 +33,8 @@ cssselect2==0.7.0
# via svglib
django==4.2.1
# via la-chariotte (pyproject.toml)
+html2text==2020.1.16
+ # à la main
html5lib==1.1
# via xhtml2pdf
idna==3.4