diff --git a/app/models.py b/app/models.py index 418d4d2..f338157 100644 --- a/app/models.py +++ b/app/models.py @@ -1,4 +1,6 @@ from datetime import datetime +from collections import defaultdict + from app import db @@ -90,6 +92,17 @@ class Order(db.Model): def __repr__(self): return 'Order %s' % (self.location.name) + def group_by_user(self): + group = defaultdict(list) + for item in self.orders: + group[item.user_id] += [item.food] + return group + + def group_by_user_pay(self): + group = defaultdict(int) + for item in self.orders: + group[item.user] += item.food.price + return group class OrderItem(db.Model): id = db.Column(db.Integer, primary_key=True) diff --git a/app/templates/order.html b/app/templates/order.html index ab64a8b..3346ab4 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -20,6 +20,10 @@ {{ item.user.username }} - {{ item.food.name }} - {{ item.food.price|euro }} {% if item.can_delete(order.id, current_user.id) -%}{%- endif %}
{% endfor %} +

Debts

+ {% for key, value in total_payments.items() %} + {{ key.username }} - {{ value|euro }}
+ {% endfor %}

Order:

diff --git a/app/views/order.py b/app/views/order.py index 92b59e9..f912bda 100644 --- a/app/views/order.py +++ b/app/views/order.py @@ -41,7 +41,8 @@ def order(id): form = OrderItemForm() form.populate(order.location) total_price = sum([o.food.price for o in order.orders]) - return render_template('order.html', order=order, form=form, total_price=total_price) + total_payments = order.group_by_user_pay() + return render_template('order.html', order=order, form=form, total_price=total_price, total_payments=total_payments) return abort(404)