|
|
|
@ -5,105 +5,139 @@ from .forms import RegisterForm, EmptyForm
|
|
|
|
|
from django.db.models.fields import DateTimeField
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def index(request):
|
|
|
|
|
if request.session.get('last_seen_shift'):
|
|
|
|
|
del request.session['last_seen_shift']
|
|
|
|
|
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=timezone.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=timezone.now(), shift_end__gte=timezone.now()).order_by('shift__start_at')[:1]), None)
|
|
|
|
|
free_shifts = Shift.objects.annotate(reg_count=Count('shiftregistration')).filter(start_at__gt=timezone.now(),
|
|
|
|
|
room__required_helpers__gt=F('reg_count')).order_by('start_at')
|
|
|
|
|
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=timezone.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=timezone.now(), shift_end__gte=timezone.now()
|
|
|
|
|
)
|
|
|
|
|
.order_by("shift__start_at")[:1]
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
free_shifts = (
|
|
|
|
|
Shift.objects.annotate(reg_count=Count("shiftregistration"))
|
|
|
|
|
.filter(start_at__gt=timezone.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=timezone.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)
|
|
|
|
|
free_shifts = (
|
|
|
|
|
Shift.objects.annotate(reg_count=Count("shiftregistration"))
|
|
|
|
|
.filter(
|
|
|
|
|
start_at__gt=timezone.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)
|
|
|
|
|
tk = get_object_or_404(LoginToken, pk=token)
|
|
|
|
|
if not tk.helper.number_validated:
|
|
|
|
|
tk.helper.number_validated=True
|
|
|
|
|
tk.helper.number_validated = True
|
|
|
|
|
tk.helper.save()
|
|
|
|
|
request.session['token'] = token
|
|
|
|
|
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')
|
|
|
|
|
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')
|
|
|
|
|
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')
|
|
|
|
|
if request.session.get("token"):
|
|
|
|
|
return redirect("index")
|
|
|
|
|
context = {}
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
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'])
|
|
|
|
|
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)
|
|
|
|
|
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,
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
context["is_registered"] = True
|
|
|
|
|
context["can_register"] = False
|
|
|
|
|
if reg[0].can_cancel():
|
|
|
|
|
context['can_cancel'] = True
|
|
|
|
|
if request.method=='POST':
|
|
|
|
|
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']:
|
|
|
|
|
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)
|
|
|
|
|
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 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
|
|
|
|
|
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)
|
|
|
|
|
return redirect("shift", shiftid=shiftid)
|
|
|
|
|