From d7dc92699262fe4b7c39602179277a9287ff897d Mon Sep 17 00:00:00 2001 From: Midgard Date: Mon, 21 Jun 2021 02:05:11 +0200 Subject: [PATCH] Aggregate comments --- app/models/order.py | 34 +++++++++++++++++++++------------- app/static/css/shop_view.css | 18 +++++++++++++++++- app/templates/order.html | 21 ++++++++++++++------- app/templates/order_items.html | 13 ++++++++----- 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/app/models/order.py b/app/models/order.py index 9b0c721..ec5d3fb 100644 --- a/app/models/order.py +++ b/app/models/order.py @@ -1,6 +1,7 @@ "Script for everything Order related in the database" import typing from datetime import datetime +from collections import defaultdict from utils import first from hlds.definitions import location_definitions @@ -69,23 +70,30 @@ class Order(db.Model): return list(sorted(group.items(), key=lambda t: (t[0] or "", t[1] or ""))) - def group_by_dish(self) -> typing.List[typing.Tuple[str, typing.List]]: + def group_by_dish(self) \ + -> typing.List[typing.Tuple[str, int, typing.List[typing.Tuple[typing.List[str], typing.List]]]]: "Group items of an Order by dish" - group: typing.Dict[str, typing.List] = dict() + group: typing.Dict[str, typing.Dict[str, typing.List]] = \ + defaultdict(lambda: defaultdict(list)) for item in self.items: - if item.dish_name not in group: - group[item.dish_name] = [] + group[item.dish_name][item.comment].append(item) - group[item.dish_name].append(item) - - for _dish_name, order_items in group.items(): - order_items.sort(key=lambda order_item: ( - (order_item.comment or " No comment") + - (order_item.for_name or "") - )) - - return list(sorted(group.items())) + return sorted( + ( + dish_name, + # Amount of items of this dish + sum(map(len, comment_group.values())), + sorted( + ( + (list(map(str.strip, comment.split(";"))) if comment else []), + sorted(items, key=lambda x: (x.for_name or "")) + ) + for comment, items in comment_group.items() + ) + ) + for dish_name, comment_group in group.items() + ) def is_closed(self) -> bool: return self.stoptime and datetime.now() > self.stoptime diff --git a/app/static/css/shop_view.css b/app/static/css/shop_view.css index ed75a75..4ef8b29 100644 --- a/app/static/css/shop_view.css +++ b/app/static/css/shop_view.css @@ -74,12 +74,28 @@ h2 { } .comments { - padding-left: 2em; + padding-left: 1.5em; + list-style-type: none; } .comments li { margin: 0.5em 0 0; } +.comment_part { + background-color: #f7f7f7; + border: 1px solid #ddd; + padding: 0.1em 0.5em; + margin-top: 2px; + margin-right: 0.4em; + border-radius: 1em; + display: inline-block; +} +.comment_part_separator { + display: inline-block; + width: 0; + overflow: hidden; +} + .total { border-top: var(--dashes); text-align: center; diff --git a/app/templates/order.html b/app/templates/order.html index eecef70..52f2720 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -230,24 +230,27 @@

Ordered dishes

- {% for dish_name, dish_order_items in order.group_by_dish() -%} - {% set has_comments = dish_order_items | map(attribute="comment") | any -%} + {% for dish_name, dish_quantity, dish_comment_groups in order.group_by_dish() -%} + {% set has_comments = dish_comment_groups | length > 1 or (dish_comment_groups | map("first") | any) -%}

- {{ dish_order_items | length }} × + {{ dish_quantity }} × {{ dish_name }}

{% if has_comments -%}
    - {% for item in dish_order_items -%} -
  • {% if item["comment"] %}{{ item["comment"] }} + {% for comment, items in dish_comment_groups -%} +
  • {{ items | length }} × + {% if comment %} + {% set semicolon = joiner(";") %} + {% for comment_part in comment %}{{ semicolon() }} {{ comment_part }}{% endfor %} {% else %}No comment - {% endif %} for {{ item.for_name }}
  • + {% endif %} for {{ items | map(attribute="for_name") | join(", ") }} {% endfor %}
{% else %} - for {{ dish_order_items | map(attribute="for_name") | join(", ") }} + for {{ dish_comment_groups[0][1] | map(attribute="for_name") | join(", ") }} {%- endif %}

@@ -437,6 +440,10 @@ li { color: var(--gray2); text-align: right; } +#per_dish .comments { + padding-left: 1.5em; + list-style-type: none; +} #per_person li { diff --git a/app/templates/order_items.html b/app/templates/order_items.html index 6d633fb..388c1ad 100644 --- a/app/templates/order_items.html +++ b/app/templates/order_items.html @@ -31,13 +31,16 @@ Haldis - Order {{ order.id }}
{% endif %} - {% for dish_name, dish_order_items in order.group_by_dish() -%} + {% for dish_name, dish_quantity, dish_comment_groups in order.group_by_dish() -%}
-

{{ dish_order_items | length }} × {{ dish_name }}

- {% if dish_order_items | map(attribute="comment") | any -%} +

{{ dish_quantity }} × {{ dish_name }}

+ {% if dish_comment_groups | map("first") | any -%}
    - {% for item in dish_order_items -%} -
  • {% if item["comment"] %}{{ item["comment"] }} + {% for comment, items in dish_comment_groups -%} +
  • {{ items | length }} × + {% if comment %} + {% set semicolon = joiner(";") %} + {% for comment_part in comment %}{{ semicolon() }} {{ comment_part }}{% endfor %} {% else %}No comment {% endif %}
  • {% endfor %}