110 lines
5.1 KiB
Python
110 lines
5.1 KiB
Python
from django.shortcuts import render, redirect, get_object_or_404
|
|
from .models import Shift, LoginToken, Helper, ShiftRegistration
|
|
import datetime
|
|
from django.db.models import F, Count, Q, ExpressionWrapper
|
|
from .forms import RegisterForm, EmptyForm
|
|
from django.db.models.fields import DateTimeField
|
|
from pprint import pprint
|
|
|
|
def index(request):
|
|
if request.session.get('last_seen_shift'):
|
|
del request.session['last_seen_shift']
|
|
# dont show shifts starting in <60 minutes?
|
|
# currently only sorts by date
|
|
context = {}
|
|
helper = None
|
|
if request.session.get('token'):
|
|
helper = LoginToken.objects.get(pk=request.session['token']).helper
|
|
context['helper'] = helper
|
|
context['my_shifts'] = helper.shiftregistration_set.filter(shift__start_at__gt=datetime.datetime.now()).order_by('shift__start_at')
|
|
context['current_shift'] = next(iter(helper.shiftregistration_set.annotate(shift_end=ExpressionWrapper(F('shift__start_at')+F('shift__duration'), output_field=DateTimeField())).filter(shift__start_at__lte=datetime.datetime.now(), shift_end__gte=datetime.datetime.now()).order_by('shift__start_at')[:1]), None)
|
|
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')
|
|
if helper:
|
|
free_shifts = Shift.objects.annotate(reg_count=Count('shiftregistration')).filter(start_at__gt=datetime.datetime.now(),
|
|
room__required_helpers__gt=F('reg_count')).filter( ~Q(shiftregistration__helper=helper)).order_by('start_at')
|
|
context['free_shifts']= free_shifts
|
|
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
|
|
# 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):
|
|
if request.session.get('token'):
|
|
del request.session['token']
|
|
return redirect('index')
|
|
|
|
def register(request):
|
|
# handle number already being registered(resend message?)
|
|
if request.session.get('token'):
|
|
return redirect('index')
|
|
context = {}
|
|
if request.method == 'POST':
|
|
form = RegisterForm(request.POST)
|
|
if not form.is_valid():
|
|
context['form']=form
|
|
return render(request, 'register.html', context)
|
|
helper = Helper(name = form.cleaned_data['name'], phone=form.cleaned_data['phone'])
|
|
helper.save()
|
|
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)
|
|
|
|
def shift(request, shiftid):
|
|
shift = get_object_or_404(Shift, pk=shiftid)
|
|
helper = None
|
|
context = { 'can_register':False,
|
|
'is_registered':False,
|
|
'can_cancel': 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
|
|
reg = ShiftRegistration.objects.filter(shift=shift, helper=helper)
|
|
if reg:
|
|
context['is_registered']=True
|
|
context['can_register']=False
|
|
if reg[0].can_cancel():
|
|
context['can_cancel'] = True
|
|
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)
|
|
|
|
def cancel(request, shiftid):
|
|
if request.method!='POST':
|
|
return redirect('shift', shiftid=shiftid)
|
|
if not EmptyForm(request.POST).is_valid():
|
|
return redirect('shift', shiftid=shiftid)
|
|
if not request.session.get('token'):
|
|
return redirect('shift', shiftid=shiftid)
|
|
helper = LoginToken.objects.get(pk=request.session['token']).helper
|
|
shift = get_object_or_404(Shift, pk=shiftid)
|
|
reg = get_object_or_404(ShiftRegistration, helper=helper, shift=shift)
|
|
if reg.can_cancel():
|
|
reg.delete()
|
|
return redirect('shift', shiftid=shiftid) |