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): # def helpers(self, object):
# object.helpers.count() # object.helpers.count()
def free_slots(self, object): def free_slots(self, object):
return object.room.required_helpers - object.shiftregistration_set.count() return object.room.required_helpers - object.shiftregistration_set.count()

@ -1,8 +1,12 @@
from .models import LoginToken from .models import LoginToken
def check_helper(get_response): def check_helper(get_response):
# One-time configuration and initialization. # One-time configuration and initialization.
def middleware(request): def middleware(request):
request.helper = None
# Code to be executed for each request before # Code to be executed for each request before
# the view (and later middleware) are called. # the view (and later middleware) are called.
if request.session.get("token"): if request.session.get("token"):
@ -12,7 +16,6 @@ def check_helper(get_response):
else: else:
request.helper = tk.helper request.helper = tk.helper
response = get_response(request) response = get_response(request)
# Code to be executed for each request/response after # Code to be executed for each request/response after

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

@ -11,6 +11,7 @@ from django.contrib import messages
def index(request): def index(request):
if request.session.get("last_seen_shift"): if request.session.get("last_seen_shift"):
del request.session["last_seen_shift"] del request.session["last_seen_shift"]
# 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 = {}
@ -29,11 +30,13 @@ def index(request):
.order_by("shift__start_at") .order_by("shift__start_at")
.first() .first()
) )
free_shifts = ( free_shifts = (
Shift.objects.annotate(reg_count=Count("shiftregistration")) Shift.objects.annotate(reg_count=Count("shiftregistration"))
.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 request.helper: if request.helper:
free_shifts = ( free_shifts = (
Shift.objects.annotate(reg_count=Count("shiftregistration")) Shift.objects.annotate(reg_count=Count("shiftregistration"))
@ -43,6 +46,7 @@ def index(request):
.filter(~Q(shiftregistration__helper=request.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
return render(request, "shiftlist.html", context) return render(request, "shiftlist.html", context)
@ -57,17 +61,21 @@ def login(request, token):
messages.SUCCESS, messages.SUCCESS,
"Nummer bestätigt, Du kannst dich jetzt für Schichten anmelden", "Nummer bestätigt, Du kannst dich jetzt für Schichten anmelden",
) )
request.session["token"] = token request.session["token"] = token
# if the user was viewing a single shift before registering, they probably want to register for that # if the user was viewing a single shift before registering, they probably want to register for that
# shift so we redirect them there. # shift so we redirect them there.
if request.session.get("last_seen_shift"): if request.session.get("last_seen_shift"):
return redirect("shift", shiftid=request.session["last_seen_shift"]) return redirect("shift", shiftid=request.session["last_seen_shift"])
return redirect("index") return redirect("index")
def logout(request): def logout(request):
if request.session.get("token"): if request.session.get("token"):
del request.session["token"] del request.session["token"]
return redirect("index") return redirect("index")
@ -76,13 +84,16 @@ def register(request):
if request.helper: if request.helper:
if request.session.get("last_seen_shift"): if request.session.get("last_seen_shift"):
return redirect("shift", shiftid=request.session["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":
form = RegisterForm(request.POST) form = RegisterForm(request.POST)
if not form.is_valid(): if not form.is_valid():
context["form"] = form context["form"] = form
return render(request, "register.html", context) return render(request, "register.html", context)
helper = Helper( helper = Helper(
name=form.cleaned_data["name"], phone=form.cleaned_data["phone"] name=form.cleaned_data["name"], phone=form.cleaned_data["phone"]
) )
@ -95,7 +106,9 @@ def register(request):
messages.INFO, messages.INFO,
f"DEBUG: login token: {token.get_absolute_url()}", f"DEBUG: login token: {token.get_absolute_url()}",
) )
return render(request, "wait_confirmation.html", {"helper": helper}) return render(request, "wait_confirmation.html", {"helper": helper})
context["form"] = RegisterForm() context["form"] = RegisterForm()
return render(request, "register.html", context) return render(request, "register.html", context)
@ -110,10 +123,12 @@ def shift(request, shiftid):
"shift": shift, "shift": shift,
"shift_form": EmptyForm, "shift_form": EmptyForm,
} }
# this currently ignores date/time # this currently ignores date/time
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 helper: if helper:
context["helper"] = helper context["helper"] = helper
reg = ShiftRegistration.objects.filter(shift=shift, helper=helper) reg = ShiftRegistration.objects.filter(shift=shift, helper=helper)
@ -122,6 +137,7 @@ def shift(request, shiftid):
context["can_register"] = False context["can_register"] = False
if reg[0].can_cancel(): if reg[0].can_cancel():
context["can_cancel"] = True context["can_cancel"] = True
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:
@ -148,6 +164,7 @@ def shift(request, shiftid):
) )
# redirect so page can be reloaded without resending post data # redirect so page can be reloaded without resending post data
return redirect("shift", shiftid=shift.pk) return redirect("shift", shiftid=shift.pk)
return render(request, "shift.html", context) return render(request, "shift.html", context)
@ -158,6 +175,7 @@ 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)
shift = get_object_or_404(Shift, pk=shiftid) shift = get_object_or_404(Shift, pk=shiftid)
reg = get_object_or_404(ShiftRegistration, helper=request.helper, shift=shift) reg = get_object_or_404(ShiftRegistration, helper=request.helper, shift=shift)
if reg.can_cancel(): if reg.can_cancel():
@ -173,4 +191,5 @@ def cancel(request, shiftid):
messages.WARNING, messages.WARNING,
"Abmeldung nicht (mehr) möglich, bitte wende dich an den Infopoint", "Abmeldung nicht (mehr) möglich, bitte wende dich an den Infopoint",
) )
return redirect("shift", shiftid=shiftid) return redirect("shift", shiftid=shiftid)

@ -52,7 +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" "shiftregister.app.middleware.check_helper",
] ]
ROOT_URLCONF = "shiftregister.urls" ROOT_URLCONF = "shiftregister.urls"

@ -18,13 +18,12 @@
</div> </div>
</div> </div>
</nav> </nav>
{% for message in messages %} {% 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> <button class="delete"></button>
{{ message }} {{ message }}
</div> </div>
{% endfor %} {% endfor %}
{% block body %} {% block body %}
{% endblock %} {% endblock %}
</body> </body>

Loading…
Cancel
Save