base for questions instead of words

This commit is contained in:
bleizi 2023-08-26 17:52:48 +02:00
parent ba0d64f0d4
commit 9596aa7b8c
No known key found for this signature in database
GPG Key ID: D46D7E3364433208
1 changed files with 54 additions and 49 deletions

View File

@ -13,31 +13,43 @@ from django.utils.translation import gettext_lazy as _
from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm, WEIBusInformation
from ...models import WEIMembership
WORDS = [
'ABBA', 'After', 'Alcoolique anonyme', 'Ambiance festive', 'Années 2000', 'Apéro', 'Art',
'Baby foot billard biere pong', 'BBQ', 'Before', 'Bière pong', 'Bon enfant', 'Calme', 'Canapé',
'Chanson paillarde', 'Chanson populaire', 'Chartreuse', 'Cheerleader', 'Chill', 'Choré',
'Cinéma', 'Cocktail', 'Comédie musicle', 'Commercial', 'Copaing', 'Danse', 'Dancefloor',
'Electro', 'Fanfare', 'Gin tonic', 'Inclusif', 'Jazz', "Jeux d'alcool", 'Jeux de carte',
'Jeux de rôle', 'Jeux de société', 'JUL', 'Jus de fruit', 'Kfet', 'Kleptomanie assurée',
'LGBTQ+', 'Livre', 'Morning beer', 'Musique', 'NAPS', 'Paillettes', 'Pastis', 'Paté Hénaff',
'Peluche', 'Pena baiona', "Peu d'alcool", 'Pilier de bar', 'PMU', 'Poulpe', 'Punch', 'Rap',
'Réveil', 'Rock', 'Rugby', 'Sandwich', 'Serge', 'Shot', 'Sociable', 'Spectacle', 'Techno',
'Techno house', 'Thérapie Taxi', 'Tradition kchanaises', 'Troisième mi-temps', 'Turn up',
'Vodka', 'Vodka pomme', 'Volley', 'Vomi stratégique'
]
WORDS = {
"ambiance":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
"musique":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
"boisson":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
"beauferie":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
"sommeil":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
"vacances":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
"activite":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
"hygiene":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
"animal":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
"fensfoire":["Question", {1:"réponse 1", 2:"réponse 2", 3:"réponse 3", 4:"réponse 4", 5:"réponse 5"}],
"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):
"""
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(
label=_("Choose a word:"),
widget=forms.RadioSelect(),
)
def __init__(self,**kwargs):
super().__init__(**kwargs)
for question in WORDS:
self.fields[question] = forms.ChoiceField(
label=WORDS[question][0]+question,
widget=forms.RadioSelect(),
)
def set_registration(self, registration):
"""
@ -50,41 +62,32 @@ class WEISurveyForm2023(forms.Form):
registration._force_save = True
registration.save()
if self.data:
self.fields["word"].choices = [(w, w) for w in WORDS]
if self.is_valid():
return
# if self.data:
# for question in WORDS:
# self.fields[question].choices = [answer for answer in WORDS[question][1]]
# 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
buses = WEISurveyAlgorithm2023.get_buses()
informations = {bus: WEIBusInformation2023(bus) for bus in buses}
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
# for question in questions:
for question in WORDS:
answers = [(answer, WORDS[question][1][answer]) for answer in WORDS[question][1]]
self.fields[question].choices = answers
class WEIBusInformation2023(WEIBusInformation):
"""
For each word, the bus has a score
For each question, the bus has ordered answers
"""
scores: dict
def __init__(self, bus):
self.scores = {}
for word in WORDS:
self.scores[word] = 0.0
for question in WORDS:
self.scores[question] = []
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
seed = 0
step = 0
def __init__(self, registration):
for i in range(1, 21):
setattr(self, "word" + str(i), None)
for question in WORDS:
setattr(self, str(question), None)
super().__init__(registration)
@ -127,9 +129,9 @@ class WEISurvey2023(WEISurvey):
@transaction.atomic
def form_valid(self, form):
word = form.cleaned_data["word"]
self.information.step += 1
setattr(self.information, "word" + str(self.information.step), word)
for question in WORDS:
answer = form.cleaned_data[question]
setattr(self.information, question, answer)
self.save()
@classmethod
@ -140,7 +142,10 @@ class WEISurvey2023(WEISurvey):
"""
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
@lru_cache()