From 35da19f4e2168d1a23d7a4385cfe19d18314666f Mon Sep 17 00:00:00 2001 From: Glandos Date: Sat, 27 Nov 2021 11:39:46 +0100 Subject: [PATCH] get weight sum along with bills to scale otherwise, we need to get the weight sum for each displayed bill. Here, we are much more scalable --- ihatemoney/models.py | 20 ++++++++++++++++++-- ihatemoney/templates/list_bills.html | 10 +++++----- ihatemoney/web.py | 7 ++----- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/ihatemoney/models.py b/ihatemoney/models.py index 473e7c0b..a7a7b170 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -246,13 +246,29 @@ class Project(db.Model): def get_bills(self): """Return the list of bills related to this project""" + return self.ordered_bills(self.get_bills_unordered()) + + def ordered_bills(self, query): return ( - self.get_bills_unordered() - .order_by(Bill.date.desc()) + query.order_by(Bill.date.desc()) .order_by(Bill.creation_date.desc()) .order_by(Bill.id.desc()) ) + def get_bill_weights(self): + return ( + db.session.query(func.sum(Person.weight), Bill) + .options(orm.subqueryload(Bill.owers)) + .select_from(Person) + .join(billowers, Bill, Project) + .filter(Person.project_id == Project.id) + .filter(Project.id == self.id) + .group_by(Bill.id) + ) + + def get_bill_weights_ordered(self): + return self.ordered_bills(self.get_bill_weights()) + def get_member_bills(self, member_id): """Return the list of bills related to a specific member""" return ( diff --git a/ihatemoney/templates/list_bills.html b/ihatemoney/templates/list_bills.html index dda16f2e..efe55288 100644 --- a/ihatemoney/templates/list_bills.html +++ b/ihatemoney/templates/list_bills.html @@ -1,7 +1,7 @@ {% extends "sidebar_table_layout.html" %} -{%- macro bill_amount(bill, currency=bill.original_currency, amount=bill.amount) %} - {{ amount|currency(currency) }} ({{ _("%(amount)s each", amount=bill.pay_each_default(amount)|currency(currency)) }}) +{%- macro bill_amount_weighted(bill, weights, currency=bill.original_currency, amount=bill.amount) %} + {{ amount|currency(currency) }} ({{ _("%(amount)s each", amount=(amount / weights)|currency(currency)) }}) {% endmacro -%} {% block title %} - {{ g.project.name }}{% endblock %} @@ -109,7 +109,7 @@ {{ _("Actions") }} - {% for bill in bills.items %} + {% for (weights, bill) in bills.items %} - {{ bill_amount(bill) }} + title="{{ bill_amount_weighted(bill, weights, g.project.default_currency, bill.converted_amount) if bill.original_currency != g.project.default_currency else '' }}"> + {{ bill_amount_weighted(bill, weights) }} diff --git a/ihatemoney/web.py b/ihatemoney/web.py index d9e7ec08..8ba6efea 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -654,11 +654,8 @@ def list_bills(): # set the last selected payer as default choice if exists if "last_selected_payer" in session: bill_form.payer.data = session["last_selected_payer"] - # Preload the "owers" relationship for all bills - bills = ( - g.project.get_bills() - .options(orm.subqueryload(Bill.owers)) - .paginate(per_page=100, error_out=True) + bills = g.project.get_bill_weights_ordered().paginate( + per_page=500, error_out=True ) return render_template(