From 18b04a710280cb36e5a28b2791ddb02de316cf21 Mon Sep 17 00:00:00 2001 From: "Andreas (@xAndy) Zimmermann" Date: Tue, 12 Apr 2022 18:31:18 +0200 Subject: [PATCH] add initial shift signup flow --- shiftregister/app/forms.py | 6 +++- shiftregister/app/templates/shift.html | 18 +++++++++++ shiftregister/app/urls.py | 2 +- shiftregister/app/views.py | 42 +++++++++++++++++++++++--- 4 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 shiftregister/app/templates/shift.html diff --git a/shiftregister/app/forms.py b/shiftregister/app/forms.py index 28a3d61..41b3460 100644 --- a/shiftregister/app/forms.py +++ b/shiftregister/app/forms.py @@ -5,4 +5,8 @@ class RegisterForm(forms.Form): name = forms.CharField(max_length=Helper.name.field.max_length, label="Name") # actually verify phone number, lol phone = forms.CharField(max_length=Helper.phone.field.max_length, label='Handynummer fuer Benarchrichtigungen') - okf = forms.BooleanField(label='Ich moechte SMS erhalten') \ No newline at end of file + okf = forms.BooleanField(label='Ich moechte SMS erhalten') + +# placeholder form for simple submit button use cases so we get csrf protection +class EmptyForm(forms.Form): + pass \ No newline at end of file diff --git a/shiftregister/app/templates/shift.html b/shiftregister/app/templates/shift.html new file mode 100644 index 0000000..96653c2 --- /dev/null +++ b/shiftregister/app/templates/shift.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} + +{% block title %}Schichtansicht{% endblock %} +{% block content %} +{%if is_registered%} +
Du bist zu dieser Schicht angemeldet
+{%endif%} +Ort: {{shift.room.name}}
+Start: {{shift.start_at}}
+Dauer: {{shift.duration}}
+{%if can_register%} +
+ {% csrf_token %} +{{shift_form.as_p}} + +
+{%endif%} +{% endblock %} \ No newline at end of file diff --git a/shiftregister/app/urls.py b/shiftregister/app/urls.py index 3e9ce4a..c323b55 100644 --- a/shiftregister/app/urls.py +++ b/shiftregister/app/urls.py @@ -7,5 +7,5 @@ urlpatterns = [ path('login/', views.login, name='token_login'), path('logout', views.logout, name='token_logout'), path('register', views.register, name='register'), - + path('shift/', views.shift, name='shift'), ] diff --git a/shiftregister/app/views.py b/shiftregister/app/views.py index 07cda71..6c1747e 100644 --- a/shiftregister/app/views.py +++ b/shiftregister/app/views.py @@ -1,10 +1,11 @@ from django.shortcuts import render, redirect, get_object_or_404 -from .models import Shift, LoginToken, Helper +from .models import Shift, LoginToken, Helper, ShiftRegistration import datetime from django.db.models import F, Count -from .forms import RegisterForm +from .forms import RegisterForm, EmptyForm def index(request): + del request.session['last_seen_shift'] # dont show shifts starting in <60 minutes? # currently only sorts by date free_shifts = Shift.objects.annotate(reg_count=Count('shiftregistration')).filter(start_at__gt=datetime.datetime.now(), @@ -20,6 +21,10 @@ def login(request, token): tk.helper.number_validated=True tk.helper.save() request.session['token'] = token + # if the user was viewing a single shift before registering, they probably want to register for that + # shift so we redirect them there. + if request.session.get('last_seen_shift'): + return redirect('shift', shiftid=request.session['last_seen_shift']) return redirect('index') def logout(request): @@ -41,6 +46,35 @@ def register(request): token = helper.send_confirmation() request.session['token'] = token.pk return render(request, 'wait_confirmation.html', {'helper':helper}) - context['form']=RegisterForm() - return render(request, 'register.html', context) \ No newline at end of file + return render(request, 'register.html', context) + +def shift(request, shiftid): + shift = get_object_or_404(Shift, pk=shiftid) + helper = None + context = { 'can_register':False, + 'is_registered':False, + 'shift':shift, + 'shift_form': EmptyForm, + } + # this currently ignores date/time + request.session['last_seen_shift'] = shiftid + if shift.room.required_helpers>shift.shiftregistration_set.count(): + context['can_register'] = True + if request.session.get('token'): + helper = LoginToken.objects.get(pk=request.session['token']).helper + context['helper'] =helper + if ShiftRegistration.objects.filter(shift=shift, helper=helper).count()!=0: + context['is_registered']=True + context['can_register']=False + if request.method=='POST': + if EmptyForm(request.POST).is_valid(): + if not helper: + # todo: add flash message that signup is required? + return redirect('register') + if context['can_register']: + s = ShiftRegistration(helper=helper, shift=shift) + s.save() + # redirect so page can be reloaded without resending post data + return redirect('shift', shiftid=shift.pk) + return render(request, 'shift.html', context) \ No newline at end of file