From 5b98daba9a9931ccbd3dc85015f6659561198bbd Mon Sep 17 00:00:00 2001 From: "Andreas (@xAndy) Zimmermann" Date: Tue, 12 Apr 2022 16:09:17 +0200 Subject: [PATCH] add token login --- shiftregister/app/models.py | 7 +++++-- shiftregister/app/templates/base.html | 3 +++ shiftregister/app/urls.py | 1 + shiftregister/app/views.py | 14 ++++++++++++-- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/shiftregister/app/models.py b/shiftregister/app/models.py index 7f65ba5..e67ffa5 100644 --- a/shiftregister/app/models.py +++ b/shiftregister/app/models.py @@ -1,5 +1,6 @@ from django.db import models -import secrets +import secrets +from django.shortcuts import reverse class Room(models.Model): name = models.CharField(max_length=200, primary_key=True) @@ -43,4 +44,6 @@ def gen_token(): class LoginToken(models.Model): id = models.CharField(max_length=20, primary_key=True, default=gen_token, editable=False) - helper = models.ForeignKey(Helper, on_delete=models.CASCADE) \ No newline at end of file + helper = models.ForeignKey(Helper, on_delete=models.CASCADE) + def get_absolute_url(self): + return reverse('token_login', kwargs={'token':self.id}) \ No newline at end of file diff --git a/shiftregister/app/templates/base.html b/shiftregister/app/templates/base.html index d9aeee9..9951d2f 100644 --- a/shiftregister/app/templates/base.html +++ b/shiftregister/app/templates/base.html @@ -5,6 +5,9 @@ + {% if helper%} + Hallo {{helper.name}} + {%endif%}
{% block content %}{% endblock %}
diff --git a/shiftregister/app/urls.py b/shiftregister/app/urls.py index 88a9cac..ada4a1a 100644 --- a/shiftregister/app/urls.py +++ b/shiftregister/app/urls.py @@ -4,4 +4,5 @@ from . import views urlpatterns = [ path('', views.index, name='index'), + path('login/', views.login, name='token_login'), ] diff --git a/shiftregister/app/views.py b/shiftregister/app/views.py index 872bfe4..2412a6e 100644 --- a/shiftregister/app/views.py +++ b/shiftregister/app/views.py @@ -1,5 +1,5 @@ -from django.shortcuts import render -from .models import Shift +from django.shortcuts import render, redirect, get_object_or_404 +from .models import Shift, LoginToken, Helper import datetime from django.db.models import F, Count @@ -9,4 +9,14 @@ def index(request): free_shifts = Shift.objects.annotate(reg_count=Count('shiftregistration')).filter(start_at__gt=datetime.datetime.now(), room__required_helpers__gt=F('reg_count')).order_by('start_at') context = {'free_shifts': free_shifts} + if request.session.get('token'): + context['helper'] = LoginToken.objects.get(pk=request.session['token']).helper return render(request, 'shiftlist.html', context) + +def login(request, token): + tk = get_object_or_404(LoginToken,pk=token) + if not tk.helper.number_validated: + tk.helper.number_validated=True + tk.helper.save() + request.session['token'] = token + return redirect('index') \ No newline at end of file