From e4998cb6e3a4735fd83f535fec64d04bf99e0bc5 Mon Sep 17 00:00:00 2001
From: Yohann D'ANELLO <yohann.danello@gmail.com>
Date: Fri, 7 Aug 2020 20:11:28 +0200
Subject: [PATCH] [WEI] Implement WEI Survey front

---
 apps/wei/forms/surveys/base.py    |  7 ++++
 apps/wei/forms/surveys/wei2020.py | 64 ++++++++++++++++++++++++-------
 apps/wei/views.py                 |  1 +
 locale/de/LC_MESSAGES/django.po   |  8 +++-
 locale/fr/LC_MESSAGES/django.po   | 18 ++++++---
 templates/base.html               |  1 +
 6 files changed, 78 insertions(+), 21 deletions(-)

diff --git a/apps/wei/forms/surveys/base.py b/apps/wei/forms/surveys/base.py
index f43dafc2..173d1758 100644
--- a/apps/wei/forms/surveys/base.py
+++ b/apps/wei/forms/surveys/base.py
@@ -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:
diff --git a/apps/wei/forms/surveys/wei2020.py b/apps/wei/forms/surveys/wei2020.py
index 4f60f6d4..34a5c587 100644
--- a/apps/wei/forms/surveys/wei2020.py
+++ b/apps/wei/forms/surveys/wei2020.py
@@ -1,27 +1,56 @@
 # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
 # SPDX-License-Identifier: GPL-3.0-or-later
 
+from random import choice
+
 from django import forms
+from django.utils.translation import gettext_lazy as _
 
-from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm
+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()
diff --git a/apps/wei/views.py b/apps/wei/views.py
index 3d96ed9f..b9edcbe9 100644
--- a/apps/wei/views.py
+++ b/apps/wei/views.py
@@ -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()
diff --git a/locale/de/LC_MESSAGES/django.po b/locale/de/LC_MESSAGES/django.po
index b3a2ca05..6296da52 100644
--- a/locale/de/LC_MESSAGES/django.po
+++ b/locale/de/LC_MESSAGES/django.po
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2020-08-07 14:17+0200\n"
+"POT-Creation-Date: 2020-08-07 19:53+0200\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -1484,6 +1484,10 @@ msgstr ""
 msgid "This team doesn't belong to the given bus."
 msgstr ""
 
+#: apps/wei/forms/surveys/wei2020.py:26
+msgid "Choose a word:"
+msgstr ""
+
 #: apps/wei/models.py:24 templates/wei/weiclub_info.html:23
 msgid "year"
 msgstr ""
@@ -1768,7 +1772,7 @@ msgstr ""
 msgid "This user didn't give her/his caution check."
 msgstr ""
 
-#: apps/wei/views.py:917 apps/wei/views.py:970 apps/wei/views.py:980
+#: apps/wei/views.py:917 apps/wei/views.py:971 apps/wei/views.py:981
 #: templates/wei/survey.html:12 templates/wei/survey_closed.html:12
 #: templates/wei/survey_end.html:12
 msgid "Survey WEI"
diff --git a/locale/fr/LC_MESSAGES/django.po b/locale/fr/LC_MESSAGES/django.po
index ddad77a5..6d75fe26 100644
--- a/locale/fr/LC_MESSAGES/django.po
+++ b/locale/fr/LC_MESSAGES/django.po
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2020-08-07 14:17+0200\n"
+"POT-Creation-Date: 2020-08-07 19:53+0200\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -547,8 +547,7 @@ msgstr "début de l'adhésion"
 #: apps/member/models.py:219
 msgid "Date from which the members can renew their membership."
 msgstr ""
-"Date à partir de laquelle les adhérents peuvent renouveler leur "
-"adhésion."
+"Date à partir de laquelle les adhérents peuvent renouveler leur adhésion."
 
 #: apps/member/models.py:225 templates/member/club_info.html:28
 msgid "membership end"
@@ -556,7 +555,9 @@ msgstr "fin de l'adhésion"
 
 #: apps/member/models.py:226
 msgid "Maximal date of a membership, after which members must renew it."
-msgstr "Date maximale d'une fin d'adhésion, après laquelle les adhérents doivent la renouveler."
+msgstr ""
+"Date maximale d'une fin d'adhésion, après laquelle les adhérents doivent la "
+"renouveler."
 
 #: apps/member/models.py:258 apps/member/models.py:283
 #: apps/note/models/notes.py:163
@@ -1518,6 +1519,10 @@ msgstr "Sélectionnez les rôles qui vous intéressent."
 msgid "This team doesn't belong to the given bus."
 msgstr "Cette équipe n'appartient pas à ce bus."
 
+#: apps/wei/forms/surveys/wei2020.py:26
+msgid "Choose a word:"
+msgstr "Choisissez un mot :"
+
 #: apps/wei/models.py:24 templates/wei/weiclub_info.html:23
 msgid "year"
 msgstr "année"
@@ -1814,7 +1819,7 @@ msgstr "Valider l'inscription WEI"
 msgid "This user didn't give her/his caution check."
 msgstr "Cet utilisateur n'a pas donné son chèque de caution."
 
-#: apps/wei/views.py:917 apps/wei/views.py:970 apps/wei/views.py:980
+#: apps/wei/views.py:917 apps/wei/views.py:971 apps/wei/views.py:981
 #: templates/wei/survey.html:12 templates/wei/survey_closed.html:12
 #: templates/wei/survey_end.html:12
 msgid "Survey WEI"
@@ -2490,7 +2495,8 @@ msgstr "Cette facture est verrouillée et ne peut pas être supprimée."
 msgid ""
 "Are you sure you want to delete this invoice? This action can't be undone."
 msgstr ""
-"Êtes-vous sûr de vouloir supprimer cette facture ? Cette action ne pourra pas être annulée."
+"Êtes-vous sûr de vouloir supprimer cette facture ? Cette action ne pourra "
+"pas être annulée."
 
 #: templates/treasury/invoice_confirm_delete.html:26
 msgid "Return to invoices list"
diff --git a/templates/base.html b/templates/base.html
index 8b45d914..e50defcb 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -231,6 +231,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
 
 <script>
     CSRF_TOKEN = "{{ csrf_token }}";
+    $(".invalid-feedback").addClass("d-block");
 </script>
 
 {% block extrajavascript %}
-- 
GitLab