Compare commits
3 commits
Author | SHA1 | Date | |
---|---|---|---|
63908cd837 | |||
cba2358c0b | |||
887d2074dc |
9 changed files with 78 additions and 0 deletions
0
nummi/api/__init__.py
Normal file
0
nummi/api/__init__.py
Normal file
3
nummi/api/admin.py
Normal file
3
nummi/api/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
nummi/api/apps.py
Normal file
6
nummi/api/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "api"
|
0
nummi/api/migrations/__init__.py
Normal file
0
nummi/api/migrations/__init__.py
Normal file
3
nummi/api/models.py
Normal file
3
nummi/api/models.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
3
nummi/api/tests.py
Normal file
3
nummi/api/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
11
nummi/api/urls.py
Normal file
11
nummi/api/urls.py
Normal 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
51
nummi/api/views.py
Normal 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"))
|
||||
)
|
||||
}
|
||||
)
|
|
@ -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,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue