forked from DGNum/gestioCOF
Fewer db requests on bda tirage.
bda.algorithm - use iterator to find max_groups, instead of a db request bda.views.do_tirage - select_related() are now focused on some relationships (they were taking useless relationships) - bda-revente filling takes 1 request (each save and add was issuing 1 request)
This commit is contained in:
parent
3e0bd2e758
commit
9f307c1bd0
2 changed files with 17 additions and 9 deletions
|
@ -22,8 +22,7 @@ class Algorithm(object):
|
||||||
show.requests
|
show.requests
|
||||||
- on crée des tables de demandes pour chaque personne, afin de
|
- on crée des tables de demandes pour chaque personne, afin de
|
||||||
pouvoir modifier les rankings"""
|
pouvoir modifier les rankings"""
|
||||||
self.max_group = \
|
self.max_group = 2*max(choice.priority for choice in choices)
|
||||||
2 * choices.aggregate(Max('priority'))['priority__max']
|
|
||||||
self.shows = []
|
self.shows = []
|
||||||
showdict = {}
|
showdict = {}
|
||||||
for show in shows:
|
for show in shows:
|
||||||
|
|
23
bda/views.py
23
bda/views.py
|
@ -1,5 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import random
|
import random
|
||||||
import hashlib
|
import hashlib
|
||||||
|
@ -262,9 +263,9 @@ def do_tirage(tirage_elt, token):
|
||||||
# Initialisation du dictionnaire data qui va contenir les résultats
|
# Initialisation du dictionnaire data qui va contenir les résultats
|
||||||
start = time.time()
|
start = time.time()
|
||||||
data = {
|
data = {
|
||||||
'shows': tirage_elt.spectacle_set.select_related().all(),
|
'shows': tirage_elt.spectacle_set.select_related('location'),
|
||||||
'token': token,
|
'token': token,
|
||||||
'members': tirage_elt.participant_set.all(),
|
'members': tirage_elt.participant_set.select_related('user'),
|
||||||
'total_slots': 0,
|
'total_slots': 0,
|
||||||
'total_losers': 0,
|
'total_losers': 0,
|
||||||
'total_sold': 0,
|
'total_sold': 0,
|
||||||
|
@ -277,7 +278,7 @@ def do_tirage(tirage_elt, token):
|
||||||
ChoixSpectacle.objects
|
ChoixSpectacle.objects
|
||||||
.filter(spectacle__tirage=tirage_elt)
|
.filter(spectacle__tirage=tirage_elt)
|
||||||
.order_by('participant', 'priority')
|
.order_by('participant', 'priority')
|
||||||
.select_related().all()
|
.select_related('participant', 'participant__user', 'spectacle')
|
||||||
)
|
)
|
||||||
results = Algorithm(data['shows'], data['members'], choices)(token)
|
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
|
# On inscrit à BdA-Revente ceux qui n'ont pas eu les places voulues
|
||||||
for (show, _, losers) in results:
|
ChoixRevente = Participant.choicesrevente.through
|
||||||
for (loser, _, _, _) in losers:
|
|
||||||
loser.choicesrevente.add(show)
|
lost_by = defaultdict(set)
|
||||||
loser.save()
|
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["duration"] = time.time() - start
|
||||||
data["results"] = results
|
data["results"] = results
|
||||||
|
|
Loading…
Add table
Reference in a new issue