diff --git a/shiftregister/app/admin.py b/shiftregister/app/admin.py index 0611887..e33a2e8 100644 --- a/shiftregister/app/admin.py +++ b/shiftregister/app/admin.py @@ -14,6 +14,7 @@ class ShiftAdmin(admin.ModelAdmin): # def helpers(self, object): # object.helpers.count() + def free_slots(self, object): return object.room.required_helpers - object.shiftregistration_set.count() diff --git a/shiftregister/app/middleware.py b/shiftregister/app/middleware.py index 027fdf6..35f0821 100644 --- a/shiftregister/app/middleware.py +++ b/shiftregister/app/middleware.py @@ -1,17 +1,20 @@ from .models import LoginToken + + def check_helper(get_response): # One-time configuration and initialization. def middleware(request): + request.helper = None + # 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")) + 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) @@ -20,4 +23,4 @@ def check_helper(get_response): return response - return middleware \ No newline at end of file + return middleware diff --git a/shiftregister/app/templates/helper_base.html b/shiftregister/app/templates/helper_base.html index 101099f..581516a 100644 --- a/shiftregister/app/templates/helper_base.html +++ b/shiftregister/app/templates/helper_base.html @@ -24,14 +24,15 @@ -{% if DEBUG%} +{% if DEBUG %} {% endif %} - -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/shiftregister/app/views.py b/shiftregister/app/views.py index 8bd05b7..8237049 100644 --- a/shiftregister/app/views.py +++ b/shiftregister/app/views.py @@ -11,6 +11,7 @@ from django.contrib import messages 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 = {} @@ -29,11 +30,13 @@ def index(request): .order_by("shift__start_at") .first() ) + 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.helper: free_shifts = ( Shift.objects.annotate(reg_count=Count("shiftregistration")) @@ -43,6 +46,7 @@ def index(request): .filter(~Q(shiftregistration__helper=request.helper)) .order_by("start_at") ) + context["free_shifts"] = free_shifts return render(request, "shiftlist.html", context) @@ -57,17 +61,21 @@ def login(request, token): messages.SUCCESS, "Nummer bestätigt, Du kannst dich jetzt für Schichten anmelden", ) + 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") @@ -76,13 +84,16 @@ def register(request): 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": 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"] ) @@ -95,7 +106,9 @@ def register(request): messages.INFO, f"DEBUG: login token: {token.get_absolute_url()}", ) + return render(request, "wait_confirmation.html", {"helper": helper}) + context["form"] = RegisterForm() return render(request, "register.html", context) @@ -110,10 +123,12 @@ def shift(request, shiftid): "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 helper: context["helper"] = helper reg = ShiftRegistration.objects.filter(shift=shift, helper=helper) @@ -122,6 +137,7 @@ def shift(request, shiftid): 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: @@ -148,6 +164,7 @@ def shift(request, shiftid): ) # redirect so page can be reloaded without resending post data return redirect("shift", shiftid=shift.pk) + return render(request, "shift.html", context) @@ -158,6 +175,7 @@ def cancel(request, shiftid): return redirect("shift", shiftid=shiftid) if not request.session.get("token"): return redirect("shift", shiftid=shiftid) + shift = get_object_or_404(Shift, pk=shiftid) reg = get_object_or_404(ShiftRegistration, helper=request.helper, shift=shift) if reg.can_cancel(): @@ -173,4 +191,5 @@ def cancel(request, shiftid): messages.WARNING, "Abmeldung nicht (mehr) möglich, bitte wende dich an den Infopoint", ) + return redirect("shift", shiftid=shiftid) diff --git a/shiftregister/settings.py b/shiftregister/settings.py index df0b2a2..44405cf 100644 --- a/shiftregister/settings.py +++ b/shiftregister/settings.py @@ -52,7 +52,7 @@ MIDDLEWARE = [ "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", - "shiftregister.app.middleware.check_helper" + "shiftregister.app.middleware.check_helper", ] ROOT_URLCONF = "shiftregister.urls" diff --git a/templates/base.html b/templates/base.html index 0a6fe2b..3ed456e 100644 --- a/templates/base.html +++ b/templates/base.html @@ -18,13 +18,12 @@ - {% for message in messages %} -
- {% endfor %} - +{% for message in messages %} + +{% endfor %} {% block body %} {% endblock %}