Compare commits

...

2 commits

9 changed files with 101 additions and 102 deletions

View file

@ -18,6 +18,8 @@ class Category(models.Model):
class CategoryForm(ModelForm):
template_name = "main/form.html"
class Meta:
model = Category
fields = ["name", "icon"]
@ -47,6 +49,8 @@ class Transaction(models.Model):
class TransactionForm(ModelForm):
template_name = "main/form.html"
class Meta:
model = Transaction
fields = [
@ -78,6 +82,8 @@ class Invoice(models.Model):
class InvoiceForm(ModelForm):
template_name = "main/form.html"
class Meta:
model = Invoice
fields = ["name", "file"]
@ -178,6 +184,8 @@ class Snapshot(models.Model):
class SnapshotForm(ModelForm):
template_name = "main/form.html"
class Meta:
model = Snapshot
fields = ["date", "value"]

View file

@ -6,6 +6,14 @@ form {
line-height: 2rem;
}
form ul.errorlist {
grid-column: 1 / -1;
color: var(--red);
font-weight: 550;
list-style-type: "! ";
margin: 0;
}
form input,
form select,
form textarea {

View file

@ -11,13 +11,9 @@
{% block body %}
<h1><i class="fa fa-{{ category.icon }}"></i> {{ category }}</h1>
<form action="{% url 'update_category' category.id %}" method="post">
<form action="{% url 'category' category.id %}" method="post">
{% csrf_token %}
{% for field in form %}
{{ field.errors }}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
{% endfor %}
{{ form }}
<div class="buttons">
<a href="{% url 'del_category' category.id %}"><input type="button" value="Delete" class="btn del" /></a>
<input type="reset" />

View file

@ -0,0 +1,5 @@
{% for field in form %}
{{ field.errors }}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
{% endfor %}

View file

@ -29,7 +29,7 @@
{% for snap in snapshots %}
<div class="snapshot {% cycle 'w' 'g' %}">
<span class="date num center">
<a href="{% url 'snapshot' snap.date %}">{{ snap.date|date:"Y-m-d" }}</a>
<a href="{% url 'snapshot' snap.id %}">{{ snap.date|date:"Y-m-d" }}</a>
</span>
<span class="value num right">{% value snap.value %}</span>
<span class="diff num right">

View file

@ -21,13 +21,9 @@
{{ snapshot }}
</h1>
<form action="{% url 'update_snapshot' snapshot.id %}" method="post">
<form action="{% url 'snapshot' snapshot.id %}" method="post">
{% csrf_token %}
{% for field in form %}
{{ field.errors }}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
{% endfor %}
{{ form }}
<div class="buttons">
<a href="{% url 'del_snapshot' snapshot.date %}"><input type="button" value="Delete" class="del" /></a>
<input type="reset" />

View file

@ -10,13 +10,10 @@
<h1>{{ transaction }}</h1>
{% spaceless %}
<form id="transaction" action="{% url 'update_transaction' transaction.id %}" method="post">
<form id="transaction" action="{% url 'transaction' transaction.id %}" method="post">
{% csrf_token %}
{% for field in form %}
{{ field.errors }}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
{% endfor %}
<input type="hidden" name="form" value="transaction" />
{{ form }}
<div class="buttons">
<a href="{% url 'del_transaction' transaction.id %}"><input type="button" value="Delete" class="btn del" /></a>
<input class="btn" type="reset" />
@ -39,13 +36,10 @@
</div>
<h3>Add Invoice</h3>
<form action="{% url 'add_invoice' transaction.id %}" method="post" enctype="multipart/form-data">
<form action="{% url 'transaction' transaction.id %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% for field in invoice_form %}
{{ field.errors }}
<label for="{{ field.id_for_label }}">{{field.label }}</label>
{{ field }}
{% endfor %}
<input type="hidden" name="form" value="invoice" />
{{ invoice_form }}
<div class="buttons"><input class="btn" type="submit" value="Add Invoice" /></div>
</form>
{% endblock %}

View file

@ -9,23 +9,13 @@ urlpatterns = [
path("transactions", views.TransactionListView.as_view(), name="transactions"),
path("transaction", views.transaction, name="transaction"),
path("transaction/<uuid>", views.transaction, name="transaction"),
path(
"transaction/<uuid>/update", views.update_transaction, name="update_transaction"
),
path("transaction/<uuid>/del", views.del_transaction, name="del_transaction"),
path("transaction/<uuid>/add_invoice", views.add_invoice, name="add_invoice"),
path(
"transaction/<uuid>/del_invoice/<invoice_id>",
views.del_invoice,
name="del_invoice",
),
path("del_invoice/<uuid>/<invoice_id>", views.del_invoice, name="del_invoice"),
path("invoice/<uuid>", views.invoice, name="invoice"),
path("category", views.category, name="category"),
path("category/<uuid>", views.category, name="category"),
path("category/<uuid>/update", views.update_category, name="update_category"),
path("category/<uuid>/del", views.del_category, name="del_category"),
path("snapshot", views.snapshot, name="snapshot"),
path("snapshot/<date>", views.snapshot, name="snapshot"),
path("snapshot/update/<uuid>", views.update_snapshot, name="update_snapshot"),
path("snapshot/<date>/del", views.del_snapshot, name="del_snapshot"),
path("snapshot/<uuid>", views.snapshot, name="snapshot"),
path("snapshot/<uuid>/del", views.del_snapshot, name="del_snapshot"),
]

View file

@ -50,32 +50,41 @@ class TransactionListView(LoginRequiredMixin, ListView):
@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)
_form = None
_inv_form = None
_invoices = []
if request.method == "GET":
if uuid is None:
_transaction = Transaction()
else:
_transaction = get_object_or_404(Transaction, id=uuid)
elif request.method == "POST":
if request.POST["form"] == "transaction":
_transaction, _ = Transaction.objects.get_or_create(id=uuid)
_form = TransactionForm(request.POST, instance=_transaction)
if _form.is_valid():
_form.save()
_inv_form = InvoiceForm(instance=Invoice(transaction=_transaction))
elif request.POST["form"] == "invoice":
_transaction = get_object_or_404(Transaction, id=uuid)
_invoice = Invoice(transaction=_transaction)
_inv_form = InvoiceForm(request.POST, request.FILES, instance=_invoice)
if _inv_form.is_valid():
_inv_form.save()
return render(
request,
"main/transaction.html",
{
"transaction": _transaction,
"form": TransactionForm(instance=_transaction),
"invoices": _invoices,
"invoice_form": InvoiceForm(instance=Invoice(transaction=_transaction)),
"form": _form or TransactionForm(instance=_transaction),
"invoices": _invoices or Invoice.objects.filter(transaction=_transaction),
"invoice_form": _inv_form
or InvoiceForm(instance=Invoice(transaction=_transaction)),
},
)
@login_required
def update_transaction(request, uuid):
_transaction, _ = Transaction.objects.get_or_create(id=uuid)
_form = TransactionForm(request.POST, instance=_transaction)
_form.save()
return redirect(transaction, uuid=uuid)
@login_required
def del_transaction(request, uuid):
_transaction = get_object_or_404(Transaction, id=uuid)
@ -90,15 +99,6 @@ def invoice(request, uuid):
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)
@ -108,31 +108,29 @@ def del_invoice(request, uuid, invoice_id):
@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)
if request.method == "GET":
if uuid is None:
_category = Category()
else:
_category = get_object_or_404(Category, id=uuid)
_form = CategoryForm(instance=_category)
elif request.method == "POST":
_category, _ = Category.objects.get_or_create(id=uuid)
_form = CategoryForm(request.POST, instance=_category)
if _form.is_valid():
_form.save()
return render(
request,
"main/category.html",
{
"category": _category,
"form": CategoryForm(instance=_category),
"transactions": _transactions,
"form": _form,
"transactions": Transaction.objects.filter(category=_category),
},
)
@login_required
def update_category(request, uuid):
_category, _ = Category.objects.get_or_create(id=uuid)
_form = CategoryForm(request.POST, instance=_category)
_form.save()
return redirect(category, uuid=uuid)
@login_required
def del_category(request, uuid):
_category = get_object_or_404(Category, id=uuid)
@ -141,34 +139,38 @@ def del_category(request, uuid):
@login_required
def snapshot(request, date=None):
if date is None:
_snapshot = Snapshot()
else:
_snapshot = get_object_or_404(Snapshot, date=date)
def snapshot(request, uuid=None):
if request.method == "GET":
if uuid is None:
_snapshot = Snapshot()
else:
_snapshot = get_object_or_404(Snapshot, id=uuid)
context = {
"snapshot": _snapshot,
"form": SnapshotForm(instance=_snapshot),
}
elif request.method == "POST":
try:
_snapshot = Snapshot.objects.get(id=uuid)
except Snapshot.DoesNotExist:
_snapshot = Snapshot(id=uuid)
_form = SnapshotForm(request.POST, instance=_snapshot)
if _form.is_valid():
_form.save()
context = {
"snapshot": _snapshot,
"form": _form,
}
return render(
request,
"main/snapshot.html",
{
"snapshot": _snapshot,
"form": SnapshotForm(instance=_snapshot),
},
context,
)
@login_required
def update_snapshot(request, uuid):
try:
_snapshot = Snapshot.objects.get(id=uuid)
except Snapshot.DoesNotExist:
_snapshot = Snapshot(id=uuid)
_form = SnapshotForm(request.POST, instance=_snapshot)
_form.save()
return redirect(snapshot, date=_snapshot.date)
@login_required
def del_snapshot(request, date):
_snapshot = get_object_or_404(Snapshot, date=date)
def del_snapshot(request, uuid):
_snapshot = get_object_or_404(Snapshot, id=uuid)
_snapshot.delete()
return redirect(index)