narrow displayed time range
This commit is contained in:
@@ -68,9 +68,11 @@ class Statistics:
|
|||||||
def __init__(self, user):
|
def __init__(self, user):
|
||||||
self.user = user
|
self.user = user
|
||||||
self.today = current_date()
|
self.today = current_date()
|
||||||
self.start = self.today - relativedelta(months=6)
|
self.calc_start = self.today - relativedelta(months=6)
|
||||||
self.end = self.today + relativedelta(months=6)
|
self.calc_end = self.today + relativedelta(months=6)
|
||||||
self.calc_start = self.start
|
self.real_calc_start = self.calc_start
|
||||||
|
self.display_start = self.today - relativedelta(months=1)
|
||||||
|
self.display_end = self.today + relativedelta(months=2)
|
||||||
self.balances = []
|
self.balances = []
|
||||||
self.transactions = []
|
self.transactions = []
|
||||||
self.actual_transactions = []
|
self.actual_transactions = []
|
||||||
@@ -93,29 +95,29 @@ class Statistics:
|
|||||||
|
|
||||||
def _fetch_relevant_balances(self):
|
def _fetch_relevant_balances(self):
|
||||||
self.balances = []
|
self.balances = []
|
||||||
balances_until_start = self.user.balances.filter(date__lte=self.start)
|
balances_until_start = self.user.balances.filter(date__lte=self.calc_start)
|
||||||
if balances_until_start.exists():
|
if balances_until_start.exists():
|
||||||
self.balances.append(balances_until_start.latest("date"))
|
self.balances.append(balances_until_start.latest("date"))
|
||||||
other_balances = self.user.balances.filter(date__gt=self.start, date__lte=self.end).order_by("date")
|
other_balances = self.user.balances.filter(date__gt=self.calc_start, date__lte=self.calc_end).order_by("date")
|
||||||
self.balances += list(other_balances)
|
self.balances += list(other_balances)
|
||||||
if self.balances:
|
if self.balances:
|
||||||
self.calc_start = self.balances[0].date
|
self.real_calc_start = self.balances[0].date
|
||||||
|
|
||||||
def _fetch_relevant_transactions(self):
|
def _fetch_relevant_transactions(self):
|
||||||
one_time_in_range = Q(
|
one_time_in_range = Q(
|
||||||
booking_date__gte=self.calc_start,
|
booking_date__gte=self.real_calc_start,
|
||||||
booking_date__lte=self.end,
|
booking_date__lte=self.calc_end,
|
||||||
recurring_months__isnull=True,
|
recurring_months__isnull=True,
|
||||||
)
|
)
|
||||||
endlessly_recurring = Q(
|
endlessly_recurring = Q(
|
||||||
booking_date__lte=self.end,
|
booking_date__lte=self.calc_end,
|
||||||
recurring_months__isnull=False,
|
recurring_months__isnull=False,
|
||||||
not_recurring_after__isnull=True,
|
not_recurring_after__isnull=True,
|
||||||
)
|
)
|
||||||
recurring_and_ending_in_range = Q(
|
recurring_and_ending_in_range = Q(
|
||||||
booking_date__lte=self.end,
|
booking_date__lte=self.calc_end,
|
||||||
recurring_months__isnull=False,
|
recurring_months__isnull=False,
|
||||||
not_recurring_after__gte=self.calc_start,
|
not_recurring_after__gte=self.real_calc_start,
|
||||||
)
|
)
|
||||||
transactions = self.user.transactions.filter(
|
transactions = self.user.transactions.filter(
|
||||||
one_time_in_range | endlessly_recurring | recurring_and_ending_in_range
|
one_time_in_range | endlessly_recurring | recurring_and_ending_in_range
|
||||||
@@ -128,9 +130,9 @@ class Statistics:
|
|||||||
if trans.recurring_months:
|
if trans.recurring_months:
|
||||||
iter_date = trans.booking_date
|
iter_date = trans.booking_date
|
||||||
iter_step = 1
|
iter_step = 1
|
||||||
limit = min(trans.not_recurring_after, self.end) if trans.not_recurring_after else self.end
|
limit = min(trans.not_recurring_after, self.calc_end) if trans.not_recurring_after else self.calc_end
|
||||||
while iter_date < limit:
|
while iter_date < limit:
|
||||||
if iter_date >= self.calc_start:
|
if iter_date >= self.real_calc_start:
|
||||||
actual_transactions.append(ActualTransaction(
|
actual_transactions.append(ActualTransaction(
|
||||||
date=iter_date,
|
date=iter_date,
|
||||||
subject=f"{trans.subject} #{iter_step}",
|
subject=f"{trans.subject} #{iter_step}",
|
||||||
@@ -148,9 +150,9 @@ class Statistics:
|
|||||||
|
|
||||||
def _calculate_daily_stats(self):
|
def _calculate_daily_stats(self):
|
||||||
self.daily_stats = []
|
self.daily_stats = []
|
||||||
iter_date = self.calc_start
|
iter_date = self.real_calc_start
|
||||||
iter_amount = None
|
iter_amount = None
|
||||||
while iter_date < self.end:
|
while iter_date < self.calc_end:
|
||||||
balance_amount, actual_transactions = None, []
|
balance_amount, actual_transactions = None, []
|
||||||
|
|
||||||
relevant_balances = [bal for bal in self.balances if bal.date == iter_date]
|
relevant_balances = [bal for bal in self.balances if bal.date == iter_date]
|
||||||
@@ -176,7 +178,9 @@ class Statistics:
|
|||||||
iter_date += relativedelta(days=1)
|
iter_date += relativedelta(days=1)
|
||||||
|
|
||||||
def _calculate_daily_amount_scale(self):
|
def _calculate_daily_amount_scale(self):
|
||||||
amounts = [s.resulting_amount for s in self.daily_stats if s.resulting_amount is not None]
|
amounts = [s.resulting_amount for s in self.daily_stats
|
||||||
|
if s.resulting_amount is not None
|
||||||
|
and self.display_start <= s.date <= self.display_end]
|
||||||
if amounts:
|
if amounts:
|
||||||
max_amount = max(amounts)
|
max_amount = max(amounts)
|
||||||
scale = _floor_to_first_two_places(max_amount * Decimal("1.25"))
|
scale = _floor_to_first_two_places(max_amount * Decimal("1.25"))
|
||||||
@@ -222,4 +226,4 @@ class Statistics:
|
|||||||
self.avg_monthly_irregular_expenses = self.avg_daily_irregular_expenses * days_per_month
|
self.avg_monthly_irregular_expenses = self.avg_daily_irregular_expenses * days_per_month
|
||||||
|
|
||||||
def get_daily_stats_in_range(self):
|
def get_daily_stats_in_range(self):
|
||||||
return [s for s in self.daily_stats if self.start <= s.date <= self.end]
|
return [s for s in self.daily_stats if self.display_start <= s.date <= self.display_end]
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ def index(request):
|
|||||||
user = request.user
|
user = request.user
|
||||||
statistics = Statistics(user)
|
statistics = Statistics(user)
|
||||||
context = {
|
context = {
|
||||||
"start": statistics.start,
|
"start": statistics.display_start,
|
||||||
"end": statistics.end,
|
"end": statistics.display_end,
|
||||||
"balances": statistics.balances,
|
"balances": statistics.balances,
|
||||||
"transactions": statistics.transactions,
|
"transactions": statistics.transactions,
|
||||||
"actual_transactions": statistics.actual_transactions,
|
"actual_transactions": statistics.actual_transactions,
|
||||||
|
|||||||
Reference in New Issue
Block a user