nummi/nummi/statement/views.py

78 lines
2.3 KiB
Python
Raw Normal View History

2023-04-22 11:16:42 +02:00
from account.models import Account
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
2023-04-22 12:30:08 +02:00
pk_url_kwarg = "statement"
2023-04-22 11:16:42 +02:00
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
statement = data["form"].instance
_transactions = statement.transaction_set.all()
return data | {
"account": statement.account,
"new_transaction_url": reverse_lazy(
"new_transaction", kwargs={"statement": statement.pk}
),
"transactions": _transactions,
}
class StatementDeleteView(NummiDeleteView):
model = Statement
2023-04-22 12:30:08 +02:00
pk_url_kwarg = "statement"
2023-04-22 11:16:42 +02:00
class StatementListView(NummiListView):
model = Statement
context_object_name = "statements"
class StatementMixin:
def get_queryset(self):
2023-04-22 12:30:08 +02:00
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)
2023-04-22 11:16:42 +02:00
def get_context_data(self, **kwargs):
2023-04-22 12:30:08 +02:00
return super().get_context_data(**kwargs) | {"statement": self.statement}
2023-04-22 11:16:42 +02:00
class StatementTListView(StatementMixin, TransactionListView):
pass