From 473d3c35465c7192c8f5a8abcf102bf7de5ebb3f Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sun, 19 Apr 2020 22:16:57 +0200 Subject: [PATCH] Integrate survey results into validation form --- apps/wei/forms/surveys/base.py | 14 +++++++++++--- apps/wei/forms/surveys/wei2020.py | 7 +++++-- apps/wei/views.py | 9 ++++++++- templates/wei/weimembership_form.html | 22 ++++++++++++++++++++-- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/apps/wei/forms/surveys/base.py b/apps/wei/forms/surveys/base.py index 8d9b68b4..0bb6d344 100644 --- a/apps/wei/forms/surveys/base.py +++ b/apps/wei/forms/surveys/base.py @@ -1,7 +1,7 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from wei.models import WEIClub, WEIRegistration +from ...models import WEIClub, WEIRegistration, Bus class WEISurvey: @@ -34,17 +34,25 @@ class WEISurvey: def save(self): self.information.save(self.registration) - def select_bus(self, bus_pk): - self.information.selected_bus_pk = bus_pk + def select_bus(self, bus): + self.information.selected_bus_pk = bus.pk + self.information.selected_bus_name = bus.name + self.information.valid = True class WEISurveyInformation: valid = False selected_bus_pk = None + selected_bus_name = None def __init__(self, registration): self.__dict__.update(registration.information) + def get_selected_bus(self): + if not self.valid: + return None + return Bus.objects.get(pk=self.selected_bus_pk) + def save(self, registration): registration.information = self.__dict__ registration.save() diff --git a/apps/wei/forms/surveys/wei2020.py b/apps/wei/forms/surveys/wei2020.py index a59a673e..489afc5b 100644 --- a/apps/wei/forms/surveys/wei2020.py +++ b/apps/wei/forms/surveys/wei2020.py @@ -18,6 +18,7 @@ class WEISurveyForm2020(forms.Form): class WEISurveyInformation2020(WEISurveyInformation): chosen_bus_pk = None + chosen_bus_name = None class WEISurvey2020(WEISurvey): @@ -33,7 +34,9 @@ class WEISurvey2020(WEISurvey): form.set_registration(self.registration) def form_valid(self, form): - self.information.chosen_bus_pk = form.cleaned_data["bus"].pk + bus = form.cleaned_data["bus"] + self.information.chosen_bus_pk = bus.pk + self.information.chosen_bus_name = bus.name self.save() @staticmethod @@ -48,5 +51,5 @@ class WEISurveyAlgorithm2020(WEISurveyAlgorithm): def run_algorithm(self): for registration in self.get_registrations(): survey = self.get_survey_class()(registration) - survey.select_bus(survey.information.chosen_bus_pk) + survey.select_bus(Bus.objects.get(pk=survey.information.chosen_bus_pk)) survey.save() diff --git a/apps/wei/views.py b/apps/wei/views.py index 09226a61..cf89f8de 100644 --- a/apps/wei/views.py +++ b/apps/wei/views.py @@ -380,6 +380,9 @@ class WEIValidateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, Crea registration = WEIRegistration.objects.get(pk=self.kwargs["pk"]) context["registration"] = registration + survey = CurrentSurvey(registration) + if survey.information.valid: + context["suggested_bus"] = survey.information.get_selected_bus() context["club"] = registration.wei context["fee"] = registration.wei.membership_fee_paid if registration.user.profile.paid \ else registration.wei.membership_fee_unpaid @@ -394,8 +397,12 @@ class WEIValidateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, Crea def get_form(self, form_class=None): form = super().get_form(form_class) - if WEIRegistration.objects.get(pk=self.kwargs["pk"]).first_year: + registration = WEIRegistration.objects.get(pk=self.kwargs["pk"]) + if registration.first_year: del form.fields["roles"] + survey = CurrentSurvey(registration) + if survey.information.valid: + form.fields["bus"].initial = survey.information.get_selected_bus() return form def form_valid(self, form): diff --git a/templates/wei/weimembership_form.html b/templates/wei/weimembership_form.html index df6c25ec..75ebad0e 100644 --- a/templates/wei/weimembership_form.html +++ b/templates/wei/weimembership_form.html @@ -10,7 +10,7 @@ {% block profile_content %}
-
+

{% trans "Review registration" %}

@@ -77,7 +77,25 @@
{% trans 'Payment from Société générale' %}
{{ registration.soge_credit|yesno }}
- {% if not registration.first_year %} + {% if registration.first_year %} +
{% trans 'Suggested bus from the survey:' %}
+ {% if registration.information.valid or True %} +
{{ suggested_bus }}
+ +
+
{% trans 'Raw survey information' %}
+
+ + {% with information=registration.information %} + {% for key, value in information.items %} +
{{ key }}
+
{{ value }}
+ {% endfor %} + {% endwith %} + {% else %} +
{% trans "The algorithm didn't run." %}
+ {% endif %} + {% else %}
{% trans 'caution check given'|capfirst %}
{{ registration.caution_check|yesno }}
{% endif %}