Update views using class based views

Updated views for transaction
Invoice form temporarily removed
This commit is contained in:
Edgar P. Burkhart 2022-12-28 09:54:39 +01:00
parent 41499f1329
commit 757ce1f45f
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
7 changed files with 57 additions and 81 deletions

View file

@ -6,6 +6,7 @@ from django.core.files.storage import Storage
from django.core.validators import FileExtensionValidator
from django.db import models
from django.forms import ModelForm
from django.urls import reverse
from django.utils.translation import gettext as _
@ -27,7 +28,7 @@ class Category(models.Model):
class CategoryForm(ModelForm):
template_name = "main/form.html"
template_name = "main/form/base.html"
class Meta:
model = Category
@ -62,6 +63,9 @@ class Transaction(models.Model):
def __str__(self):
return f"{self.date} {self.name}"
def get_absolute_url(self):
return reverse("transaction", kwargs={"pk": self.pk})
@property
def invoices(self):
return Invoice.objects.filter(transaction=self)
@ -77,7 +81,7 @@ class Transaction(models.Model):
class TransactionForm(ModelForm):
template_name = "main/form.html"
template_name = "main/form/base.html"
class Meta:
model = Transaction
@ -123,7 +127,7 @@ class Invoice(models.Model):
class InvoiceForm(ModelForm):
template_name = "main/form.html"
template_name = "main/form/base.html"
prefix = "invoice"
class Meta:
@ -263,7 +267,7 @@ class Snapshot(models.Model):
class SnapshotForm(ModelForm):
template_name = "main/form.html"
template_name = "main/form/base.html"
class Meta:
model = Snapshot

View file

@ -1,10 +0,0 @@
{% if form.non_field_errors %}
<ul class='errorlist'>
{% for error in form.non_field_errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% endif %}
{% for field in form %}
{{ field.errors }}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
<div>{{ field }}</div>
{% endfor %}

View file

@ -0,0 +1,14 @@
{% load i18n %}
{% block fields %}
{% csrf_token %}
{% if form.non_field_errors %}
<ul class='errorlist'>
{% for error in form.non_field_errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% endif %}
{% for field in form %}
{{ field.errors }}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
<div>{{ field }}</div>
{% endfor %}
{% endblock %}

View file

@ -1,64 +0,0 @@
{% extends "main/base.html" %}
{% load static %}
{% load main_extras %}
{% load i18n %}
{% block link %}
{{ block.super }}
<link rel="stylesheet"
href="{% static 'main/css/form.css' %}"
type="text/css"/>
<link rel="stylesheet"
href="{% static 'main/css/table.css' %}"
type="text/css"/>
{% endblock %}
{% block body %}
<h1>{{ transaction }}</h1>
{% spaceless %}
<form id="transaction"
action="{% url 'transaction' transaction.id %}"
method="post">
{% csrf_token %}
<input type="hidden" name="form" value="transaction" />
{{ form }}
{% form_buttons transaction %}
</form>
{% endspaceless %}
<div class="split">
{% if transaction.has_invoice %}
<div>
<h2>{% translate "Invoices" %}</h2>
<div id="invoices" class="table col1-1-1">
<div class="header">
<strong class="attach center"><i class="fa fa-file"></i></strong>
<strong class="name">{% translate "Name" %}</strong>
<strong class="attach right"><i class="fa fa-trash-can"></i></strong>
</div>
{% for inv in transaction.invoices %}
<div class="invoice">
<a href="{{ inv.file.url }}"><i class="fa-regular fa-file"></i></a>
<a href="{{ inv.file.url }}">{{ inv.name }}</a>
<a href="{% url 'del_invoice' transaction.id inv.id %}"
class="right"
onclick="return confirm('{% translate "Delete" %} <{{ inv.name }}> ?')">
{% translate "Delete" %}
</a>
</div>
{% endfor %}
</div>
</div>
{% endif %}
<div>
<h3>{% translate "Add invoice" %}</h3>
<form action="{% url 'transaction' transaction.id %}"
method="post"
enctype="multipart/form-data">
{% csrf_token %}
<input type="hidden" name="form" value="invoice" />
{{ invoice_form }}
<div class="buttons">
<input class="btn" type="submit" value="{% translate "Add" %}" />
</div>
</form>
</div>
</div>
{% endblock %}

View file

@ -0,0 +1,22 @@
{% extends "main/base.html" %}
{% load static %}
{% load main_extras %}
{% load i18n %}
{% block link %}
{{ block.super }}
<link rel="stylesheet"
href="{% static 'main/css/form.css' %}"
type="text/css"/>
<link rel="stylesheet"
href="{% static 'main/css/table.css' %}"
type="text/css"/>
{% endblock %}
{% block body %}
<h1>{{ transaction }}</h1>
{% spaceless %}
<form id="transaction" action="" method="post">
{{ form }}
{% form_buttons form.instance %}
</form>
{% endspaceless %}
{% endblock %}

View file

@ -7,8 +7,8 @@ urlpatterns = [
path("login", views.LoginView.as_view(), name="login"),
path("logout", views.LogoutView.as_view(), name="logout"),
path("transactions", views.TransactionListView.as_view(), name="transactions"),
path("transaction", views.transaction, name="transaction"),
path("transaction/<uuid>", views.transaction, name="transaction"),
path("transaction", views.TransactionCreateView.as_view(), name="transaction"),
path("transaction/<pk>", views.TransactionUpdateView.as_view(), name="transaction"),
path("transaction/<uuid>/del", views.del_transaction, name="del_transaction"),
path("del_invoice/<uuid>/<invoice_id>", views.del_invoice, name="del_invoice"),
path("invoice/<uuid>", views.invoice, name="invoice"),

View file

@ -11,7 +11,7 @@ from django.core.paginator import Paginator
from django.db import models
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.views.generic import ListView
from django.views.generic import CreateView, ListView, UpdateView
from .models import (
Category,
@ -92,6 +92,16 @@ def transaction(request, uuid=None):
)
class TransactionCreateView(LoginRequiredMixin, CreateView):
model = Transaction
form_class = TransactionForm
class TransactionUpdateView(LoginRequiredMixin, UpdateView):
model = Transaction
form_class = TransactionForm
@login_required
def del_transaction(request, uuid):
_transaction = get_object_or_404(Transaction, id=uuid)