Fix point of order weighting

This commit is contained in:
Finn Stutzenstein 2021-02-22 13:33:36 +01:00
parent baad950698
commit f8446ee609
No known key found for this signature in database
GPG Key ID: 9042F605C6324654

View File

@ -475,45 +475,34 @@ class SpeakerManager(models.Manager):
Note that this method has side-effects: It might change the weight of other speakers to make Note that this method has side-effects: It might change the weight of other speakers to make
space for the new point of order speaker. space for the new point of order speaker.
""" """
if point_of_order: queryset = self.filter(list_of_speakers=list_of_speakers, begin_time=None)
# get non-poo speakers out of the way
if not point_of_order:
return (queryset.aggregate(models.Max("weight"))["weight__max"] or 0) + 1
# get the first waiting speaker # get the first waiting speaker
first_speaker = self.filter(begin_time=None).order_by("weight").first() first_speaker = queryset.order_by("weight").first()
if first_speaker is None: if first_speaker is None:
return 1 return 1 # no speakers yet
if not first_speaker.point_of_order: if not first_speaker.point_of_order:
return first_speaker.weight - 1 return first_speaker.weight - 1
# get the first non-poo speaker # get the first non-poo speaker
first_non_poo_speaker = ( first_non_poo_speaker = (
self.filter(begin_time=None, point_of_order=False) queryset.filter(point_of_order=False).order_by("weight").first()
.order_by("weight")
.first()
) )
if first_non_poo_speaker is None: if first_non_poo_speaker is None:
# max weight + 1, the speaker is last since there are no non-poo speakers # max weight + 1, the speaker is last since there are no non-poo speakers
return ( return (queryset.aggregate(models.Max("weight"))["weight__max"] or 0) + 1
self.filter(
list_of_speakers=list_of_speakers, begin_time=None
).aggregate(models.Max("weight"))["weight__max"]
+ 1
)
weight = first_non_poo_speaker.weight weight = first_non_poo_speaker.weight
# add +1 to all speakers with weight >= first_non_poo_speaker.weight # add +1 to all speakers with weight >= first_non_poo_speaker.weight
self.filter( queryset.filter(weight__gte=weight).update(weight=F("weight") + 1)
list_of_speakers=list_of_speakers, begin_time=None, weight__gte=weight
).update(weight=F("weight") + 1)
return weight return weight
else:
return (
self.filter(list_of_speakers=list_of_speakers).aggregate(
models.Max("weight")
)["weight__max"]
or 0
) + 1
class Speaker(RESTModelMixin, models.Model): class Speaker(RESTModelMixin, models.Model):