Added totals

This commit is contained in:
Feliciaan De Palmenaer 2015-03-28 01:31:04 +01:00
parent 7514396669
commit a0b79b68ac
3 changed files with 19 additions and 1 deletions

View file

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

View file

@ -20,6 +20,10 @@
{{ item.user.username }} - {{ item.food.name }} - {{ item.food.price|euro }}
{% if item.can_delete(order.id, current_user.id) -%}<a href="{{ url_for('.delete_item', order_id=order.id, item_id=item.id) }}"><span class="glyphicon glyphicon-remove"></span></a>{%- endif %}<br/>
{% endfor %}
<h3>Debts</h3>
{% for key, value in total_payments.items() %}
{{ key.username }} - {{ value|euro }}<br/>
{% endfor %}
</div>
<div class="col-md-push-1 col-md-4">
<h4>Order:</h4>

View file

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