Skip to content

Commit

Permalink
Display monthly statistics for the range of months where the project …
Browse files Browse the repository at this point in the history
…was active

instead of the currently hard-coded "one year" range in the past starting
from today.
  • Loading branch information
Baptiste Jonglez committed Oct 20, 2021
1 parent 45f0f0e commit 89c4875
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 8 additions & 0 deletions ihatemoney/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ def get_member_bills(self, member_id):
.order_by(Bill.id.desc())
)

def get_most_recent_bill(self):
"""Returns the most recent bill (according to bill date) or None if there are no bills"""
return self.get_bills_unordered().order_by(Bill.date.desc()).first()

def get_least_recent_bill(self):
"""Returns the least recent bill (according to bill date) or None if there are no bills"""
return self.get_bills_unordered().order_by(Bill.date.asc()).first()

def get_pretty_bills(self, export_format="json"):
"""Return a list of project's bills with pretty formatting"""
bills = self.get_bills()
Expand Down
16 changes: 14 additions & 2 deletions ihatemoney/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
from datetime import datetime
from functools import wraps
import itertools
import json
import os

Expand Down Expand Up @@ -895,12 +896,23 @@ def strip_ip_addresses():
@main.route("/<project_id>/statistics")
def statistics():
"""Compute what each member has paid and spent and display it"""
today = datetime.now()
if g.project.has_bills():
first_date = g.project.get_least_recent_bill().date
last_date = g.project.get_most_recent_bill().date
else:
first_date = datetime.now() - relativedelta(months=12)
last_date = datetime.now()
# Infinite iterator towards the past
all_months = (last_date - relativedelta(months=i) for i in itertools.count())
# Stop when reaching one month before the first date
months = itertools.takewhile(
lambda x: x > first_date - relativedelta(months=1), all_months
)
return render_template(
"statistics.html",
members_stats=g.project.members_stats,
monthly_stats=g.project.monthly_stats,
months=[today - relativedelta(months=i) for i in range(12)],
months=months,
current_view="statistics",
)

Expand Down

0 comments on commit 89c4875

Please sign in to comment.