Introduce has_multiple_currencies

Factor get_bills in get_bills_unordered and use it wherever needed.
It should be faster, but completely unoticeable :)
This commit is contained in:
Glandos 2021-07-01 22:57:55 +02:00
parent 63bb7705d0
commit 45a4eab600
2 changed files with 17 additions and 10 deletions

View file

@ -140,7 +140,7 @@ class Project(db.Model):
"spent": sum( "spent": sum(
[ [
bill.pay_each() * member.weight bill.pay_each() * member.weight
for bill in self.get_bills().all() for bill in self.get_bills_unordered().all()
if member in bill.owers if member in bill.owers
] ]
), ),
@ -157,7 +157,7 @@ class Project(db.Model):
:rtype dict: :rtype dict:
""" """
monthly = defaultdict(lambda: defaultdict(float)) monthly = defaultdict(lambda: defaultdict(float))
for bill in self.get_bills().all(): for bill in self.get_bills_unordered().all():
monthly[bill.date.year][bill.date.month] += bill.converted_amount monthly[bill.date.year][bill.date.month] += bill.converted_amount
return monthly return monthly
@ -216,15 +216,25 @@ class Project(db.Model):
def has_bills(self): def has_bills(self):
"""return if the project do have bills or not""" """return if the project do have bills or not"""
return self.get_bills().count() > 0 return self.get_bills_unordered().count() > 0
def get_bills(self): def has_multiple_currencies(self):
"""Return the list of bills related to this project""" """Return if multiple currencies are used"""
return self.get_bills_unordered().group_by(Bill.original_currency).count() > 1
def get_bills_unordered(self):
"""Base query for bill list"""
return ( return (
Bill.query.join(Person, Project) Bill.query.join(Person, Project)
.filter(Bill.payer_id == Person.id) .filter(Bill.payer_id == Person.id)
.filter(Person.project_id == Project.id) .filter(Person.project_id == Project.id)
.filter(Project.id == self.id) .filter(Project.id == self.id)
)
def get_bills(self):
"""Return the list of bills related to this project"""
return (
self.get_bills_unordered()
.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())
@ -233,11 +243,8 @@ class Project(db.Model):
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 (
Bill.query.join(Person, Project) self.get_bills_unordered()
.filter(Bill.payer_id == Person.id)
.filter(Person.project_id == Project.id)
.filter(Person.id == member_id) .filter(Person.id == member_id)
.filter(Project.id == self.id)
.order_by(Bill.date.desc()) .order_by(Bill.date.desc())
.order_by(Bill.id.desc()) .order_by(Bill.id.desc())
) )

View file

@ -5,7 +5,7 @@
<thead><tr><th>{{ _("Project") }}</th><th>{{ _("Number of members") }}</th><th>{{ _("Number of bills") }}</th><th>{{_("Newest bill")}}</th><th>{{_("Oldest bill")}}</th><th>{{_("Actions")}}</th></tr></thead> <thead><tr><th>{{ _("Project") }}</th><th>{{ _("Number of members") }}</th><th>{{ _("Number of bills") }}</th><th>{{_("Newest bill")}}</th><th>{{_("Oldest bill")}}</th><th>{{_("Actions")}}</th></tr></thead>
<tbody>{% for project in projects|sort(attribute='name') %} <tbody>{% for project in projects|sort(attribute='name') %}
<tr> <tr>
<td><a href="{{ url_for(".list_bills", project_id=project.id) }}" title="{{ project.name }}">{{ project.name }}</a></td><td>{{ project.members | count }}</td><td>{{ project.get_bills().count() }}</td> <td><a href="{{ url_for(".list_bills", project_id=project.id) }}" title="{{ project.name }}">{{ project.name }}</a></td><td>{{ project.members | count }}</td><td>{{ project.get_bills_unordered().count() }}</td>
{% if project.has_bills() %} {% if project.has_bills() %}
<td>{{ project.get_bills().all()[0].date }}</td> <td>{{ project.get_bills().all()[0].date }}</td>
<td>{{ project.get_bills().all()[-1].date }}</td> <td>{{ project.get_bills().all()[-1].date }}</td>