Merge pull request #9 from ZeusWPI/resto-veg-and-unrecognized

Resto veg and unrecognized
This commit is contained in:
redfast00 2019-10-03 16:05:23 +02:00 committed by GitHub
commit ab6e6ba816
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -244,14 +244,26 @@ RESTO_TEMPLATE = """
## Visch
{fish_table}
## Keuneteetn
## Vegetoarisch
{vegi_table}
{uncategorized}
## Groensels
{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'])
def resto_menu():
today = datetime.today()
@ -262,11 +274,14 @@ def resto_menu():
if not resto["open"]:
return 'De resto is vandaag gesloten.'
else:
def table_for(kind):
items = [meal for meal in resto["meals"] if meal["kind"] == kind]
if len(items) == 0:
def table_for(kinds):
items = [meal for meal in resto["meals"] if meal["kind"] in kinds]
return format_items(items)
def format_items(items):
if not items:
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(
name=item["name"],
width=maxwidth + 2,
@ -274,11 +289,24 @@ def resto_menu():
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(
soup_table=table_for("soup"),
meat_table=table_for("meat"),
fish_table=table_for("fish"),
vegi_table=table_for("vegetarian"),
**{k: table_for(v) for k,v in RESTO_TABLES.items()},
uncategorized=uncategorized,
vegetable_table="\n".join(resto["vegetables"])
)