From aa33599391f2381a473d379a1fa2f901c60d070a Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Sun, 24 Mar 2019 17:49:54 +0100 Subject: [PATCH] Basic email with order summary --- copanier/__init__.py | 12 ++++++++++++ copanier/emails.py | 4 +++- copanier/templates/delivery.html | 2 +- copanier/templates/emails/order_summary.html | 5 +++++ copanier/templates/emails/order_summary.txt | 17 +++++++++++++++++ copanier/templates/includes/order_summary.html | 12 ++++++++++++ copanier/templates/signing_sheet.html | 13 +------------ 7 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 copanier/templates/emails/order_summary.html create mode 100644 copanier/templates/emails/order_summary.txt create mode 100644 copanier/templates/includes/order_summary.html diff --git a/copanier/__init__.py b/copanier/__init__.py index dfb7089..79fa6f3 100644 --- a/copanier/__init__.py +++ b/copanier/__init__.py @@ -244,6 +244,18 @@ async def place_order(request, response, id): delivery.orders = {} delivery.orders[email] = order delivery.persist() + html = env.get_template("emails/order_summary.html").render( + order=order, delivery=delivery + ) + txt = env.get_template("emails/order_summary.txt").render( + order=order, delivery=delivery + ) + emails.send( + email, + f"Copanier: résumé de la commande {delivery.producer}", + body=txt, + html=html, + ) response.message("Jour de fête! Votre commande a bien été prise en compte!") response.redirect = request.path else: diff --git a/copanier/emails.py b/copanier/emails.py index a69c03d..4f8bd85 100644 --- a/copanier/emails.py +++ b/copanier/emails.py @@ -14,12 +14,14 @@ Les gentils copains d'Épinamap """ -def send(to, subject, body): +def send(to, subject, body, html=None): msg = EmailMessage() msg.set_content(body) msg["Subject"] = subject msg["From"] = config.FROM_EMAIL msg["To"] = to + if html: + msg.add_alternative(html, subtype='html') if not config.SEND_EMAILS: return print("Sending email", str(msg)) try: diff --git a/copanier/templates/delivery.html b/copanier/templates/delivery.html index 94b7cd9..5af5923 100644 --- a/copanier/templates/delivery.html +++ b/copanier/templates/delivery.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block body %} -

{{ delivery.producer }} Ma commande

+

{{ delivery.producer }} {% if delivery.is_open %}Ma commande{% endif %}

{% include "includes/delivery_head.html" %} diff --git a/copanier/templates/emails/order_summary.html b/copanier/templates/emails/order_summary.html new file mode 100644 index 0000000..2b353d5 --- /dev/null +++ b/copanier/templates/emails/order_summary.html @@ -0,0 +1,5 @@ +

Bonjour,

+

Voici le résumé de la commande «{{ delivery.producer }}»

+{% include "includes/order_summary.html" %} +

Livraison: {{ delivery.where }}, le {{ delivery.from_date|date }} de {{ delivery.from_date|time }} à {{ delivery.to_date|time }}

+

Bonne journée!

diff --git a/copanier/templates/emails/order_summary.txt b/copanier/templates/emails/order_summary.txt new file mode 100644 index 0000000..c8d2a7b --- /dev/null +++ b/copanier/templates/emails/order_summary.txt @@ -0,0 +1,17 @@ +Salut salut, + +Voici le résumé de la commande «{{ delivery.producer }}» + +Produit | Prix unitaire | Quantité + +{% for product in delivery.products %} +{% if order.get_quantity(product) %} +{{ product.name }} | {{ product.price }} € | {{ order.get_quantity(product) }} +{% endif %} +{% endfor %} + +Total: {{ order.total(delivery.products) if order else 0 }} € + +Livraison: {{ delivery.where }}, le {{ delivery.from_date|date }} de {{ delivery.from_date|time }} à {{ delivery.to_date|time }} + +Bonne journée! diff --git a/copanier/templates/includes/order_summary.html b/copanier/templates/includes/order_summary.html new file mode 100644 index 0000000..0d8da23 --- /dev/null +++ b/copanier/templates/includes/order_summary.html @@ -0,0 +1,12 @@ +
+ + {% for product in delivery.products %} + {% if order.get_quantity(product) %} + + + + + {% endif %} + {% endfor %} +
ProduitPrix unitaireQuantité
{{ product.name }}{{ product.price }} €{{ order.get_quantity(product) }}
+

Total: {{ order.total(delivery.products) if order else 0 }} €

diff --git a/copanier/templates/signing_sheet.html b/copanier/templates/signing_sheet.html index 5c5b6d2..9bd12df 100644 --- a/copanier/templates/signing_sheet.html +++ b/copanier/templates/signing_sheet.html @@ -5,17 +5,6 @@

{{ delivery.producer }} {{ delivery.from_date.date() }} - liste d'émargement

{% for email, order in delivery.orders.items() %}

{{ email }}

- - - {% for product in delivery.products %} - {% if order.get_quantity(product) %} - - - - - {% endif %} - {% endfor %} -
ProduitPrix unitaireQuantité
{{ product.name }}{{ product.price }} €{{ order.get_quantity(product) }}
-

Total: {{ order.total(delivery.products) if order else 0 }} €

+ {% include "includes/order_summary.html" %}
{% endfor %}