mirror of https://gitlab.crans.org/bde/nk20
base for questions instead of words
This commit is contained in:
parent
ba0d64f0d4
commit
9596aa7b8c
|
@ -13,31 +13,43 @@ from django.utils.translation import gettext_lazy as _
|
||||||
from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm, WEIBusInformation
|
from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm, WEIBusInformation
|
||||||
from ...models import WEIMembership
|
from ...models import WEIMembership
|
||||||
|
|
||||||
WORDS = [
|
WORDS = {
|
||||||
'ABBA', 'After', 'Alcoolique anonyme', 'Ambiance festive', 'Années 2000', 'Apéro', 'Art',
|
"ambiance":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
'Baby foot billard biere pong', 'BBQ', 'Before', 'Bière pong', 'Bon enfant', 'Calme', 'Canapé',
|
"musique":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
'Chanson paillarde', 'Chanson populaire', 'Chartreuse', 'Cheerleader', 'Chill', 'Choré',
|
"boisson":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
'Cinéma', 'Cocktail', 'Comédie musicle', 'Commercial', 'Copaing', 'Danse', 'Dancefloor',
|
"beauferie":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
'Electro', 'Fanfare', 'Gin tonic', 'Inclusif', 'Jazz', "Jeux d'alcool", 'Jeux de carte',
|
"sommeil":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
'Jeux de rôle', 'Jeux de société', 'JUL', 'Jus de fruit', 'Kfet', 'Kleptomanie assurée',
|
"vacances":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
'LGBTQ+', 'Livre', 'Morning beer', 'Musique', 'NAPS', 'Paillettes', 'Pastis', 'Paté Hénaff',
|
"activite":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
'Peluche', 'Pena baiona', "Peu d'alcool", 'Pilier de bar', 'PMU', 'Poulpe', 'Punch', 'Rap',
|
"hygiene":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
'Réveil', 'Rock', 'Rugby', 'Sandwich', 'Serge', 'Shot', 'Sociable', 'Spectacle', 'Techno',
|
"animal":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
'Techno house', 'Thérapie Taxi', 'Tradition kchanaises', 'Troisième mi-temps', 'Turn up',
|
"fensfoire":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
'Vodka', 'Vodka pomme', 'Volley', 'Vomi stratégique'
|
"kokarde":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
]
|
"copain":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
|
"vie":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
|
"jeux":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
|
"calin":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
|
"vommi":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
|
"kfet":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
|
"fatigue":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
|
"duree trajet":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
|
||||||
|
"scolarite":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class WEISurveyForm2023(forms.Form):
|
class WEISurveyForm2023(forms.Form):
|
||||||
"""
|
"""
|
||||||
Survey form for the year 2023.
|
Survey form for the year 2023.
|
||||||
Members choose 20 words, from which we calculate the best associated bus.
|
Members answer 20 question, from which we calculate the best associated bus.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
word = forms.ChoiceField(
|
def __init__(self,**kwargs):
|
||||||
label=_("Choose a word:"),
|
super().__init__(**kwargs)
|
||||||
widget=forms.RadioSelect(),
|
for question in WORDS:
|
||||||
)
|
self.fields[question] = forms.ChoiceField(
|
||||||
|
label=WORDS[question][0]+question,
|
||||||
|
widget=forms.RadioSelect(),
|
||||||
|
)
|
||||||
|
|
||||||
def set_registration(self, registration):
|
def set_registration(self, registration):
|
||||||
"""
|
"""
|
||||||
|
@ -50,41 +62,32 @@ class WEISurveyForm2023(forms.Form):
|
||||||
registration._force_save = True
|
registration._force_save = True
|
||||||
registration.save()
|
registration.save()
|
||||||
|
|
||||||
if self.data:
|
# if self.data:
|
||||||
self.fields["word"].choices = [(w, w) for w in WORDS]
|
# for question in WORDS:
|
||||||
if self.is_valid():
|
# self.fields[question].choices = [answer for answer in WORDS[question][1]]
|
||||||
return
|
# if self.is_valid():
|
||||||
|
# return
|
||||||
|
|
||||||
rng = Random((information.step + 1) * information.seed)
|
# rng = Random(information.seed) # add someting if it's alwais the same
|
||||||
|
# questions = list(WORDS.keys())
|
||||||
|
# rng.shuffle(questions)
|
||||||
|
|
||||||
words = None
|
# for question in questions:
|
||||||
|
for question in WORDS:
|
||||||
buses = WEISurveyAlgorithm2023.get_buses()
|
answers = [(answer, WORDS[question][1][answer]) for answer in WORDS[question][1]]
|
||||||
informations = {bus: WEIBusInformation2023(bus) for bus in buses}
|
self.fields[question].choices = answers
|
||||||
scores = sum((list(informations[bus].scores.values()) for bus in buses), [])
|
|
||||||
average_score = sum(scores) / len(scores)
|
|
||||||
|
|
||||||
preferred_words = {bus: [word for word in WORDS
|
|
||||||
if informations[bus].scores[word] >= average_score]
|
|
||||||
for bus in buses}
|
|
||||||
while words is None or len(set(words)) != len(words):
|
|
||||||
# Ensure that there is no the same word 2 times
|
|
||||||
words = [rng.choice(words) for _ignored2, words in preferred_words.items()]
|
|
||||||
rng.shuffle(words)
|
|
||||||
words = [(w, w) for w in words]
|
|
||||||
self.fields["word"].choices = words
|
|
||||||
|
|
||||||
|
|
||||||
class WEIBusInformation2023(WEIBusInformation):
|
class WEIBusInformation2023(WEIBusInformation):
|
||||||
"""
|
"""
|
||||||
For each word, the bus has a score
|
For each question, the bus has ordered answers
|
||||||
"""
|
"""
|
||||||
scores: dict
|
scores: dict
|
||||||
|
|
||||||
def __init__(self, bus):
|
def __init__(self, bus):
|
||||||
self.scores = {}
|
self.scores = {}
|
||||||
for word in WORDS:
|
for question in WORDS:
|
||||||
self.scores[word] = 0.0
|
self.scores[question] = []
|
||||||
super().__init__(bus)
|
super().__init__(bus)
|
||||||
|
|
||||||
|
|
||||||
|
@ -95,11 +98,10 @@ class WEISurveyInformation2023(WEISurveyInformation):
|
||||||
"""
|
"""
|
||||||
# Random seed that is stored at the first time to ensure that words are generated only once
|
# Random seed that is stored at the first time to ensure that words are generated only once
|
||||||
seed = 0
|
seed = 0
|
||||||
step = 0
|
|
||||||
|
|
||||||
def __init__(self, registration):
|
def __init__(self, registration):
|
||||||
for i in range(1, 21):
|
for question in WORDS:
|
||||||
setattr(self, "word" + str(i), None)
|
setattr(self, str(question), None)
|
||||||
super().__init__(registration)
|
super().__init__(registration)
|
||||||
|
|
||||||
|
|
||||||
|
@ -127,9 +129,9 @@ class WEISurvey2023(WEISurvey):
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
word = form.cleaned_data["word"]
|
for question in WORDS:
|
||||||
self.information.step += 1
|
answer = form.cleaned_data[question]
|
||||||
setattr(self.information, "word" + str(self.information.step), word)
|
setattr(self.information, question, answer)
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -140,7 +142,10 @@ class WEISurvey2023(WEISurvey):
|
||||||
"""
|
"""
|
||||||
The survey is complete once the bus is chosen.
|
The survey is complete once the bus is chosen.
|
||||||
"""
|
"""
|
||||||
return self.information.step == 20
|
for question in WORDS:
|
||||||
|
if not getattr(self.information, question):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@lru_cache()
|
@lru_cache()
|
||||||
|
|
Loading…
Reference in New Issue