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,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
return middleware

@ -24,14 +24,15 @@
</div>
</section>
{% if DEBUG%}
{% 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>
<a href="{% url 'token_logout' %}">logout</a>
<a href="{{ helper.logintoken_set.first.get_absolute_url }}">login url</a>
{% endif %}
</div>
</footer>
{% endif %}
{% endblock %}
{% 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"

@ -18,13 +18,12 @@
</div>
</div>
</nav>
{% for message in messages %}
<div class="notification {% for tag in message.tags.split %} is-{{ tag }} {% endfor %}}">
<button class="delete"></button>
{{ message }}
</div>
{% endfor %}
{% for message in messages %}
<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