Allow to resend order by email

This commit is contained in:
Yohan Boniface 2019-03-24 22:05:26 +01:00
parent 24d278f04d
commit bc802cae42
4 changed files with 43 additions and 19 deletions

View file

@ -255,27 +255,35 @@ async def place_order(request, response, id):
delivery.persist() delivery.persist()
if user and user.email == email: if user and user.email == email:
# Only send email if order has been placed by the user itself. # Only send email if order has been placed by the user itself.
html = env.get_template("emails/order_summary.html").render( emails.send_order(
order=order, delivery=delivery env, person=Person(email=email), delivery=delivery, order=order
)
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(f"La commande pour «{email}» a bien été prise en compte!") response.message(f"La commande pour «{email}» a bien été prise en compte!")
response.redirect = f"/livraison/{delivery.id}" response.redirect = f"/livraison/{delivery.id}"
else: else:
order = delivery.orders.get(email) or Order() order = delivery.orders.get(email) or Order()
response.html( response.html(
"place_order.html", {"delivery": delivery, "person": email, "order": order} "place_order.html",
{"delivery": delivery, "person": Person(email=email), "order": order},
) )
@app.route("/livraison/{id}/courriel", methods=["GET"])
@auth_required
async def send_order(request, response, id):
delivery = Delivery.load(id)
email = request.query.get("email")
order = delivery.orders.get(email)
if not order:
response.message(f"Aucune commande pour «{email}»", status="warning")
else:
emails.send_order(
env, person=Person(email=email), delivery=delivery, order=order
)
response.message(f"Commande envoyée à «{email}»")
response.redirect = f"/livraison/{delivery.id}"
@app.route("/livraison/{id}/émargement", methods=["GET"]) @app.route("/livraison/{id}/émargement", methods=["GET"])
@auth_required @auth_required
async def signing_sheet(request, response, id): async def signing_sheet(request, response, id):

View file

@ -21,7 +21,7 @@ def send(to, subject, body, html=None):
msg["From"] = config.FROM_EMAIL msg["From"] = config.FROM_EMAIL
msg["To"] = to msg["To"] = to
if html: if html:
msg.add_alternative(html, subtype='html') msg.add_alternative(html, subtype="html")
if not config.SEND_EMAILS: if not config.SEND_EMAILS:
return print("Sending email", str(msg)) return print("Sending email", str(msg))
try: try:
@ -32,3 +32,18 @@ def send(to, subject, body, html=None):
raise RuntimeError raise RuntimeError
finally: finally:
server.quit() server.quit()
def send_order(env, person, delivery, order):
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
)
send(
person.email,
f"Copanier: résumé de la commande {delivery.producer}",
body=txt,
html=html,
)

View file

@ -114,7 +114,7 @@ h4,
h5, h5,
legend { legend {
/*margin: 0;*/ /*margin: 0;*/
color: var(--primary-color); color: #444;
line-height: 1; line-height: 1;
font-weight: 300; font-weight: 300;
} }
@ -376,7 +376,7 @@ hr {
.notification { .notification {
width: 100%; width: 100%;
text-align: center; text-align: center;
color: #f1f1f1; color: white;
line-height: 3rem; line-height: 3rem;
height: 3rem; height: 3rem;
vertical-align: middle; vertical-align: middle;
@ -387,8 +387,8 @@ hr {
.notification.error { .notification.error {
background-color: #e10055; background-color: #e10055;
} }
.notification.error { .notification.warning {
background-color: #8c5a2d; background-color: #f9b42d;
} }
.notification i { .notification i {
font-size: 2rem; font-size: 2rem;

View file

@ -2,7 +2,7 @@
{% block body %} {% block body %}
<article class="order"> <article class="order">
<h3>{{ delivery.producer }} — Commande de «{{ person }}»</h3> <h3>{{ delivery.producer }} — Commande de «{{ person.email }}»</h3>
{% include "includes/delivery_head.html" %} {% include "includes/delivery_head.html" %}
<form method="post"> <form method="post">
<table class="order"> <table class="order">
@ -20,8 +20,9 @@
{% endfor %} {% endfor %}
</table> </table>
<p>Total: {{ order.total(delivery.products) if order else 0 }} €</p> <p>Total: {{ order.total(delivery.products) if order else 0 }} €</p>
<input type="hidden" name="email" value="{{ person }}"> <input type="hidden" name="email" value="{{ person.email }}">
<input type="submit" value="Valider la commande"> <input type="submit" value="Valider la commande">
<a class="button" href="/livraison/{{ delivery.id }}/courriel?email={{ person.email }}">Envoyer par courriel</a>
</form> </form>
</article> </article>
{% endblock body %} {% endblock body %}