move token->helper into middleware to simplify code and error paths

pull/1/head
parent f4a0ee55d4
commit 5077333ff1

@ -5,8 +5,6 @@ from .models import LoginToken
def proc(request):
context = {
"DEBUG": settings.DEBUG,
"helper": request.helper,
}
tk = LoginToken.objects.filter(pk=request.session.get("token")).first()
if tk:
context["helper"] = tk.helper
return context

@ -0,0 +1,23 @@
from .models import LoginToken
def check_helper(get_response):
# One-time configuration and initialization.
def middleware(request):
# Code to be executed for each request before
# the view (and later middleware) are called.
if request.session.get("token"):
tk = LoginToken.objects.get(pk=request.session.get("token"))
if not tk:
del request.session["token"]
else:
request.helper = tk.helper
response = get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
return middleware

@ -14,14 +14,12 @@ def index(request):
# 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["my_shifts"] = helper.shiftregistration_set.filter(
if request.helper:
context["my_shifts"] = request.helper.shiftregistration_set.filter(
shift__start_at__gt=timezone.now()
).order_by("shift__start_at")
context["current_shift"] = (
helper.shiftregistration_set.annotate(
request.helper.shiftregistration_set.annotate(
shift_end=ExpressionWrapper(
F("shift__start_at") + F("shift__duration"),
output_field=DateTimeField(),
@ -36,13 +34,13 @@ def index(request):
.filter(start_at__gt=timezone.now(), room__required_helpers__gt=F("reg_count"))
.order_by("start_at")
)
if helper:
if request.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))
.filter(~Q(shiftregistration__helper=request.helper))
.order_by("start_at")
)
context["free_shifts"] = free_shifts
@ -75,7 +73,9 @@ def logout(request):
def register(request):
# handle number already being registered(resend message?)
if request.session.get("token"):
if request.helper:
if request.session.get("last_seen_shift"):
return redirect("shift", shiftid=request.session["last_seen_shift"])
return redirect("index")
context = {}
if request.method == "POST":
@ -102,7 +102,7 @@ def register(request):
def shift(request, shiftid):
shift = get_object_or_404(Shift, pk=shiftid)
helper = None
helper = request.helper
context = {
"can_register": False,
"is_registered": False,
@ -114,8 +114,7 @@ def shift(request, shiftid):
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
if helper:
context["helper"] = helper
reg = ShiftRegistration.objects.filter(shift=shift, helper=helper)
if reg:
@ -126,7 +125,6 @@ def shift(request, shiftid):
if request.method == "POST":
if EmptyForm(request.POST).is_valid():
if not helper:
# todo: add flash message that signup is required?
messages.add_message(
request,
messages.WARNING,
@ -160,9 +158,8 @@ def cancel(request, shiftid):
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)
reg = get_object_or_404(ShiftRegistration, helper=request.helper, shift=shift)
if reg.can_cancel():
reg.delete()
messages.add_message(

@ -52,6 +52,7 @@ MIDDLEWARE = [
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"shiftregister.app.middleware.check_helper"
]
ROOT_URLCONF = "shiftregister.urls"

Loading…
Cancel
Save