move token->helper into middleware to simplify code and error paths
This commit is contained in:
parent
f4a0ee55d4
commit
5077333ff1
|
@ -5,8 +5,6 @@ from .models import LoginToken
|
||||||
def proc(request):
|
def proc(request):
|
||||||
context = {
|
context = {
|
||||||
"DEBUG": settings.DEBUG,
|
"DEBUG": settings.DEBUG,
|
||||||
|
"helper": request.helper,
|
||||||
}
|
}
|
||||||
tk = LoginToken.objects.filter(pk=request.session.get("token")).first()
|
|
||||||
if tk:
|
|
||||||
context["helper"] = tk.helper
|
|
||||||
return context
|
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?
|
# dont show shifts starting in <60 minutes?
|
||||||
# currently only sorts by date
|
# currently only sorts by date
|
||||||
context = {}
|
context = {}
|
||||||
helper = None
|
if request.helper:
|
||||||
if request.session.get("token"):
|
context["my_shifts"] = request.helper.shiftregistration_set.filter(
|
||||||
helper = LoginToken.objects.get(pk=request.session["token"]).helper
|
|
||||||
context["my_shifts"] = helper.shiftregistration_set.filter(
|
|
||||||
shift__start_at__gt=timezone.now()
|
shift__start_at__gt=timezone.now()
|
||||||
).order_by("shift__start_at")
|
).order_by("shift__start_at")
|
||||||
context["current_shift"] = (
|
context["current_shift"] = (
|
||||||
helper.shiftregistration_set.annotate(
|
request.helper.shiftregistration_set.annotate(
|
||||||
shift_end=ExpressionWrapper(
|
shift_end=ExpressionWrapper(
|
||||||
F("shift__start_at") + F("shift__duration"),
|
F("shift__start_at") + F("shift__duration"),
|
||||||
output_field=DateTimeField(),
|
output_field=DateTimeField(),
|
||||||
|
@ -36,13 +34,13 @@ def index(request):
|
||||||
.filter(start_at__gt=timezone.now(), room__required_helpers__gt=F("reg_count"))
|
.filter(start_at__gt=timezone.now(), room__required_helpers__gt=F("reg_count"))
|
||||||
.order_by("start_at")
|
.order_by("start_at")
|
||||||
)
|
)
|
||||||
if helper:
|
if request.helper:
|
||||||
free_shifts = (
|
free_shifts = (
|
||||||
Shift.objects.annotate(reg_count=Count("shiftregistration"))
|
Shift.objects.annotate(reg_count=Count("shiftregistration"))
|
||||||
.filter(
|
.filter(
|
||||||
start_at__gt=timezone.now(), room__required_helpers__gt=F("reg_count")
|
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")
|
.order_by("start_at")
|
||||||
)
|
)
|
||||||
context["free_shifts"] = free_shifts
|
context["free_shifts"] = free_shifts
|
||||||
|
@ -75,7 +73,9 @@ def logout(request):
|
||||||
|
|
||||||
def register(request):
|
def register(request):
|
||||||
# handle number already being registered(resend message?)
|
# 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")
|
return redirect("index")
|
||||||
context = {}
|
context = {}
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
|
@ -102,7 +102,7 @@ def register(request):
|
||||||
|
|
||||||
def shift(request, shiftid):
|
def shift(request, shiftid):
|
||||||
shift = get_object_or_404(Shift, pk=shiftid)
|
shift = get_object_or_404(Shift, pk=shiftid)
|
||||||
helper = None
|
helper = request.helper
|
||||||
context = {
|
context = {
|
||||||
"can_register": False,
|
"can_register": False,
|
||||||
"is_registered": False,
|
"is_registered": False,
|
||||||
|
@ -114,8 +114,7 @@ def shift(request, shiftid):
|
||||||
request.session["last_seen_shift"] = shiftid
|
request.session["last_seen_shift"] = shiftid
|
||||||
if shift.room.required_helpers > shift.shiftregistration_set.count():
|
if shift.room.required_helpers > shift.shiftregistration_set.count():
|
||||||
context["can_register"] = True
|
context["can_register"] = True
|
||||||
if request.session.get("token"):
|
if helper:
|
||||||
helper = LoginToken.objects.get(pk=request.session["token"]).helper
|
|
||||||
context["helper"] = helper
|
context["helper"] = helper
|
||||||
reg = ShiftRegistration.objects.filter(shift=shift, helper=helper)
|
reg = ShiftRegistration.objects.filter(shift=shift, helper=helper)
|
||||||
if reg:
|
if reg:
|
||||||
|
@ -126,7 +125,6 @@ def shift(request, shiftid):
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
if EmptyForm(request.POST).is_valid():
|
if EmptyForm(request.POST).is_valid():
|
||||||
if not helper:
|
if not helper:
|
||||||
# todo: add flash message that signup is required?
|
|
||||||
messages.add_message(
|
messages.add_message(
|
||||||
request,
|
request,
|
||||||
messages.WARNING,
|
messages.WARNING,
|
||||||
|
@ -160,9 +158,8 @@ def cancel(request, shiftid):
|
||||||
return redirect("shift", shiftid=shiftid)
|
return redirect("shift", shiftid=shiftid)
|
||||||
if not request.session.get("token"):
|
if not request.session.get("token"):
|
||||||
return redirect("shift", shiftid=shiftid)
|
return redirect("shift", shiftid=shiftid)
|
||||||
helper = LoginToken.objects.get(pk=request.session["token"]).helper
|
|
||||||
shift = get_object_or_404(Shift, pk=shiftid)
|
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():
|
if reg.can_cancel():
|
||||||
reg.delete()
|
reg.delete()
|
||||||
messages.add_message(
|
messages.add_message(
|
||||||
|
|
|
@ -52,6 +52,7 @@ MIDDLEWARE = [
|
||||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||||
"django.contrib.messages.middleware.MessageMiddleware",
|
"django.contrib.messages.middleware.MessageMiddleware",
|
||||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
|
"shiftregister.app.middleware.check_helper"
|
||||||
]
|
]
|
||||||
|
|
||||||
ROOT_URLCONF = "shiftregister.urls"
|
ROOT_URLCONF = "shiftregister.urls"
|
||||||
|
|
Loading…
Reference in New Issue