101 lines
2.9 KiB
Python
101 lines
2.9 KiB
Python
from account.models import Account
|
|
from category.models import Category
|
|
from django.contrib import messages
|
|
from django.contrib.auth import views as auth_views
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.urls import reverse_lazy
|
|
from django.utils.html import format_html
|
|
from django.utils.translation import gettext as _
|
|
from django.views.generic import (
|
|
CreateView,
|
|
DeleteView,
|
|
ListView,
|
|
TemplateView,
|
|
UpdateView,
|
|
)
|
|
from history.utils import echarts, history
|
|
from statement.models import Statement
|
|
from transaction.models import Transaction
|
|
|
|
|
|
class IndexView(LoginRequiredMixin, TemplateView):
|
|
template_name = "main/index.html"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
_max = 8
|
|
_transactions = Transaction.objects.filter(user=self.request.user)
|
|
_statements = Statement.objects.filter(user=self.request.user)
|
|
_history = history(_transactions.exclude(category__budget=False))
|
|
|
|
res = {
|
|
"accounts": Account.objects.filter(user=self.request.user),
|
|
"transactions": _transactions[:_max],
|
|
"categories": Category.objects.filter(user=self.request.user),
|
|
"statements": _statements[:_max],
|
|
"history": _history,
|
|
"echarts_history": echarts(_transactions),
|
|
}
|
|
if _transactions.count() > _max:
|
|
res["transactions_url"] = reverse_lazy("transactions")
|
|
if _statements.count() > _max:
|
|
res["statements_url"] = reverse_lazy("statements")
|
|
|
|
return super().get_context_data(**kwargs) | res
|
|
|
|
|
|
class UserMixin(LoginRequiredMixin):
|
|
def get_queryset(self, **kwargs):
|
|
return super().get_queryset().filter(user=self.request.user)
|
|
|
|
def get_form_kwargs(self):
|
|
return super().get_form_kwargs() | {
|
|
"user": self.request.user,
|
|
}
|
|
|
|
|
|
class NummiCreateView(UserMixin, CreateView):
|
|
def form_valid(self, form):
|
|
form.instance.user = self.request.user
|
|
self.next = form.data.get("next")
|
|
return super().form_valid(form)
|
|
|
|
def get_success_url(self):
|
|
surl = super().get_success_url()
|
|
messages.success(
|
|
self.request,
|
|
format_html(
|
|
"<a href='{surl}'>{name}</a> {msg}",
|
|
surl=surl,
|
|
name=self.object,
|
|
msg=_("was created successfully"),
|
|
),
|
|
)
|
|
|
|
return self.next or surl
|
|
|
|
|
|
class NummiUpdateView(UserMixin, UpdateView):
|
|
pass
|
|
|
|
|
|
class NummiDeleteView(UserMixin, DeleteView):
|
|
template_name = "main/confirm_delete.html"
|
|
success_url = reverse_lazy("index")
|
|
|
|
def get_form_kwargs(self):
|
|
_res = super().get_form_kwargs()
|
|
_res.pop("user")
|
|
return _res
|
|
|
|
|
|
class LoginView(auth_views.LoginView):
|
|
template_name = "main/login.html"
|
|
next_page = "index"
|
|
|
|
|
|
class LogoutView(auth_views.LogoutView):
|
|
next_page = "login"
|
|
|
|
|
|
class NummiListView(UserMixin, ListView):
|
|
paginate_by = 96
|