Add quarterly api endpoint
This commit is contained in:
parent
cba2358c0b
commit
63908cd837
2 changed files with 18 additions and 0 deletions
|
@ -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"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -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"))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue