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" "Script for everything Order related in the database"
import typing import typing
from datetime import datetime from datetime import datetime
from collections import defaultdict
from utils import first from utils import first
from hlds.definitions import location_definitions 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 ""))) 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 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: for item in self.items:
if item.dish_name not in group: group[item.dish_name][item.comment].append(item)
group[item.dish_name] = []
group[item.dish_name].append(item) return sorted(
(
for _dish_name, order_items in group.items(): dish_name,
order_items.sort(key=lambda order_item: ( # Amount of items of this dish
(order_item.comment or " No comment") + sum(map(len, comment_group.values())),
(order_item.for_name or "") sorted(
)) (
(list(map(str.strip, comment.split(";"))) if comment else []),
return list(sorted(group.items())) 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: def is_closed(self) -> bool:
return self.stoptime and datetime.now() > self.stoptime return self.stoptime and datetime.now() > self.stoptime

View file

@ -74,12 +74,28 @@ h2 {
} }
.comments { .comments {
padding-left: 2em; padding-left: 1.5em;
list-style-type: none;
} }
.comments li { .comments li {
margin: 0.5em 0 0; 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 { .total {
border-top: var(--dashes); border-top: var(--dashes);
text-align: center; text-align: center;

View file

@ -230,24 +230,27 @@
<div class="box" id="per_dish"> <div class="box" id="per_dish">
<h3>Ordered dishes</h3> <h3>Ordered dishes</h3>
{% for dish_name, dish_order_items in order.group_by_dish() -%} {% for dish_name, dish_quantity, dish_comment_groups in order.group_by_dish() -%}
{% set has_comments = dish_order_items | map(attribute="comment") | any -%} {% 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 }}"> <div class="dish {{ 'spacecake no_comments' if not has_comments }}">
<h4> <h4>
<span class="quantity">{{ dish_order_items | length }}</span> × <span class="quantity">{{ dish_quantity }}</span> ×
{{ dish_name }} {{ dish_name }}
</h4> </h4>
{% if has_comments -%} {% if has_comments -%}
<ul class="comments"> <ul class="comments">
{% for item in dish_order_items -%} {% for comment, items in dish_comment_groups -%}
<li class="spacecake">{% if item["comment"] %}<span>{{ item["comment"] }}</span> <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> {% 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 %} {% endfor %}
</ul> </ul>
{% else %} {% 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 %} {%- endif %}
</p> </p>
@ -437,6 +440,10 @@ li {
color: var(--gray2); color: var(--gray2);
text-align: right; text-align: right;
} }
#per_dish .comments {
padding-left: 1.5em;
list-style-type: none;
}
#per_person li { #per_person li {

View file

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