diff --git a/copanier/__init__.py b/copanier/__init__.py index a7186d3..733d353 100644 --- a/copanier/__init__.py +++ b/copanier/__init__.py @@ -20,9 +20,10 @@ class Response(Response): if self.request.cookies.get("message"): context["message"] = json.loads(self.request.cookies["message"]) self.cookies.set("message", "") + context["config"] = config self.body = env.get_template(template_name).render(*args, **context) - def xlsx(self, body, filename="epinamap.xlsx"): + def xlsx(self, body, filename=f"{config.SITE_NAME}.xlsx"): self.body = body mimetype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" self.headers["Content-Disposition"] = f'attachment; filename="{filename}"' @@ -128,10 +129,13 @@ async def sesame(request, response): async def send_sesame(request, response, unprotected=True): email = request.form.get("email") token = utils.create_token(email) - emails.send( + emails.send_from_template( + env, + "access_granted", email, - "Sésame Copanier", - emails.ACCESS_GRANTED.format(hostname=request.host, token=token.decode()), + f"Sésame {config.SITE_NAME}", + hostname=request.host, + token=token.decode(), ) response.message(f"Un sésame vous a été envoyé à l'adresse '{email}'") response.redirect = "/" @@ -150,6 +154,12 @@ async def set_sesame(request, response, token): response.redirect = "/" +@app.route("/déconnexion", methods=["GET"]) +async def logout(request, response): + response.cookies.set(name="token", value="", httponly=True) + response.redirect = "/" + + @app.route("/", methods=["GET"]) async def home(request, response): response.html("home.html", incoming=Delivery.incoming(), former=Delivery.former()) diff --git a/copanier/config.py b/copanier/config.py index eeef361..eb1f89d 100644 --- a/copanier/config.py +++ b/copanier/config.py @@ -13,6 +13,9 @@ SMTP_LOGIN = "" FROM_EMAIL = "copanier@epinamap.org" STAFF = [] LOCALE = "fr_FR.UTF-8" +SITE_NAME = "Copanier" +SITE_DESCRIPTION = "Les paniers piano d'Épinamap" +EMAIL_SIGNATURE = "Les gentils copains d'Épinamap" def init(): diff --git a/copanier/emails.py b/copanier/emails.py index 9b627b7..3ab9636 100644 --- a/copanier/emails.py +++ b/copanier/emails.py @@ -4,16 +4,6 @@ from email.message import EmailMessage from . import config -ACCESS_GRANTED = """Hey ho! - -Voici le sésame: - -https://{hostname}/sésame/{token} - -Les gentils copains d'Épinamap -""" - - def send(to, subject, body, html=None): msg = EmailMessage() msg.set_content(body) @@ -35,16 +25,20 @@ def send(to, subject, body, html=None): server.quit() +def send_from_template(env, template, to, subject, **params): + params["config"] = config + html = env.get_template(f"emails/{template}.html").render(**params) + txt = env.get_template(f"emails/{template}.txt").render(**params) + send(to, subject, body=txt, html=html) + + def send_order(request, env, person, delivery, order): - html = env.get_template("emails/order_summary.html").render( - order=order, delivery=delivery, request=request - ) - txt = env.get_template("emails/order_summary.txt").render( - order=order, delivery=delivery, request=request - ) - send( + send_from_template( + env, + "order_summary", person.email, - f"Copanier: résumé de la commande {delivery.producer}", - body=txt, - html=html, + f"{config.SITE_NAME} : résumé de la commande {delivery.producer}", + order=order, + delivery=delivery, + request=request ) diff --git a/copanier/templates/base.html b/copanier/templates/base.html index f5ff2fe..454ae48 100644 --- a/copanier/templates/base.html +++ b/copanier/templates/base.html @@ -1,7 +1,7 @@ - {% if title %}{{ title }} - {% endif %}Commandes Epinamap + {% if title %}{{ title }} - {% endif %}{{ config.SITE_NAME }} @@ -17,7 +17,7 @@
{{ message[0] }}
{% endif %}