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
This commit is contained in:
Glandos 2021-11-27 11:39:46 +01:00
parent a0d13e4081
commit 35da19f4e2
3 changed files with 25 additions and 12 deletions

View file

@ -246,13 +246,29 @@ class Project(db.Model):
def get_bills(self): def get_bills(self):
"""Return the list of bills related to this project""" """Return the list of bills related to this project"""
return self.ordered_bills(self.get_bills_unordered())
def ordered_bills(self, query):
return ( return (
self.get_bills_unordered() query.order_by(Bill.date.desc())
.order_by(Bill.date.desc())
.order_by(Bill.creation_date.desc()) .order_by(Bill.creation_date.desc())
.order_by(Bill.id.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): def get_member_bills(self, member_id):
"""Return the list of bills related to a specific member""" """Return the list of bills related to a specific member"""
return ( return (

View file

@ -1,7 +1,7 @@
{% extends "sidebar_table_layout.html" %} {% extends "sidebar_table_layout.html" %}
{%- macro bill_amount(bill, currency=bill.original_currency, amount=bill.amount) %} {%- macro bill_amount_weighted(bill, weights, currency=bill.original_currency, amount=bill.amount) %}
{{ amount|currency(currency) }} ({{ _("%(amount)s each", amount=bill.pay_each_default(amount)|currency(currency)) }}) {{ amount|currency(currency) }} ({{ _("%(amount)s each", amount=(amount / weights)|currency(currency)) }})
{% endmacro -%} {% endmacro -%}
{% block title %} - {{ g.project.name }}{% endblock %} {% block title %} - {{ g.project.name }}{% endblock %}
@ -109,7 +109,7 @@
</th><th>{{ _("Actions") }}</th></tr> </th><th>{{ _("Actions") }}</th></tr>
</thead> </thead>
<tbody> <tbody>
{% for bill in bills.items %} {% for (weights, bill) in bills.items %}
<tr owers="{{bill.owers|join(',','id')}}" payer="{{bill.payer.id}}"> <tr owers="{{bill.owers|join(',','id')}}" payer="{{bill.payer.id}}">
<td> <td>
<span data-toggle="tooltip" data-placement="top" <span data-toggle="tooltip" data-placement="top"
@ -128,8 +128,8 @@
{%- endif %}</td> {%- endif %}</td>
<td> <td>
<span data-toggle="tooltip" data-placement="top" <span data-toggle="tooltip" data-placement="top"
title="{{ bill_amount(bill, g.project.default_currency, bill.converted_amount) if bill.original_currency != g.project.default_currency else '' }}"> title="{{ bill_amount_weighted(bill, weights, g.project.default_currency, bill.converted_amount) if bill.original_currency != g.project.default_currency else '' }}">
{{ bill_amount(bill) }} {{ bill_amount_weighted(bill, weights) }}
</span> </span>
</td> </td>
<td class="bill-actions"> <td class="bill-actions">

View file

@ -654,11 +654,8 @@ def list_bills():
# set the last selected payer as default choice if exists # set the last selected payer as default choice if exists
if "last_selected_payer" in session: if "last_selected_payer" in session:
bill_form.payer.data = session["last_selected_payer"] bill_form.payer.data = session["last_selected_payer"]
# Preload the "owers" relationship for all bills bills = g.project.get_bill_weights_ordered().paginate(
bills = ( per_page=500, error_out=True
g.project.get_bills()
.options(orm.subqueryload(Bill.owers))
.paginate(per_page=100, error_out=True)
) )
return render_template( return render_template(