From 1fa38d08c682c82f82a1f4988988a58835082748 Mon Sep 17 00:00:00 2001 From: Midgard Date: Tue, 10 Mar 2020 18:39:11 +0100 Subject: [PATCH] Sort comments in order views --- app/models/order.py | 8 ++++++-- app/templates/order_items.html | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/models/order.py b/app/models/order.py index 360604c..d4f3fcf 100644 --- a/app/models/order.py +++ b/app/models/order.py @@ -56,15 +56,19 @@ class Order(db.Model): return group - def group_by_dish(self) -> typing.Dict[str, typing.Any]: + def group_by_dish(self, sort_comments=False) -> typing.Dict[str, typing.Dict[str, typing.Any]]: "Group items of an Order by dish" - group: typing.Dict[str, typing.Any] = dict() + group: typing.Dict[str, typing.Dict[str, typing.Any]] = dict() for item in self.items: dish = group.get(item.dish_name, dict()) dish["count"] = dish.get("count", 0) + 1 dish["comments"] = dish.get("comments", []) + [item.comment] group[item.dish_name] = dish + if sort_comments: + for _dish_name, dish_props in group.items(): + dish_props["comments"].sort() + return group def is_closed(self) -> bool: diff --git a/app/templates/order_items.html b/app/templates/order_items.html index 12b0231..917c4aa 100644 --- a/app/templates/order_items.html +++ b/app/templates/order_items.html @@ -33,7 +33,7 @@ Haldis - Order {{ order.id }} {% endif %} - {% for key, value in order.group_by_dish().items() -%} + {% for key, value in order.group_by_dish(True).items() -%}

{{ value["count"] }} × {{ key }}

{% if value["comments"]|any -%}