diff --git a/app/apps/monthly_overview/views/main.py b/app/apps/monthly_overview/views/main.py index e71f51e..4204626 100644 --- a/app/apps/monthly_overview/views/main.py +++ b/app/apps/monthly_overview/views/main.py @@ -17,6 +17,7 @@ from apps.common.decorators.htmx import only_htmx from apps.common.functions.dates import remaining_days_in_month from apps.transactions.filters import TransactionsFilter from apps.transactions.models import Transaction +from apps.transactions.utils.default_ordering import default_order @login_required @@ -61,13 +62,8 @@ def monthly_overview(request, month: int, year: int): @login_required @require_http_methods(["GET"]) 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) + order = request.GET.get("order") - # TO-DO Improve date-order f = TransactionsFilter(request.GET) transactions_filtered = ( f.qs.filter() @@ -75,18 +71,6 @@ def transactions_list(request, month: int, year: int): reference_date__year=year, reference_date__month=month, ) - .annotate( - date_order=Case( - 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(), - ) - ) - .order_by("date_order", "date", "id") .prefetch_related( "account", "account__group", @@ -97,6 +81,18 @@ def transactions_list(request, month: int, year: int): "installment_plan", ) ) + + if order == "default": + transactions_filtered = default_order( + transactions_filtered, extra_ordering=["date", "id"] + ) + elif order == "newer": + transactions_filtered = transactions_filtered.order_by("-date", "id") + elif order == "older": + transactions_filtered = transactions_filtered.order_by("date", "id") + else: + transactions_filtered = transactions_filtered.order_by("date", "id") + return render( request, "monthly_overview/fragments/list.html", diff --git a/app/apps/transactions/views/transactions.py b/app/apps/transactions/views/transactions.py index 00015db..4cf4b13 100644 --- a/app/apps/transactions/views/transactions.py +++ b/app/apps/transactions/views/transactions.py @@ -14,6 +14,7 @@ from apps.common.decorators.htmx import only_htmx from apps.transactions.forms import TransactionForm, TransferForm from apps.transactions.models import Transaction from apps.transactions.filters import TransactionsFilter +from apps.transactions.utils.default_ordering import default_order @only_htmx @@ -164,19 +165,26 @@ def transaction_all_index(request): @login_required @require_http_methods(["GET"]) def transaction_all_list(request): - transactions = ( - Transaction.objects.prefetch_related( - "account", - "account__group", - "category", - "tags", - "account__exchange_currency", - "account__currency", - "installment_plan", - ) - .all() - .order_by("date") - ) + order = request.GET.get("order") + + transactions = Transaction.objects.prefetch_related( + "account", + "account__group", + "category", + "tags", + "account__exchange_currency", + "account__currency", + "installment_plan", + ).all() + + if order == "default": + transactions = default_order(transactions, extra_ordering=["date", "id"]) + elif order == "newer": + transactions = transactions.order_by("-date", "id") + elif order == "older": + transactions = transactions.order_by("date", "id") + else: + transactions = transactions.order_by("date", "id") f = TransactionsFilter(request.GET, queryset=transactions) diff --git a/app/templates/monthly_overview/pages/overview.html b/app/templates/monthly_overview/pages/overview.html index 2e820f7..b1d7bd2 100644 --- a/app/templates/monthly_overview/pages/overview.html +++ b/app/templates/monthly_overview/pages/overview.html @@ -103,25 +103,35 @@
{# Filter transactions#}
-
+
+
+
+ + +
+
+ hx-trigger="load, updated from:window" hx-include="#filter, #order">
diff --git a/app/templates/transactions/fragments/list_all.html b/app/templates/transactions/fragments/list_all.html index 66e8b21..744a753 100644 --- a/app/templates/transactions/fragments/list_all.html +++ b/app/templates/transactions/fragments/list_all.html @@ -1,4 +1,8 @@ +{% load natural %} {% load i18n %} + +{% regroup page_obj by date|customnaturaldate as transactions_by_date %} +
- {% for transaction in page_obj %} - - {% empty %} -
-
-
- -

{% translate "No transactions found" %}

-

{% translate "Try adding one" %}

+ {% for x in transactions_by_date %} +
+ +
+
+ {% for transaction in x.list %} + + {% endfor %} +
-
- {% endfor %} + {% empty %} +
+
+
+ +

{% translate "No transactions found" %}

+

{% translate "Try adding one" %}

+
+
+
+{% endfor %} + {% if page_obj.has_other_pages %}
+
+
+ + +
+
+ hx-trigger="load, updated from:window" hx-include="#filter, #page, #order">