diff --git a/shiftregister/app/models.py b/shiftregister/app/models.py index aa23702..c2452b9 100644 --- a/shiftregister/app/models.py +++ b/shiftregister/app/models.py @@ -3,6 +3,7 @@ import secrets from django.shortcuts import reverse from datetime import timedelta from django.utils import timezone +from django.db.models import F, Count, Q, ExpressionWrapper class Room(models.Model): @@ -22,6 +23,8 @@ class Shift(models.Model): return f"{self.room.name}: {self.start_at}" def has_ended(self): return (self.start_at + self.duration) < timezone.now() + def is_running(self): + return (self.start_at <=timezone.now()) and (not self.has_ended()) class Helper(models.Model): @@ -40,6 +43,17 @@ class Helper(models.Model): msg = Message(to=self, text=text) msg.save() return token + + # current or next shift + def important_shift(self): + ret = ShiftRegistration.objects.annotate( + shift_end=ExpressionWrapper( + F("shift__start_at") + F("shift__duration"), + output_field=models.DateTimeField(), + )).filter(helper=self, shift_end__gte=timezone.now()).order_by("shift__start_at").first() + if ret: + return ret.shift + class ShiftRegistration(models.Model): diff --git a/shiftregister/app/templates/helper_base.html b/shiftregister/app/templates/helper_base.html index 5164754..21a6cc4 100644 --- a/shiftregister/app/templates/helper_base.html +++ b/shiftregister/app/templates/helper_base.html @@ -5,6 +5,20 @@ {% block navbar %} {% if not helper%} Anmelden +{% else %} + + {% endif %} {% endblock %} diff --git a/shiftregister/app/templates/shiftlist.html b/shiftregister/app/templates/shiftlist.html index 3e6360c..3087883 100644 --- a/shiftregister/app/templates/shiftlist.html +++ b/shiftregister/app/templates/shiftlist.html @@ -4,7 +4,7 @@ {% block content %} {% if current_shift %}

Aktuelle Schicht

-{{ current_shift.shift.room.name }} {{ current_shift.shift.start_at }}Details +{{ current_shift.room.name }} {{ current_shift.start_at }}Details {% endif %} {% if my_shifts %}

Meine Schichten

diff --git a/shiftregister/app/views.py b/shiftregister/app/views.py index 8237049..3c77335 100644 --- a/shiftregister/app/views.py +++ b/shiftregister/app/views.py @@ -19,17 +19,11 @@ def index(request): context["my_shifts"] = request.helper.shiftregistration_set.filter( shift__start_at__gt=timezone.now() ).order_by("shift__start_at") - context["current_shift"] = ( - request.helper.shiftregistration_set.annotate( - shift_end=ExpressionWrapper( - F("shift__start_at") + F("shift__duration"), - output_field=DateTimeField(), - ) - ) - .filter(shift__start_at__lte=timezone.now(), shift_end__gte=timezone.now()) - .order_by("shift__start_at") - .first() - ) + + imp_shift = request.helper.important_shift() + print(imp_shift) + if imp_shift and imp_shift.is_running(): + context["current_shift"] = imp_shift free_shifts = ( Shift.objects.annotate(reg_count=Count("shiftregistration")) diff --git a/templates/base.html b/templates/base.html index 61c4a4a..7a90e75 100644 --- a/templates/base.html +++ b/templates/base.html @@ -21,6 +21,19 @@ {% block body %} {% include 'notifications.html' %} {% endblock %} - +