From 45a110db2313ab3c0138517ed6e541d284329bcc Mon Sep 17 00:00:00 2001 From: Midgard Date: Sat, 29 Feb 2020 21:56:04 +0100 Subject: [PATCH] Add price range on location view --- app/app.py | 12 ++++-------- app/forms.py | 11 ++--------- app/templates/location.html | 2 +- app/utils.py | 5 +++++ 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/app/app.py b/app/app.py index 51888a3..6595a52 100755 --- a/app/app.py +++ b/app/app.py @@ -18,7 +18,7 @@ from flask_script import Manager, Server from login import init_login from models import db from models.anonymous_user import AnonymouseUser -from utils import euro_string +from utils import euro_string, price_range_string from zeus import init_oauth @@ -159,15 +159,11 @@ def add_template_filters(app: Flask) -> None: return time @app.template_filter("year") - def current_year(value: typing.Any) -> str: # pylint: disable=W0613 - "A function which returns the current year" + def current_year(_value: typing.Any) -> str: return str(datetime.now().year) - @app.template_filter("euro") - def euro(value: int) -> str: - "A function which converts a value to its euro_string" - return euro_string(value) - + app.template_filter("euro")(euro_string) + app.template_filter("price_range")(price_range_string) app.template_filter("any")(any) diff --git a/app/forms.py b/app/forms.py index 115a80a..cf92c6b 100644 --- a/app/forms.py +++ b/app/forms.py @@ -9,7 +9,7 @@ from flask_wtf import FlaskForm as Form from wtforms import (DateTimeField, SelectField, SelectMultipleField, StringField, SubmitField, FieldList, validators) -from utils import euro_string +from utils import euro_string, price_range_string from hlds.definitions import location_definitions from hlds.models import Location, Dish, Choice from models import User @@ -53,16 +53,9 @@ class OrderItemForm(Form): comment = StringField("Comment") submit_button = SubmitField("Submit") - @staticmethod - def format_price_range(price_range): - if price_range[0] == price_range[1]: - return euro_string(price_range[0]) - else: - return "from {}".format(euro_string(price_range[0])) - def populate(self, location: Location) -> None: self.dish_id.choices = [ - (dish.id, (dish.name + ": " + self.format_price_range(dish.price_range()))) + (dish.id, (dish.name + ": " + price_range_string(dish.price_range()))) for dish in location.dishes ] if not self.is_submitted() and self.comment.data is None: diff --git a/app/templates/location.html b/app/templates/location.html index 2cfa19b..34f7453 100644 --- a/app/templates/location.html +++ b/app/templates/location.html @@ -33,7 +33,7 @@ {{ dish.name or dish.id }} {{ dish.description or "" }} - {{ dish.price|euro }} + {{ dish.price_range()|price_range(true) }} {%- endfor %} diff --git a/app/utils.py b/app/utils.py index f0b4245..cf2e02b 100644 --- a/app/utils.py +++ b/app/utils.py @@ -9,6 +9,11 @@ def euro_string(value: int) -> str: """ return "€ {}.{:02}".format(*divmod(value, 100)) +def price_range_string(price_range, include_upper=False): + if price_range[0] == price_range[1]: + return euro_string(price_range[0]) + return ("{}—{}" if include_upper else "from {}").format(*map(euro_string, price_range)) + def first(iterable: Iterable, default=None): """