145 lines
4.3 KiB
Python
145 lines
4.3 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
|
||
|
from main.views import (
|
||
|
NummiCreateView,
|
||
|
NummiDeleteView,
|
||
|
NummiListView,
|
||
|
NummiUpdateView,
|
||
|
UserMixin,
|
||
|
)
|
||
|
from statement.models import Statement
|
||
|
|
||
|
from .forms import InvoiceForm, TransactionForm
|
||
|
from .models import Invoice, Transaction
|
||
|
|
||
|
|
||
|
class TransactionCreateView(NummiCreateView):
|
||
|
model = Transaction
|
||
|
form_class = TransactionForm
|
||
|
template_name = "main/form/transaction.html"
|
||
|
|
||
|
def get_initial(self):
|
||
|
_queryset = Statement.objects.filter(user=self.request.user)
|
||
|
if "statement" in self.kwargs:
|
||
|
self.statement = get_object_or_404(_queryset, pk=self.kwargs["statement"])
|
||
|
else:
|
||
|
self.statement = _queryset.first()
|
||
|
return {"statement": self.statement}
|
||
|
|
||
|
def get_form_kwargs(self):
|
||
|
if "statement" in self.kwargs:
|
||
|
return super().get_form_kwargs() | {"disable_statement": True}
|
||
|
return super().get_form_kwargs()
|
||
|
|
||
|
def get_context_data(self, **kwargs):
|
||
|
if "statement" in self.kwargs:
|
||
|
return super().get_context_data(**kwargs) | {"statement": self.statement}
|
||
|
return super().get_context_data(**kwargs)
|
||
|
|
||
|
|
||
|
class InvoiceCreateView(NummiCreateView):
|
||
|
model = Invoice
|
||
|
form_class = InvoiceForm
|
||
|
template_name = "main/form/invoice.html"
|
||
|
|
||
|
def form_valid(self, form):
|
||
|
form.instance.transaction = get_object_or_404(
|
||
|
Transaction.objects.filter(user=self.request.user),
|
||
|
pk=self.kwargs["transaction_pk"],
|
||
|
)
|
||
|
return super().form_valid(form)
|
||
|
|
||
|
def get_success_url(self):
|
||
|
return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk})
|
||
|
|
||
|
|
||
|
class TransactionUpdateView(NummiUpdateView):
|
||
|
model = Transaction
|
||
|
form_class = TransactionForm
|
||
|
template_name = "main/form/transaction.html"
|
||
|
|
||
|
|
||
|
class InvoiceUpdateView(NummiUpdateView):
|
||
|
model = Invoice
|
||
|
form_class = InvoiceForm
|
||
|
template_name = "main/form/invoice.html"
|
||
|
|
||
|
def get_success_url(self):
|
||
|
return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk})
|
||
|
|
||
|
def get_queryset(self):
|
||
|
return (
|
||
|
super()
|
||
|
.get_queryset()
|
||
|
.filter(
|
||
|
transaction=get_object_or_404(
|
||
|
Transaction, pk=self.kwargs["transaction_pk"]
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
|
||
|
|
||
|
class TransactionDeleteView(NummiDeleteView):
|
||
|
model = Transaction
|
||
|
|
||
|
|
||
|
class InvoiceDeleteView(NummiDeleteView):
|
||
|
model = Invoice
|
||
|
|
||
|
def get_success_url(self):
|
||
|
return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk})
|
||
|
|
||
|
def get_queryset(self):
|
||
|
return (
|
||
|
super()
|
||
|
.get_queryset()
|
||
|
.filter(
|
||
|
transaction=get_object_or_404(
|
||
|
Transaction, pk=self.kwargs["transaction_pk"]
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
|
||
|
|
||
|
class TransactionListView(NummiListView):
|
||
|
model = Transaction
|
||
|
template_name = "main/list/transaction.html"
|
||
|
context_object_name = "transactions"
|
||
|
|
||
|
|
||
|
class TransactionMonthView(UserMixin, MonthArchiveView):
|
||
|
template_name = "main/month/transaction.html"
|
||
|
model = Transaction
|
||
|
date_field = "date"
|
||
|
context_object_name = "transactions"
|
||
|
month_format = "%m"
|
||
|
|
||
|
account = None
|
||
|
category = None
|
||
|
|
||
|
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(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):
|
||
|
if "account" in self.kwargs:
|
||
|
return super().get_context_data(**kwargs) | {"account": self.account}
|
||
|
if "category" in self.kwargs:
|
||
|
return super().get_context_data(**kwargs) | {"category": self.category}
|
||
|
return super().get_context_data(**kwargs)
|