|
|
|
@ -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)
|
|
|
|
|
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)
|