From d2cd115751a734d072ae630fe41e97c28c00bc95 Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Wed, 9 Oct 2024 22:31:08 -0300 Subject: [PATCH] feat: improve monthly transactions ordering --- app/apps/monthly_overview/views/main.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/apps/monthly_overview/views/main.py b/app/apps/monthly_overview/views/main.py index 21ae82b..8c3f6cd 100644 --- a/app/apps/monthly_overview/views/main.py +++ b/app/apps/monthly_overview/views/main.py @@ -74,7 +74,10 @@ def transactions_list(request, month: int, year: int): today = timezone.localdate(timezone.now()) yesterday = today - timezone.timedelta(days=1) tomorrow = today + timezone.timedelta(days=1) + last_7_days = today - timezone.timedelta(days=7) + next_7_days = today + timezone.timedelta(days=7) + # TO-DO Improve date-order f = TransactionsFilter(request.GET) transactions_filtered = ( f.qs.filter() @@ -84,10 +87,12 @@ def transactions_list(request, month: int, year: int): ) .annotate( date_order=Case( - When(date=tomorrow, then=Value(0)), - When(date=today, then=Value(1)), - When(date=yesterday, then=Value(2)), - default=Value(3), + When(date__lte=next_7_days, date__gte=tomorrow, then=Value(0)), + When(date=tomorrow, then=Value(1)), + When(date=today, then=Value(2)), + When(date=yesterday, then=Value(3)), + When(date__gte=last_7_days, date__lte=today, then=Value(4)), + default=Value(5), output_field=IntegerField(), ) )