Fix middleware and formatting

pull/1/head
Luca 3 years ago
parent 7dd90e2c2c
commit aac22c9eb6

@ -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()

@ -1,8 +1,12 @@
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"):
@ -12,7 +16,6 @@ def check_helper(get_response):
else:
request.helper = tk.helper
response = get_response(request)
# Code to be executed for each request/response after

@ -26,12 +26,13 @@
{% if DEBUG %}
<footer class="footer">
Debug Mode:
<div class="container">
Debug-Modus:
{% if helper %}
<a href="{% url 'token_logout' %}">logout</a>
<a href="{{ helper.logintoken_set.first.get_absolute_url }}">login url</a>
{% endif %}
</div>
</footer>
{% endif %}
{% endblock %}

@ -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)

@ -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"

@ -19,12 +19,11 @@
</div>
</nav>
{% for message in messages %}
<div class="notification {% for tag in message.tags.split %} is-{{ tag }} {% endfor %}}">
<div class="notification{% for tag in message.tags.split %} is-{{ tag }}{% endfor %}">
<button class="delete"></button>
{{ message }}
</div>
{% endfor %}
{% block body %}
{% endblock %}
</body>

Loading…
Cancel
Save