Add category page
This commit is contained in:
parent
df49afb5a4
commit
467c8002e2
6 changed files with 84 additions and 22 deletions
|
|
@ -3,29 +3,24 @@ from django.http import HttpResponse
|
|||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth import views as auth_views
|
||||
|
||||
from .models import Transaction, TransactionForm, Invoice, InvoiceForm, Category
|
||||
from .models import (
|
||||
Transaction,
|
||||
TransactionForm,
|
||||
Invoice,
|
||||
InvoiceForm,
|
||||
Category,
|
||||
CategoryForm,
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def index(request):
|
||||
_transactions = Transaction.objects.order_by("-date")[:5]
|
||||
_categories = Category.objects.filter(parent=None)
|
||||
|
||||
def _cat_list(cat):
|
||||
children = []
|
||||
for child in Category.objects.filter(parent=cat):
|
||||
children += _cat_list(child)
|
||||
if len(children) == 0:
|
||||
return (cat.name,)
|
||||
return cat.name, children
|
||||
|
||||
_cats = []
|
||||
for cat in _categories:
|
||||
_cats += _cat_list(cat)
|
||||
_categories = Category.objects.all()
|
||||
|
||||
context = {
|
||||
"transactions": _transactions,
|
||||
"categories": _cats,
|
||||
"categories": _categories,
|
||||
}
|
||||
return render(request, "main/index.html", context)
|
||||
|
||||
|
|
@ -91,3 +86,33 @@ def del_invoice(request, uuid, invoice_id):
|
|||
_invoice = get_object_or_404(Invoice, id=invoice_id)
|
||||
_invoice.delete()
|
||||
return redirect(transaction, uuid=uuid)
|
||||
|
||||
|
||||
@login_required
|
||||
def category(request, uuid=None):
|
||||
if uuid is None:
|
||||
_category = Category()
|
||||
_transactions = None
|
||||
else:
|
||||
_category = get_object_or_404(Category, id=uuid)
|
||||
_transactions = Transaction.objects.filter(category=_category)
|
||||
return render(
|
||||
request,
|
||||
"main/category.html",
|
||||
{
|
||||
"category": _category,
|
||||
"form": CategoryForm(instance=_category),
|
||||
"transactions": _transactions,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def update_category(request, uuid):
|
||||
try:
|
||||
_category = Category.objects.get(id=uuid)
|
||||
except Category.DoesNotExist:
|
||||
_category = Category(id=uuid)
|
||||
_form = CategoryForm(request.POST, instance=_category)
|
||||
_form.save()
|
||||
return redirect(category, uuid=uuid)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue