Split backend in applications
This commit is contained in:
parent
a0d0b5d594
commit
b05c3e6760
47 changed files with 1463 additions and 866 deletions
97
nummi/statement/views.py
Normal file
97
nummi/statement/views.py
Normal file
|
@ -0,0 +1,97 @@
|
|||
from account.models import Account
|
||||
from django.db import models
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse_lazy
|
||||
from main.views import NummiCreateView, NummiDeleteView, NummiListView, NummiUpdateView
|
||||
from transaction.views import TransactionListView
|
||||
|
||||
from .forms import StatementForm
|
||||
from .models import Statement
|
||||
|
||||
|
||||
class StatementCreateView(NummiCreateView):
|
||||
model = Statement
|
||||
form_class = StatementForm
|
||||
template_name = "main/form/statement.html"
|
||||
|
||||
def get_initial(self):
|
||||
_queryset = Account.objects.filter(user=self.request.user)
|
||||
if "account" in self.kwargs:
|
||||
self.account = get_object_or_404(_queryset, pk=self.kwargs["account"])
|
||||
else:
|
||||
self.account = _queryset.first()
|
||||
return {"account": self.account}
|
||||
|
||||
def get_form_kwargs(self):
|
||||
if "account" in self.kwargs:
|
||||
return super().get_form_kwargs() | {"disable_account": True}
|
||||
return super().get_form_kwargs()
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
if "account" in self.kwargs:
|
||||
return super().get_context_data(**kwargs) | {"account": self.account}
|
||||
return super().get_context_data(**kwargs)
|
||||
|
||||
|
||||
class StatementUpdateView(NummiUpdateView):
|
||||
model = Statement
|
||||
form_class = StatementForm
|
||||
template_name = "main/form/statement.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
data = super().get_context_data(**kwargs)
|
||||
statement = data["form"].instance
|
||||
|
||||
_transactions = statement.transaction_set.all()
|
||||
if _transactions:
|
||||
_categories = (
|
||||
_transactions.values("category", "category__name", "category__icon")
|
||||
.annotate(
|
||||
sum=models.Sum("value"),
|
||||
sum_m=models.Sum("value", filter=models.Q(value__lt=0)),
|
||||
sum_p=models.Sum("value", filter=models.Q(value__gt=0)),
|
||||
)
|
||||
.order_by("-sum")
|
||||
)
|
||||
data["categories"] = {
|
||||
"data": _categories,
|
||||
"max": max(
|
||||
_categories.aggregate(
|
||||
max=models.Max("sum_p", default=0),
|
||||
min=models.Min("sum_m", default=0),
|
||||
).values(),
|
||||
),
|
||||
}
|
||||
|
||||
return data | {
|
||||
"account": statement.account,
|
||||
"new_transaction_url": reverse_lazy(
|
||||
"new_transaction", kwargs={"statement": statement.pk}
|
||||
),
|
||||
"transactions": _transactions,
|
||||
}
|
||||
|
||||
|
||||
class StatementDeleteView(NummiDeleteView):
|
||||
model = Statement
|
||||
|
||||
|
||||
class StatementListView(NummiListView):
|
||||
model = Statement
|
||||
template_name = "main/list/statement.html"
|
||||
context_object_name = "statements"
|
||||
|
||||
|
||||
class StatementMixin:
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().filter(statement=self.kwargs.get("pk"))
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
return super().get_context_data(**kwargs) | {
|
||||
"object": Statement.objects.get(pk=self.kwargs.get("pk")),
|
||||
"statement": True,
|
||||
}
|
||||
|
||||
|
||||
class StatementTListView(StatementMixin, TransactionListView):
|
||||
pass
|
Loading…
Add table
Add a link
Reference in a new issue