nummi/nummi/transaction/views.py

175 lines
5 KiB
Python

from account.models import Account
from category.models import Category
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy
from django.views.generic.dates import MonthArchiveView, YearArchiveView
from history.utils import history
from main.views import (
NummiCreateView,
NummiDeleteView,
NummiDetailView,
NummiListView,
NummiUpdateView,
UserMixin,
)
from .forms import InvoiceForm, TransactionForm
from .models import Invoice, Transaction
class TransactionCreateView(NummiCreateView):
model = Transaction
form_class = TransactionForm
def get_initial(self):
initial = super().get_initial()
if "statement" in self.kwargs:
initial["statement"] = get_object_or_404(
self.request.user.statement_set, pk=self.kwargs["statement"]
)
return initial
def get_form_kwargs(self):
form_kwargs = super().get_form_kwargs()
if "statement" in self.kwargs:
form_kwargs["disable_statement"] = True
form_kwargs["autocomplete"] = True
return form_kwargs
class InvoiceCreateView(NummiCreateView):
model = Invoice
form_class = InvoiceForm
def form_valid(self, form):
form.instance.transaction = get_object_or_404(
Transaction.objects.filter(user=self.request.user),
pk=self.kwargs["transaction"],
)
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy("transaction", args=(self.object.transaction.pk,))
class TransactionUpdateView(NummiUpdateView):
model = Transaction
form_class = TransactionForm
pk_url_kwarg = "transaction"
class TransactionDetailView(NummiDetailView):
model = Transaction
pk_url_kwarg = "transaction"
context_object_name = "transaction"
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
transaction = data.get("transaction")
return data | {
"statement": transaction.statement,
"category": transaction.category,
}
class InvoiceUpdateView(NummiUpdateView):
model = Invoice
form_class = InvoiceForm
pk_url_kwarg = "invoice"
def get_success_url(self):
return reverse_lazy("transaction", args=(self.object.transaction.pk,))
def get_queryset(self):
return (
super()
.get_queryset()
.filter(
transaction=get_object_or_404(
Transaction, pk=self.kwargs["transaction"]
)
)
)
class TransactionDeleteView(NummiDeleteView):
model = Transaction
pk_url_kwarg = "transaction"
class InvoiceDeleteView(NummiDeleteView):
model = Invoice
pk_url_kwarg = "invoice"
def get_success_url(self):
return reverse_lazy("transaction", args=(self.object.transaction.pk,))
def get_queryset(self):
return (
super()
.get_queryset()
.filter(
transaction=get_object_or_404(
Transaction, pk=self.kwargs["transaction"]
)
)
)
class TransactionListView(NummiListView):
model = Transaction
context_object_name = "transactions"
paginate_by = 50
class TransactionACMixin:
model = Transaction
def get_queryset(self):
if "account" in self.kwargs:
self.account = get_object_or_404(
Account.objects.filter(user=self.request.user),
pk=self.kwargs["account"],
)
return super().get_queryset().filter(statement__account=self.account)
if "category" in self.kwargs:
self.category = get_object_or_404(
Category.objects.filter(user=self.request.user),
pk=self.kwargs["category"],
)
return super().get_queryset().filter(category=self.category)
return super().get_queryset()
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
if "category" in self.kwargs:
return context_data | {"category": self.category}
if "account" in self.kwargs:
return context_data | {"account": self.account}
return context_data
class TransactionMonthView(UserMixin, TransactionACMixin, MonthArchiveView):
model = Transaction
date_field = "date"
context_object_name = "transactions"
month_format = "%m"
class TransactionYearView(UserMixin, TransactionACMixin, YearArchiveView):
model = Transaction
date_field = "date"
context_object_name = "transactions"
make_object_list = True
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
h_data = context_data.get("transactions")
if not (context_data.get("account") or context_data.get("category")):
h_data = h_data.exclude(category__budget=False)
return context_data | {"history": history(h_data)}