Sort comments in order views

This commit is contained in:
Midgard 2020-03-10 18:39:11 +01:00
parent 289b36b918
commit 1fa38d08c6
Signed by: midgard
GPG key ID: 511C112F1331BBB4
2 changed files with 7 additions and 3 deletions

View file

@ -56,15 +56,19 @@ class Order(db.Model):
return group 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 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: for item in self.items:
dish = group.get(item.dish_name, dict()) dish = group.get(item.dish_name, dict())
dish["count"] = dish.get("count", 0) + 1 dish["count"] = dish.get("count", 0) + 1
dish["comments"] = dish.get("comments", []) + [item.comment] dish["comments"] = dish.get("comments", []) + [item.comment]
group[item.dish_name] = dish group[item.dish_name] = dish
if sort_comments:
for _dish_name, dish_props in group.items():
dish_props["comments"].sort()
return group return group
def is_closed(self) -> bool: def is_closed(self) -> bool:

View file

@ -33,7 +33,7 @@ Haldis - Order {{ order.id }}
</div> </div>
{% endif %} {% endif %}
{% for key, value in order.group_by_dish().items() -%} {% for key, value in order.group_by_dish(True).items() -%}
<div class="dish"> <div class="dish">
<h2><span class="quantity">{{ value["count"] }}</span> × {{ key }}</h2> <h2><span class="quantity">{{ value["count"] }}</span> × {{ key }}</h2>
{% if value["comments"]|any -%} {% if value["comments"]|any -%}