Aggregate comments

This commit is contained in:
Midgard 2021-06-21 02:05:11 +02:00
parent 1ff8ceb521
commit 16bd9243d4
Signed by: midgard
GPG key ID: 511C112F1331BBB4
4 changed files with 60 additions and 26 deletions

View file

@ -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

View file

@ -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;

View file

@ -230,24 +230,27 @@
<div class="box" id="per_dish">
<h3>Ordered dishes</h3>
{% 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) -%}
<div class="dish {{ 'spacecake no_comments' if not has_comments }}">
<h4>
<span class="quantity">{{ dish_order_items | length }}</span> ×
<span class="quantity">{{ dish_quantity }}</span> ×
{{ dish_name }}
</h4>
{% if has_comments -%}
<ul class="comments">
{% for item in dish_order_items -%}
<li class="spacecake">{% if item["comment"] %}<span>{{ item["comment"] }}</span>
{% for comment, items in dish_comment_groups -%}
<li class="spacecake"><span class="comment"><span class="quantity">{{ items | length }}</span> ×
{% if comment %}
{% set semicolon = joiner(";") %}
{% for comment_part in comment %}<span class="comment_part_separator">{{ semicolon() }} </span><span class="comment_part">{{ comment_part }}</span>{% endfor %}
{% else %}<i>No comment</i>
{% endif %}<span class="spacer"> </span><span class="item_for">for {{ item.for_name }}</span></li>
{% endif %}</span><span class="spacer"> </span><span class="item_for">for {{ items | map(attribute="for_name") | join(", ") }}</span></li>
{% endfor %}
</ul>
{% else %}
<span class="spacer"> </span><span class="item_for">for {{ dish_order_items | map(attribute="for_name") | join(", ") }}</span>
<span class="spacer"> </span><span class="item_for">for {{ dish_comment_groups[0][1] | map(attribute="for_name") | join(", ") }}</span>
{%- endif %}
</p>
@ -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 {

View file

@ -31,13 +31,16 @@ Haldis - Order {{ order.id }}
</div>
{% 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() -%}
<div class="dish">
<h2><span class="quantity">{{ dish_order_items | length }}</span> × {{ dish_name }}</h2>
{% if dish_order_items | map(attribute="comment") | any -%}
<h2><span class="quantity">{{ dish_quantity }}</span> × {{ dish_name }}</h2>
{% if dish_comment_groups | map("first") | any -%}
<ul class="comments">
{% for item in dish_order_items -%}
<li>{% if item["comment"] %}{{ item["comment"] }}
{% for comment, items in dish_comment_groups -%}
<li><span class="quantity">{{ items | length }}</span> ×
{% if comment %}
{% set semicolon = joiner(";") %}
{% for comment_part in comment %}<span class="comment_part_separator">{{ semicolon() }} </span><span class="comment_part">{{ comment_part }}</span>{% endfor %}
{% else %}<i>No comment</i>
{% endif %}</li>
{% endfor %}