use tzaware now for db queries

pull/1/head
parent 4269418065
commit be767b6ee2

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

Loading…
Cancel
Save