shiftregister/shiftregister/app/views.py

26 lines
1.0 KiB
Python

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
def index(request):
# 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(),
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')
def logout(request):
del request.session['token']
return redirect('index')