nk20/apps/wei/forms/surveys/wei2020.py

130 lines
3.8 KiB
Python
Raw Normal View History

# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
2020-04-19 18:35:49 +00:00
# SPDX-License-Identifier: GPL-3.0-or-later
2020-08-07 18:11:28 +00:00
from random import choice
2020-04-19 18:35:49 +00:00
from django import forms
2020-09-11 20:52:16 +00:00
from django.db import transaction
2020-08-07 18:11:28 +00:00
from django.utils.translation import gettext_lazy as _
2020-04-19 18:35:49 +00:00
2020-08-07 18:11:28 +00:00
from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm, WEIBusInformation
2020-04-19 18:35:49 +00:00
from ...models import Bus
2020-08-07 18:11:28 +00:00
# TODO: Use new words
WORDS = ['Rap', 'Retro', 'DJ', 'Rock', 'Jazz', 'Chansons Populaires', 'Chansons Paillardes', 'Pop', 'Fanfare',
'Biere', 'Pastis', 'Vodka', 'Cocktails', 'Eau', 'Sirop', 'Jus de fruit', 'Binge Drinking', 'Rhum',
'Eau de vie', 'Apéro', 'Morning beer', 'Huit-six', 'Jeux de societé', 'Jeux de cartes', 'Danse', 'Karaoké',
'Bière Pong', 'Poker', 'Loup Garou', 'Films', "Jeux d'alcool", 'Sport', 'Rangées de cul', 'Chips', 'BBQ',
'Kebab', 'Saucisse', 'Vegan', 'Vege', 'LGBTIQ+', 'Dab', 'Solitaire', 'Séducteur', 'Sociale', 'Chanteur',
'Se lacher', 'Chill', 'Débile', 'Beauf', 'Bon enfant']
2020-04-19 18:35:49 +00:00
class WEISurveyForm2020(forms.Form):
2020-04-19 23:26:53 +00:00
"""
Survey form for the year 2020.
2020-08-07 18:11:28 +00:00
Members choose 20 words, from which we calculate the best associated bus.
2020-04-19 23:26:53 +00:00
"""
2020-08-07 18:11:28 +00:00
word = forms.ChoiceField(
label=_("Choose a word:"),
widget=forms.RadioSelect(),
2020-04-19 18:35:49 +00:00
)
def set_registration(self, registration):
2020-04-19 23:26:53 +00:00
"""
Filter the bus selector with the buses of the current WEI.
"""
2020-08-07 18:11:28 +00:00
words = [choice(WORDS) for _ in range(10)]
words = [(w, w) for w in words]
if self.data:
2020-08-10 21:18:40 +00:00
self.fields["word"].choices = [(w, w) for w in WORDS]
2020-08-07 18:11:28 +00:00
if self.is_valid():
return
self.fields["word"].choices = words
class WEIBusInformation2020(WEIBusInformation):
"""
For each word, the bus has a score
"""
def __init__(self, bus):
for word in WORDS:
setattr(self, word, 0.0)
super().__init__(bus)
2020-04-19 18:35:49 +00:00
class WEISurveyInformation2020(WEISurveyInformation):
2020-04-19 23:26:53 +00:00
"""
We store the id of the selected bus. We store only the name, but is not used in the selection:
that's only for humans that try to read data.
"""
2020-08-07 18:11:28 +00:00
step = 0
def __init__(self, registration):
for i in range(1, 21):
setattr(self, "word" + str(i), None)
super().__init__(registration)
2020-04-19 18:35:49 +00:00
class WEISurvey2020(WEISurvey):
2020-04-19 23:26:53 +00:00
"""
Survey for the year 2020.
"""
2020-08-07 18:11:28 +00:00
2020-04-19 23:26:53 +00:00
@classmethod
def get_year(cls):
return 2020
@classmethod
def get_survey_information_class(cls):
2020-04-19 18:35:49 +00:00
return WEISurveyInformation2020
def get_form_class(self):
return WEISurveyForm2020
def update_form(self, form):
2020-04-19 23:26:53 +00:00
"""
Filter the bus selector with the buses of the WEI.
"""
2020-04-19 18:35:49 +00:00
form.set_registration(self.registration)
2020-09-11 20:52:16 +00:00
@transaction.atomic
2020-04-19 18:35:49 +00:00
def form_valid(self, form):
2020-08-07 18:11:28 +00:00
word = form.cleaned_data["word"]
self.information.step += 1
setattr(self.information, "word" + str(self.information.step), word)
2020-04-19 18:35:49 +00:00
self.save()
2020-04-19 23:26:53 +00:00
@classmethod
def get_algorithm_class(cls):
2020-04-19 18:35:49 +00:00
return WEISurveyAlgorithm2020
2020-04-19 23:26:53 +00:00
def is_complete(self) -> bool:
"""
The survey is complete once the bus is chosen.
"""
2020-08-07 18:11:28 +00:00
return self.information.step == 20
2020-04-19 23:26:53 +00:00
2020-04-19 18:35:49 +00:00
class WEISurveyAlgorithm2020(WEISurveyAlgorithm):
2020-04-19 23:26:53 +00:00
"""
The algorithm class for the year 2020.
For now, the algorithm is quite simple: the selected bus is the chosen bus.
TODO: Improve this algorithm.
"""
@classmethod
def get_survey_class(cls):
2020-04-19 18:35:49 +00:00
return WEISurvey2020
2020-08-07 18:11:28 +00:00
@classmethod
def get_bus_information_class(cls):
return WEIBusInformation2020
2020-04-19 18:35:49 +00:00
def run_algorithm(self):
for registration in self.get_registrations():
survey = self.get_survey_class()(registration)
2020-08-07 18:11:28 +00:00
survey.select_bus(choice(Bus.objects.all()))
2020-04-19 18:35:49 +00:00
survey.save()