Add quarterly api endpoint

This commit is contained in:
Edgar P. Burkhart 2023-04-16 15:58:31 +02:00
parent cba2358c0b
commit 63908cd837
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 18 additions and 0 deletions

View file

@ -7,4 +7,5 @@ urlpatterns = [
path("categories", views.CategoryListView.as_view(), name="categories"), path("categories", views.CategoryListView.as_view(), name="categories"),
path("accounts", views.AccountListView.as_view(), name="accounts"), path("accounts", views.AccountListView.as_view(), name="accounts"),
path("snapshots", views.SnapshotListView.as_view(), name="snapshots"), path("snapshots", views.SnapshotListView.as_view(), name="snapshots"),
path("history", views.HistoryView.as_view(), name="history"),
] ]

View file

@ -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.http import JsonResponse
from django.views import View from django.views import View
from django.views.generic.list import MultipleObjectMixin from django.views.generic.list import MultipleObjectMixin
@ -32,3 +34,18 @@ class SnapshotListView(UserMixin, MultipleObjectMixin, View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
return JsonResponse({"snapshots": list(self.get_queryset().values())}) 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"))
)
}
)