1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-06-20 17:41:55 +02:00

[WEI] Implement WEI Survey front

This commit is contained in:
Yohann D'ANELLO
2020-08-07 20:11:28 +02:00
parent ad59b5c81e
commit e4998cb6e3
6 changed files with 79 additions and 22 deletions

View File

@ -44,6 +44,13 @@ class WEIBusInformation:
def __init__(self, bus: Bus):
self.__dict__.update(bus.information)
self.bus = bus
self.save()
def save(self):
d = self.__dict__.copy()
d.pop("bus")
self.bus.information = d
self.bus.save()
class WEISurveyAlgorithm:

View File

@ -1,27 +1,56 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django import forms
from random import choice
from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm
from django import forms
from django.utils.translation import gettext_lazy as _
from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm, WEIBusInformation
from ...models import Bus
# 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']
class WEISurveyForm2020(forms.Form):
"""
Survey form for the year 2020.
For now, that's only a Bus selector.
TODO: Do a better survey (later)
Members choose 20 words, from which we calculate the best associated bus.
"""
bus = forms.ModelChoiceField(
Bus.objects,
word = forms.ChoiceField(
label=_("Choose a word:"),
widget=forms.RadioSelect(),
)
def set_registration(self, registration):
"""
Filter the bus selector with the buses of the current WEI.
"""
self.fields["bus"].queryset = Bus.objects.filter(wei=registration.wei)
words = [choice(WORDS) for _ in range(10)]
words = [(w, w) for w in words]
if self.data:
self.fields["word"].choices = WORDS
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)
class WEISurveyInformation2020(WEISurveyInformation):
@ -29,14 +58,19 @@ class WEISurveyInformation2020(WEISurveyInformation):
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.
"""
chosen_bus_pk = None
chosen_bus_name = None
step = 0
def __init__(self, registration):
for i in range(1, 21):
setattr(self, "word" + str(i), None)
super().__init__(registration)
class WEISurvey2020(WEISurvey):
"""
Survey for the year 2020.
"""
@classmethod
def get_year(cls):
return 2020
@ -55,9 +89,9 @@ class WEISurvey2020(WEISurvey):
form.set_registration(self.registration)
def form_valid(self, form):
bus = form.cleaned_data["bus"]
self.information.chosen_bus_pk = bus.pk
self.information.chosen_bus_name = bus.name
word = form.cleaned_data["word"]
self.information.step += 1
setattr(self.information, "word" + str(self.information.step), word)
self.save()
@classmethod
@ -68,7 +102,7 @@ class WEISurvey2020(WEISurvey):
"""
The survey is complete once the bus is chosen.
"""
return self.information.chosen_bus_pk is not None
return self.information.step == 20
class WEISurveyAlgorithm2020(WEISurveyAlgorithm):
@ -82,8 +116,12 @@ class WEISurveyAlgorithm2020(WEISurveyAlgorithm):
def get_survey_class(cls):
return WEISurvey2020
@classmethod
def get_bus_information_class(cls):
return WEIBusInformation2020
def run_algorithm(self):
for registration in self.get_registrations():
survey = self.get_survey_class()(registration)
survey.select_bus(Bus.objects.get(pk=survey.information.chosen_bus_pk))
survey.select_bus(choice(Bus.objects.all()))
survey.save()

View File

@ -918,6 +918,7 @@ class WEISurveyView(LoginRequiredMixin, BaseFormView, DetailView):
def dispatch(self, request, *args, **kwargs):
obj = self.get_object()
self.object = obj
wei = obj.wei
today = date.today()