From 8c628718ff831248ec2e11decc8d5c871b810200 Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Mon, 28 Oct 2024 00:56:54 -0300 Subject: [PATCH] feat: add default transaction ordering function --- .../transactions/utils/default_ordering.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 app/apps/transactions/utils/default_ordering.py diff --git a/app/apps/transactions/utils/default_ordering.py b/app/apps/transactions/utils/default_ordering.py new file mode 100644 index 0000000..614772f --- /dev/null +++ b/app/apps/transactions/utils/default_ordering.py @@ -0,0 +1,25 @@ +from django.db.models import Case, When, Value, IntegerField, QuerySet +from django.utils import timezone + + +def default_order(queryset: QuerySet, extra_ordering=None) -> QuerySet: + if extra_ordering is None: + extra_ordering = list() + + 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) + + return queryset.annotate( + date_order=Case( + When(date=today, then=Value(0)), + When(date=tomorrow, then=Value(1)), + When(date=yesterday, then=Value(2)), + When(date__lte=next_7_days, date__gte=tomorrow, then=Value(3)), + When(date__gte=last_7_days, date__lte=today, then=Value(4)), + default=Value(5), + output_field=IntegerField(), + ) + ).order_by("date_order", *extra_ordering)