52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
from django.shortcuts import get_object_or_404
|
|
from django.urls import reverse_lazy
|
|
from main.views import NummiCreateView, NummiDeleteView, NummiUpdateView
|
|
from transaction.utils import history
|
|
from transaction.views import TransactionListView
|
|
|
|
from .forms import CategoryForm
|
|
from .models import Category
|
|
|
|
|
|
class CategoryCreateView(NummiCreateView):
|
|
model = Category
|
|
form_class = CategoryForm
|
|
|
|
|
|
class CategoryUpdateView(NummiUpdateView):
|
|
model = Category
|
|
form_class = CategoryForm
|
|
pk_url_kwarg = "category"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
data = super().get_context_data(**kwargs)
|
|
category = data["form"].instance
|
|
|
|
return data | {
|
|
"transactions": category.transaction_set.all()[:8],
|
|
"transactions_url": reverse_lazy(
|
|
"category_transactions", args=(category.pk,)
|
|
),
|
|
"history": history(category.transaction_set),
|
|
}
|
|
|
|
|
|
class CategoryDeleteView(NummiDeleteView):
|
|
model = Category
|
|
pk_url_kwarg = "category"
|
|
|
|
|
|
class CategoryMixin:
|
|
def get_queryset(self):
|
|
self.category = get_object_or_404(
|
|
Category.objects.filter(user=self.request.user),
|
|
pk=self.kwargs.get("category"),
|
|
)
|
|
return super().get_queryset().filter(category=self.category)
|
|
|
|
def get_context_data(self, **kwargs):
|
|
return super().get_context_data(**kwargs) | {"category": self.category}
|
|
|
|
|
|
class CategoryTListView(CategoryMixin, TransactionListView):
|
|
pass
|