replace fix/iter with next on queries

pull/1/head
parent 6209a2671e
commit f4a0ee55d4

@ -20,20 +20,16 @@ def index(request):
context["my_shifts"] = helper.shiftregistration_set.filter( context["my_shifts"] = 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"] = next( context["current_shift"] = (
iter( helper.shiftregistration_set.annotate(
helper.shiftregistration_set.annotate( shift_end=ExpressionWrapper(
shift_end=ExpressionWrapper( F("shift__start_at") + F("shift__duration"),
F("shift__start_at") + F("shift__duration"), output_field=DateTimeField(),
output_field=DateTimeField(),
)
) )
.filter( )
shift__start_at__lte=timezone.now(), shift_end__gte=timezone.now() .filter(shift__start_at__lte=timezone.now(), shift_end__gte=timezone.now())
) .order_by("shift__start_at")
.order_by("shift__start_at")[:1] .first()
),
None,
) )
free_shifts = ( free_shifts = (
Shift.objects.annotate(reg_count=Count("shiftregistration")) Shift.objects.annotate(reg_count=Count("shiftregistration"))

@ -16,26 +16,22 @@ def index(request):
def shift_overview(request): def shift_overview(request):
context = {} context = {}
context["running_shifts"] = ( context["running_shifts"] = (
reg.shift shift
for reg in ShiftRegistration.objects.annotate( for shift in Shift.objects.annotate(
shift_end=ExpressionWrapper( end_at=ExpressionWrapper(
F("shift__start_at") + F("shift__duration"), F("start_at") + F("duration"),
output_field=DateTimeField(), output_field=DateTimeField(),
) )
) )
.filter(shift__start_at__lte=timezone.now(), shift_end__gte=timezone.now()) .filter(start_at__lte=timezone.now(), end_at__gte=timezone.now())
.order_by("shift__start_at") .order_by("start_at")
) )
# probably can do some distinct/group by stuff but not sure how tih django queries # probably can do some distinct/group by stuff but not sure how tih django queries
context["next_shifts"] = ( context["next_shifts"] = (
next( Shift.objects.filter(room=room, start_at__gt=timezone.now())
iter( .order_by("start_at")
Shift.objects.filter(room=room, start_at__gt=timezone.now()).order_by( .first()
"start_at"
)
)
)
for room in Room.objects.all() for room in Room.objects.all()
) )

Loading…
Cancel
Save