2
0
Fork 0

use tzaware now for db queries

This commit is contained in:
Andreas (@xAndy) Zimmermann 2022-04-13 18:34:41 +02:00
parent 4269418065
commit be767b6ee2
1 changed files with 5 additions and 6 deletions

View File

@ -1,10 +1,9 @@
from django.shortcuts import render, redirect, get_object_or_404
from .models import Shift, LoginToken, Helper, ShiftRegistration
import datetime
from django.db.models import F, Count, Q, ExpressionWrapper
from .forms import RegisterForm, EmptyForm
from django.db.models.fields import DateTimeField
from pprint import pprint
from django.utils import timezone
def index(request):
if request.session.get('last_seen_shift'):
@ -16,12 +15,12 @@ def index(request):
if request.session.get('token'):
helper = LoginToken.objects.get(pk=request.session['token']).helper
context['helper'] = helper
context['my_shifts'] = helper.shiftregistration_set.filter(shift__start_at__gt=datetime.datetime.now()).order_by('shift__start_at')
context['current_shift'] = next(iter(helper.shiftregistration_set.annotate(shift_end=ExpressionWrapper(F('shift__start_at')+F('shift__duration'), output_field=DateTimeField())).filter(shift__start_at__lte=datetime.datetime.now(), shift_end__gte=datetime.datetime.now()).order_by('shift__start_at')[:1]), None)
free_shifts = Shift.objects.annotate(reg_count=Count('shiftregistration')).filter(start_at__gt=datetime.datetime.now(),
context['my_shifts'] = helper.shiftregistration_set.filter(shift__start_at__gt=timezone.now()).order_by('shift__start_at')
context['current_shift'] = next(iter(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')[:1]), None)
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 helper:
free_shifts = Shift.objects.annotate(reg_count=Count('shiftregistration')).filter(start_at__gt=datetime.datetime.now(),
free_shifts = Shift.objects.annotate(reg_count=Count('shiftregistration')).filter(start_at__gt=timezone.now(),
room__required_helpers__gt=F('reg_count')).filter( ~Q(shiftregistration__helper=helper)).order_by('start_at')
context['free_shifts']= free_shifts
return render(request, 'shiftlist.html', context)