Compare commits

...
Sign in to create a new pull request.

3 commits
main ... api

9 changed files with 78 additions and 0 deletions

0
nummi/api/__init__.py Normal file
View file

3
nummi/api/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
nummi/api/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "api"

View file

3
nummi/api/models.py Normal file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
nummi/api/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

11
nummi/api/urls.py Normal file
View file

@ -0,0 +1,11 @@
from django.urls import path
from . import views
urlpatterns = [
path("transactions", views.TransactionListView.as_view(), name="transactions"),
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"),
]

51
nummi/api/views.py Normal file
View file

@ -0,0 +1,51 @@
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
from main.models import Account, Category, Snapshot, Transaction
from main.views import UserMixin
class TransactionListView(UserMixin, MultipleObjectMixin, View):
model = Transaction
def get(self, request, *args, **kwargs):
return JsonResponse({"transactions": list(self.get_queryset().values())})
class CategoryListView(UserMixin, MultipleObjectMixin, View):
model = Category
def get(self, request, *args, **kwargs):
return JsonResponse({"categories": list(self.get_queryset().values())})
class AccountListView(UserMixin, MultipleObjectMixin, View):
model = Account
def get(self, request, *args, **kwargs):
return JsonResponse({"accounts": list(self.get_queryset().values())})
class SnapshotListView(UserMixin, MultipleObjectMixin, View):
model = Snapshot
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"))
)
}
)

View file

@ -20,6 +20,7 @@ from django.urls import include, path
urlpatterns = i18n_patterns(
path("", include("main.urls")),
path("plot/", include("plot.urls")),
path("api/", include("api.urls")),
path("admin/", admin.site.urls),
prefix_default_language=False,
)