From b5202a9de6d3525c4334195c7ae8da4c973394f0 Mon Sep 17 00:00:00 2001 From: Midgard Date: Wed, 11 May 2022 02:43:08 +0200 Subject: [PATCH] Don't crash when rendering None price --- app/app.py | 3 ++- app/templates/order.html | 2 +- app/utils.py | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/app.py b/app/app.py index 3553d2a..c2d9e05 100755 --- a/app/app.py +++ b/app/app.py @@ -19,7 +19,7 @@ from login import init_login from markupsafe import Markup from models import db from models.anonymous_user import AnonymouseUser -from utils import euro_string, price_range_string +from utils import euro_string, price_range_string, ignore_none from zeus import init_oauth @@ -151,6 +151,7 @@ def add_template_filters(app: Flask) -> None: app.template_filter("price_range")(price_range_string) app.template_filter("any")(any) app.template_filter("all")(all) + app.template_filter("ignore_none")(ignore_none) def create_app(): diff --git a/app/templates/order.html b/app/templates/order.html index 39d43c7..ffb267b 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -281,7 +281,7 @@ {{ "disabled" if not order.can_modify_payment(current_user.id) }}> - {{ order_items | map(attribute="price") | sum | euro }} + {{ order_items | map(attribute="price") | ignore_none | sum | euro }} {% if paid %}{% endif %} diff --git a/app/utils.py b/app/utils.py index 80556f2..9ef9037 100644 --- a/app/utils.py +++ b/app/utils.py @@ -4,10 +4,12 @@ import re from typing import Iterable, Optional -def euro_string(value: int, unit="€ ") -> str: +def euro_string(value: Optional[int], unit="€ ") -> str: """ Convert cents to string formatted euro """ + if value is None: + return "✗" euro, cents = divmod(value, 100) if cents: return f"{unit}{euro}.{cents:02}"