Make /resto deal with vegan and unrecognized kinds
Better to show a fallback category if a kind isn't recognized than nothing at all! The code is refactored to have a table definition in RESTO_TABLES, this allows to DRY when recognizing unrecognized meals (hah). The ** operator is used to copy dicts while being able to amend them: {**meal, "name": …} is a copy of meal but with a different name.
This commit is contained in:
parent
b5033e8b0c
commit
5ba49e6ecd
1 changed files with 37 additions and 9 deletions
46
app/app.py
46
app/app.py
|
@ -246,12 +246,24 @@ RESTO_TEMPLATE = """
|
||||||
|
|
||||||
## Vegetoarisch
|
## Vegetoarisch
|
||||||
{vegi_table}
|
{vegi_table}
|
||||||
|
{uncategorized}
|
||||||
## Groensels
|
## Groensels
|
||||||
{vegetable_table}
|
{vegetable_table}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
UNCATEGORIZED_TEMPLATE = """
|
||||||
|
## Nog etwad anders...
|
||||||
|
{}
|
||||||
|
"""
|
||||||
|
|
||||||
|
RESTO_TABLES = {
|
||||||
|
"soup_table": {"soup"},
|
||||||
|
"meat_table": {"meat"},
|
||||||
|
"fish_table": {"fish"},
|
||||||
|
"vegi_table": {"vegetarian", "vegan"},
|
||||||
|
}
|
||||||
|
|
||||||
@app.route('/resto', methods=['GET'])
|
@app.route('/resto', methods=['GET'])
|
||||||
def resto_menu():
|
def resto_menu():
|
||||||
today = datetime.today()
|
today = datetime.today()
|
||||||
|
@ -262,11 +274,14 @@ def resto_menu():
|
||||||
if not resto["open"]:
|
if not resto["open"]:
|
||||||
return 'De resto is vandaag gesloten.'
|
return 'De resto is vandaag gesloten.'
|
||||||
else:
|
else:
|
||||||
def table_for(kind):
|
def table_for(kinds):
|
||||||
items = [meal for meal in resto["meals"] if meal["kind"] == kind]
|
items = [meal for meal in resto["meals"] if meal["kind"] in kinds]
|
||||||
if len(items) == 0:
|
return format_items(items)
|
||||||
|
|
||||||
|
def format_items(items):
|
||||||
|
if not items:
|
||||||
return "None :("
|
return "None :("
|
||||||
maxwidth = max(map(lambda item: len(item["name"]), items))
|
maxwidth = max(len(item["name"]) for item in items)
|
||||||
return "\n".join("{name: <{width}}{price}".format(
|
return "\n".join("{name: <{width}}{price}".format(
|
||||||
name=item["name"],
|
name=item["name"],
|
||||||
width=maxwidth + 2,
|
width=maxwidth + 2,
|
||||||
|
@ -274,11 +289,24 @@ def resto_menu():
|
||||||
for item in items
|
for item in items
|
||||||
)
|
)
|
||||||
|
|
||||||
|
recognized_kinds = set.union(*RESTO_TABLES.values())
|
||||||
|
uncategorized_meals = [
|
||||||
|
meal for meal in resto["meals"]
|
||||||
|
if meal["kind"] not in recognized_kinds
|
||||||
|
]
|
||||||
|
|
||||||
|
uncategorized = "" if not uncategorized_meals else \
|
||||||
|
UNCATEGORIZED_TEMPLATE.format(
|
||||||
|
format_items([
|
||||||
|
# Small hack: change "name" into "name (kind)"
|
||||||
|
{**meal, "name": "{} ({})".format(meal["name"], meal["kind"])}
|
||||||
|
for meal in uncategorized_meals
|
||||||
|
])
|
||||||
|
)
|
||||||
|
|
||||||
return RESTO_TEMPLATE.format(
|
return RESTO_TEMPLATE.format(
|
||||||
soup_table=table_for("soup"),
|
**{k: table_for(v) for k,v in RESTO_TABLES.items()},
|
||||||
meat_table=table_for("meat"),
|
uncategorized=uncategorized,
|
||||||
fish_table=table_for("fish"),
|
|
||||||
vegi_table=table_for("vegetarian"),
|
|
||||||
vegetable_table="\n".join(resto["vegetables"])
|
vegetable_table="\n".join(resto["vegetables"])
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue