diff --git a/bda/algorithm.py b/bda/algorithm.py index bf9b690f..7f18ce18 100644 --- a/bda/algorithm.py +++ b/bda/algorithm.py @@ -22,8 +22,7 @@ class Algorithm(object): show.requests - on crée des tables de demandes pour chaque personne, afin de pouvoir modifier les rankings""" - self.max_group = \ - 2 * choices.aggregate(Max('priority'))['priority__max'] + self.max_group = 2*max(choice.priority for choice in choices) self.shows = [] showdict = {} for show in shows: diff --git a/bda/views.py b/bda/views.py index 089c1f29..b6dd6aec 100644 --- a/bda/views.py +++ b/bda/views.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +from collections import defaultdict from functools import partial import random import hashlib @@ -262,9 +263,9 @@ def do_tirage(tirage_elt, token): # Initialisation du dictionnaire data qui va contenir les résultats start = time.time() data = { - 'shows': tirage_elt.spectacle_set.select_related().all(), + 'shows': tirage_elt.spectacle_set.select_related('location'), 'token': token, - 'members': tirage_elt.participant_set.all(), + 'members': tirage_elt.participant_set.select_related('user'), 'total_slots': 0, 'total_losers': 0, 'total_sold': 0, @@ -277,7 +278,7 @@ def do_tirage(tirage_elt, token): ChoixSpectacle.objects .filter(spectacle__tirage=tirage_elt) .order_by('participant', 'priority') - .select_related().all() + .select_related('participant', 'participant__user', 'spectacle') ) results = Algorithm(data['shows'], data['members'], choices)(token) @@ -334,10 +335,18 @@ def do_tirage(tirage_elt, token): ]) # On inscrit à BdA-Revente ceux qui n'ont pas eu les places voulues - for (show, _, losers) in results: - for (loser, _, _, _) in losers: - loser.choicesrevente.add(show) - loser.save() + ChoixRevente = Participant.choicesrevente.through + + lost_by = defaultdict(set) + for show, _, losers in results: + for loser, _, _, _ in losers: + lost_by[loser].add(show) + + ChoixRevente.objects.bulk_create( + ChoixRevente(participant=member, spectacle=show) + for member, shows in lost_by.items() + for show in shows + ) data["duration"] = time.time() - start data["results"] = results