80 lines
2.5 KiB
Python
80 lines
2.5 KiB
Python
from account.models import Account
|
|
from category.utils import get_categories
|
|
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
|
|
|
|
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
|
|
pk_url_kwarg = "statement"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
data = super().get_context_data(**kwargs)
|
|
statement = data["form"].instance
|
|
|
|
_transactions = statement.transaction_set.all()
|
|
if _transactions:
|
|
data["categories"] = get_categories(_transactions)
|
|
|
|
return data | {
|
|
"account": statement.account,
|
|
"new_transaction_url": reverse_lazy(
|
|
"new_transaction", kwargs={"statement": statement.pk}
|
|
),
|
|
"transactions": _transactions,
|
|
}
|
|
|
|
|
|
class StatementDeleteView(NummiDeleteView):
|
|
model = Statement
|
|
pk_url_kwarg = "statement"
|
|
|
|
|
|
class StatementListView(NummiListView):
|
|
model = Statement
|
|
context_object_name = "statements"
|
|
|
|
|
|
class StatementMixin:
|
|
def get_queryset(self):
|
|
self.statement = get_object_or_404(
|
|
Statement.objects.filter(user=self.request.user),
|
|
pk=self.kwargs.get("statement"),
|
|
)
|
|
return super().get_queryset().filter(statement=self.statement)
|
|
|
|
def get_context_data(self, **kwargs):
|
|
return super().get_context_data(**kwargs) | {"statement": self.statement}
|
|
|
|
|
|
class StatementTListView(StatementMixin, TransactionListView):
|
|
pass
|