Add login

This commit is contained in:
Edgar P. Burkhart 2022-05-20 15:06:37 +02:00
parent e38b781ff9
commit 1f48c41005
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
5 changed files with 34 additions and 0 deletions

View file

@ -3,6 +3,7 @@
{% block body %}
<h1>Nummi</h1>
<a href="{% url 'logout' %}">Logout</a>
<a href="{% url 'transaction' %}">Add transaction</a>
{% if transactions %}

View file

@ -0,0 +1,14 @@
{% extends "main/base.html" %}
{% block body %}
<h1>Login</h1>
<form action="{% url 'login' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<input hidden value="{{ next }}" name="next" />
<input type="submit" />
</form>
{% endblock %}

View file

@ -4,6 +4,8 @@ from . import views
urlpatterns = [
path("", views.index, name="index"),
path("login", views.LoginView.as_view(), name="login"),
path("logout", views.LogoutView.as_view(), name="logout"),
path("transaction", views.transaction, name="transaction"),
path("transaction/<uuid>", views.transaction, name="transaction"),
path(

View file

@ -1,9 +1,12 @@
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
@login_required
def index(request):
_transactions = Transaction.objects.order_by("-date")[:5]
_categories = Category.objects.order_by("name")
@ -15,6 +18,15 @@ def index(request):
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()
@ -34,6 +46,7 @@ def transaction(request, uuid=None):
)
@login_required
def update_transaction(request, uuid):
try:
_transaction = Transaction.objects.get(id=uuid)
@ -44,12 +57,14 @@ def update_transaction(request, uuid):
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)
@ -58,6 +73,7 @@ def add_invoice(request, uuid):
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()

View file

@ -119,6 +119,7 @@ USE_TZ = True
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = "static/"
LOGIN_URL = "login"
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field