nummi/nummi/main/views.py
Edgar P. Burkhart 7c5e804aba
Update templates with new snapshot and transaction tables
Add link on homepage to see all transactions and snapshots
2022-12-31 11:07:11 +01:00

240 lines
6.3 KiB
Python

from django.contrib.auth import views as auth_views
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.postgres.search import (
SearchQuery,
SearchRank,
SearchVector,
TrigramSimilarity,
)
from django.db import models
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse_lazy
from django.views.generic import (
CreateView,
DeleteView,
ListView,
TemplateView,
UpdateView,
)
from django.views.generic.edit import ProcessFormView
from .forms import AccountForm, CategoryForm, InvoiceForm, SnapshotForm, TransactionForm
from .models import Account, Category, Invoice, Snapshot, Transaction
class IndexView(LoginRequiredMixin, TemplateView):
template_name = "main/index.html"
def get_context_data(self, **kwargs):
_max = 10
res = super().get_context_data(**kwargs) | {
"accounts": Account.objects.filter(user=self.request.user),
"transactions": Transaction.objects.filter(user=self.request.user)[:10],
"categories": Category.objects.filter(user=self.request.user),
"snapshots": Snapshot.objects.filter(user=self.request.user)[:10],
}
if res["transactions"].count() == 10:
res["transactions_url"] = reverse_lazy("transactions")
if res["snapshots"].count() == 10:
res["snapshots_url"] = reverse_lazy("snapshots")
return res
class UserMixin(LoginRequiredMixin):
def get_queryset(self, **kwargs):
return super().get_queryset().filter(user=self.request.user)
class UserFormMixin:
def get_form_kwargs(self):
return super().get_form_kwargs() | {
"user": self.request.user,
"initial": self.request.GET,
}
class NummiCreateView(UserMixin, UserFormMixin, CreateView):
def form_valid(self, form):
form.instance.user = self.request.user
return super().form_valid(form)
class NummiUpdateView(UserMixin, UserFormMixin, UpdateView):
pass
class NummiDeleteView(UserMixin, DeleteView):
template_name = "main/confirm_delete.html"
success_url = reverse_lazy("index")
class LoginView(auth_views.LoginView):
template_name = "main/login.html"
next_page = "index"
class LogoutView(auth_views.LogoutView):
next_page = "login"
class AccountCreateView(NummiCreateView):
model = Account
form_class = AccountForm
class TransactionCreateView(NummiCreateView):
model = Transaction
form_class = TransactionForm
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_pk"],
)
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk})
class CategoryCreateView(NummiCreateView):
model = Category
form_class = CategoryForm
class SnapshotCreateView(NummiCreateView):
model = Snapshot
form_class = SnapshotForm
class AccountUpdateView(NummiUpdateView):
model = Account
form_class = AccountForm
class TransactionUpdateView(NummiUpdateView):
model = Transaction
form_class = TransactionForm
class InvoiceUpdateView(NummiUpdateView):
model = Invoice
form_class = InvoiceForm
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 CategoryUpdateView(NummiUpdateView):
model = Category
form_class = CategoryForm
class SnapshotUpdateView(NummiUpdateView):
model = Snapshot
form_class = SnapshotForm
class AccountDeleteView(NummiDeleteView):
model = Account
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 CategoryDeleteView(NummiDeleteView):
model = Category
class SnapshotDeleteView(NummiDeleteView):
model = Snapshot
class NummiListView(UserMixin, ListView):
paginate_by = 24
class TransactionListView(NummiListView):
model = Transaction
template_name = "main/list/transaction.html"
context_object_name = "transactions"
class SnapshotListView(NummiListView):
model = Snapshot
template_name = "main/list/snapshot.html"
context_object_name = "snapshots"
class AccountMixin:
def get_queryset(self):
return super().get_queryset().filter(account=self.kwargs.get("pk"))
class AccountTListView(AccountMixin, TransactionListView):
pass
class AccountSListView(AccountMixin, SnapshotListView):
pass
class SnapshotTListView(TransactionListView):
def get_queryset(self):
return super().get_queryset().filter(snapshot=self.kwargs.get("pk"))
class SearchView(TransactionListView):
def post(self, *args, **kwargs):
return redirect("search", search=self.request.POST.get("search"))
def get_queryset(self):
self.search = self.kwargs["search"]
return (
super()
.get_queryset()
.annotate(
rank=SearchRank(
SearchVector("name", weight="A")
+ SearchVector("description", weight="B")
+ SearchVector("trader", weight="B"),
SearchQuery(self.search, search_type="websearch"),
),
similarity=TrigramSimilarity("name", self.search),
)
.filter(models.Q(rank__gte=0.1) | models.Q(similarity__gte=0.3))
.order_by("-rank", "-date")
)
def get_context_data(self, **kwargs):
return super().get_context_data(**kwargs) | {"search": self.kwargs["search"]}