diff --git a/nummi/api/urls.py b/nummi/api/urls.py index 1579b84..cb74a80 100644 --- a/nummi/api/urls.py +++ b/nummi/api/urls.py @@ -7,4 +7,5 @@ urlpatterns = [ path("categories", views.CategoryListView.as_view(), name="categories"), path("accounts", views.AccountListView.as_view(), name="accounts"), path("snapshots", views.SnapshotListView.as_view(), name="snapshots"), + path("history", views.HistoryView.as_view(), name="history"), ] diff --git a/nummi/api/views.py b/nummi/api/views.py index ce1749a..876d5fd 100644 --- a/nummi/api/views.py +++ b/nummi/api/views.py @@ -1,3 +1,5 @@ +from django.db.models import Sum +from django.db.models.functions import ExtractQuarter, ExtractYear from django.http import JsonResponse from django.views import View from django.views.generic.list import MultipleObjectMixin @@ -32,3 +34,18 @@ class SnapshotListView(UserMixin, MultipleObjectMixin, View): def get(self, request, *args, **kwargs): return JsonResponse({"snapshots": list(self.get_queryset().values())}) + + +class HistoryView(UserMixin, MultipleObjectMixin, View): + model = Transaction + + def get(self, request, *args, **kwargs): + return JsonResponse( + { + "data": list( + self.get_queryset() + .values("category__name", quarter=ExtractQuarter("date"), year=ExtractYear("date")) + .annotate(Sum("value")) + ) + } + )