118 lines
3.1 KiB
Python
118 lines
3.1 KiB
Python
from django.shortcuts import render, get_object_or_404, redirect
|
|
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,
|
|
CategoryForm,
|
|
)
|
|
|
|
|
|
@login_required
|
|
def index(request):
|
|
_transactions = Transaction.objects.order_by("-date")[:5]
|
|
_categories = Category.objects.all()
|
|
|
|
context = {
|
|
"transactions": _transactions,
|
|
"categories": _categories,
|
|
}
|
|
return render(request, "main/index.html", context)
|
|
|
|
|
|
class LoginView(auth_views.LoginView):
|
|
template_name = "main/login.html"
|
|
next_page = "index"
|
|
|
|
|
|
class LogoutView(auth_views.LogoutView):
|
|
next_page = "login"
|
|
|
|
|
|
@login_required
|
|
def transaction(request, uuid=None):
|
|
if uuid is None:
|
|
_transaction = Transaction()
|
|
_invoices = []
|
|
else:
|
|
_transaction = get_object_or_404(Transaction, id=uuid)
|
|
_invoices = Invoice.objects.filter(transaction=_transaction)
|
|
return render(
|
|
request,
|
|
"main/transaction.html",
|
|
{
|
|
"transaction": _transaction,
|
|
"form": TransactionForm(instance=_transaction),
|
|
"invoices": _invoices,
|
|
"invoice_form": InvoiceForm(instance=Invoice(transaction=_transaction)),
|
|
},
|
|
)
|
|
|
|
|
|
@login_required
|
|
def update_transaction(request, uuid):
|
|
try:
|
|
_transaction = Transaction.objects.get(id=uuid)
|
|
except Transaction.DoesNotExist:
|
|
_transaction = Transaction(id=uuid)
|
|
_form = TransactionForm(request.POST, instance=_transaction)
|
|
_form.save()
|
|
return redirect(transaction, uuid=uuid)
|
|
|
|
|
|
@login_required
|
|
def invoice(request, uuid):
|
|
_invoice = get_object_or_404(Invoice, id=uuid)
|
|
with _invoice.file.open() as _file:
|
|
return HttpResponse(_file.read(), content_type="application/pdf")
|
|
|
|
|
|
@login_required
|
|
def add_invoice(request, uuid):
|
|
_transaction = get_object_or_404(Transaction, id=uuid)
|
|
_invoice = Invoice(transaction=_transaction)
|
|
_form = InvoiceForm(request.POST, request.FILES, instance=_invoice)
|
|
_form.save()
|
|
return redirect(transaction, uuid=uuid)
|
|
|
|
|
|
@login_required
|
|
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)
|