add current/next shift to menu

pull/1/head
parent c8a4575c1b
commit 9601367f12

@ -3,6 +3,7 @@ import secrets
from django.shortcuts import reverse from django.shortcuts import reverse
from datetime import timedelta from datetime import timedelta
from django.utils import timezone from django.utils import timezone
from django.db.models import F, Count, Q, ExpressionWrapper
class Room(models.Model): class Room(models.Model):
@ -22,6 +23,8 @@ class Shift(models.Model):
return f"{self.room.name}: {self.start_at}" return f"{self.room.name}: {self.start_at}"
def has_ended(self): def has_ended(self):
return (self.start_at + self.duration) < timezone.now() 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): class Helper(models.Model):
@ -41,6 +44,17 @@ class Helper(models.Model):
msg.save() msg.save()
return token 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): class ShiftRegistration(models.Model):
class Meta: class Meta:

@ -5,6 +5,20 @@
{% block navbar %} {% block navbar %}
{% if not helper%} {% if not helper%}
<a class="navbar-item" href="{% url 'register' %}">Anmelden</a> <a class="navbar-item" href="{% url 'register' %}">Anmelden</a>
{% else %}
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="navbarfoo">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
<div class="navbar-menu" id="navbarfoo">
{% if not helper.number_validated %}
<p class="navbar-item has-text-danger">Bestaige deien Telefonnummer ueber den Link in der SMS</p>
{% endif %}
{% if helper.important_shift %}
<a class="navbar-item" href="{% url 'shift' helper.important_shift.pk %}">{%if helper.important_shift.is_running%}Laufende{% else %}Nächste{% endif %} Schicht</a>
{% endif %}
</div>
{% endif %} {% endif %}
{% endblock %} {% endblock %}

@ -4,7 +4,7 @@
{% block content %} {% block content %}
{% if current_shift %} {% if current_shift %}
<h2>Aktuelle Schicht</h2> <h2>Aktuelle Schicht</h2>
{{ current_shift.shift.room.name }} {{ current_shift.shift.start_at }}<a href="{% url 'shift' current_shift.shift.id %}">Details</a> {{ current_shift.room.name }} {{ current_shift.start_at }}<a href="{% url 'shift' current_shift.id %}">Details</a>
{% endif %} {% endif %}
{% if my_shifts %} {% if my_shifts %}
<h2>Meine Schichten</h2> <h2>Meine Schichten</h2>

@ -19,17 +19,11 @@ def index(request):
context["my_shifts"] = request.helper.shiftregistration_set.filter( context["my_shifts"] = request.helper.shiftregistration_set.filter(
shift__start_at__gt=timezone.now() shift__start_at__gt=timezone.now()
).order_by("shift__start_at") ).order_by("shift__start_at")
context["current_shift"] = (
request.helper.shiftregistration_set.annotate( imp_shift = request.helper.important_shift()
shift_end=ExpressionWrapper( print(imp_shift)
F("shift__start_at") + F("shift__duration"), if imp_shift and imp_shift.is_running():
output_field=DateTimeField(), context["current_shift"] = imp_shift
)
)
.filter(shift__start_at__lte=timezone.now(), shift_end__gte=timezone.now())
.order_by("shift__start_at")
.first()
)
free_shifts = ( free_shifts = (
Shift.objects.annotate(reg_count=Count("shiftregistration")) Shift.objects.annotate(reg_count=Count("shiftregistration"))

@ -21,6 +21,19 @@
{% block body %} {% block body %}
{% include 'notifications.html' %} {% include 'notifications.html' %}
{% endblock %} {% endblock %}
<script>document.querySelectorAll('.delete').forEach(btn => btn.addEventListener('click', event => event.target.parentElement.remove()));</script> <script>
document.querySelectorAll('.delete').forEach(btn => btn.addEventListener('click', event => event.target.parentElement.remove()));
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
if ($navbarBurgers.length > 0) {
$navbarBurgers.forEach( el => {
el.addEventListener('click', () => {
const target = el.dataset.target;
const $target = document.getElementById(target);
el.classList.toggle('is-active');
$target.classList.toggle('is-active');
});
});
}
</script>
</body> </body>
</html> </html>

Loading…
Cancel
Save