1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-12-25 00:42:22 +00:00
nk20/apps/wei/forms/surveys/wei2020.py

90 lines
2.4 KiB
Python
Raw Normal View History

2020-04-19 18:35:49 +00:00
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django import forms
from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm
from ...models import Bus
class WEISurveyForm2020(forms.Form):
2020-04-19 23:26:53 +00:00
"""
Survey form for the year 2020.
For now, that's only a Bus selector.
TODO: Do a better survey (later)
"""
2020-04-19 18:35:49 +00:00
bus = forms.ModelChoiceField(
Bus.objects,
)
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-04-19 18:35:49 +00:00
self.fields["bus"].queryset = Bus.objects.filter(wei=registration.wei)
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-04-19 18:35:49 +00:00
chosen_bus_pk = None
chosen_bus_name = None
2020-04-19 18:35:49 +00:00
class WEISurvey2020(WEISurvey):
2020-04-19 23:26:53 +00:00
"""
Survey for the year 2020.
"""
@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)
def form_valid(self, form):
bus = form.cleaned_data["bus"]
self.information.chosen_bus_pk = bus.pk
self.information.chosen_bus_name = bus.name
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.
"""
return self.information.chosen_bus_pk is not None
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
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))
2020-04-19 18:35:49 +00:00
survey.save()