Add price range on location view

This commit is contained in:
Midgard 2020-02-29 21:56:04 +01:00
parent f2112ec26d
commit 45a110db23
Signed by: midgard
GPG key ID: 511C112F1331BBB4
4 changed files with 12 additions and 18 deletions

View file

@ -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)

View file

@ -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:

View file

@ -33,7 +33,7 @@
<tr>
<td>{{ dish.name or dish.id }}</td>
<td>{{ dish.description or "" }}</td>
<td style="white-space: nowrap;">{{ dish.price|euro }}<td>
<td style="white-space: nowrap;">{{ dish.price_range()|price_range(true) }}<td>
</tr>
{%- endfor %}
</tbody>

View file

@ -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):
"""