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

WEI Survey (work in progress)

This commit is contained in:
Yohann D'ANELLO
2020-04-19 20:35:49 +02:00
parent b62fa4cc6d
commit 8113c5cd61
19 changed files with 255 additions and 18 deletions

View File

@ -184,7 +184,7 @@ class InvoiceRenderView(LoginRequiredMixin, View):
except IOError as e:
raise e
finally:
# Delete all temporary files
# Delete all temporary forms
shutil.rmtree(tmp_dir)
return response

View File

@ -0,0 +1,10 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from .registration import *
from .surveys import *
__all__ = [
'WEIForm', 'WEIRegistrationForm', 'WEIMembershipForm', 'BusForm', 'BusTeamForm',
'WEISurvey', 'WEISurveyInformation', 'WEISurveyAlgorithm', 'CurrentSurvey',
]

View File

@ -5,7 +5,7 @@ from django import forms
from django.contrib.auth.models import User
from note_kfet.inputs import AmountInput, DatePickerInput, Autocomplete, ColorWidget
from .models import WEIClub, WEIRegistration, Bus, BusTeam, WEIMembership, WEIRole
from wei.models import WEIClub, WEIRegistration, Bus, BusTeam, WEIMembership, WEIRole
class WEIForm(forms.ModelForm):

View File

@ -0,0 +1,12 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm
from .wei2020 import WEISurvey2020
__all__ = [
'WEISurvey', 'WEISurveyInformation', 'WEISurveyAlgorithm', 'CurrentSurvey',
]
CurrentSurvey = WEISurvey2020

View File

@ -0,0 +1,61 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from wei.models import WEIClub, WEIRegistration
class WEISurvey:
year = None
step = 0
def __init__(self, registration):
self.registration = registration
self.information = self.get_survey_information_class()(registration)
def get_wei(self):
return WEIClub.objects.get(year=self.year)
def get_survey_information_class(self):
raise NotImplementedError
def get_form_class(self):
raise NotImplementedError
def update_form(self, form):
pass
@staticmethod
def get_algorithm_class():
raise NotImplementedError
def form_valid(self, form):
raise NotImplementedError
def save(self):
self.information.save(self.registration)
def select_bus(self, bus_pk):
self.information.selected_bus_pk = bus_pk
class WEISurveyInformation:
valid = False
selected_bus_pk = None
def __init__(self, registration):
self.__dict__.update(registration.information)
def save(self, registration):
registration.information = self.__dict__
registration.save()
class WEISurveyAlgorithm:
def get_survey_class(self):
raise NotImplementedError
def get_registrations(self):
return WEIRegistration.objects.filter(wei__year=self.get_survey_class().year, first_year=True).all()
def run_algorithm(self):
raise NotImplementedError

View File

@ -0,0 +1,52 @@
# 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):
bus = forms.ModelChoiceField(
Bus.objects,
)
def set_registration(self, registration):
self.fields["bus"].queryset = Bus.objects.filter(wei=registration.wei)
class WEISurveyInformation2020(WEISurveyInformation):
chosen_bus_pk = None
class WEISurvey2020(WEISurvey):
year = 2020
def get_survey_information_class(self):
return WEISurveyInformation2020
def get_form_class(self):
return WEISurveyForm2020
def update_form(self, form):
form.set_registration(self.registration)
def form_valid(self, form):
self.information.chosen_bus_pk = form.cleaned_data["bus"].pk
self.save()
@staticmethod
def get_algorithm_class():
return WEISurveyAlgorithm2020
class WEISurveyAlgorithm2020(WEISurveyAlgorithm):
def get_survey_class(self):
return WEISurvey2020
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.save()

View File

View File

@ -0,0 +1,2 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later

View File

@ -0,0 +1,2 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later

View File

@ -0,0 +1,14 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django.core.management import BaseCommand
from django.utils.translation import gettext_lazy as _
from wei.forms import CurrentSurvey
class Command(BaseCommand):
help = _("Attribute to each first year member a bus for the WEI")
def handle(self, *args, **options):
CurrentSurvey.get_algorithm_class()().run_algorithm()

View File

@ -64,6 +64,27 @@ class Bus(models.Model):
verbose_name=_("description"),
)
information_json = models.TextField(
default="{}",
verbose_name=_("survey information"),
help_text=_("Information about the survey for new members, encoded in JSON"),
)
@property
def information(self):
"""
The information about the survey for new members are stored in a dictionary that can evolve following the years.
The dictionary is stored as a JSON string.
"""
return json.loads(self.information_json)
@information.setter
def information(self, information):
"""
Store information as a JSON string
"""
self.information_json = json.dumps(information)
def __str__(self):
return self.name

View File

@ -5,7 +5,7 @@ from django.urls import path
from .views import CurrentWEIDetailView, WEIListView, WEICreateView, WEIDetailView, WEIUpdateView,\
BusCreateView, BusManageView, BusUpdateView, BusTeamCreateView, BusTeamManageView, BusTeamUpdateView,\
WEIRegister1AView, WEIRegister2AView, WEIUpdateRegistrationView, WEIValidateRegistrationView
WEIRegister1AView, WEIRegister2AView, WEIUpdateRegistrationView, WEIValidateRegistrationView, WEISurveyView
app_name = 'wei'
@ -27,4 +27,5 @@ urlpatterns = [
path('register/<int:wei_pk>/2A+/myself/', WEIRegister2AView.as_view(), name="wei_register_2A_myself"),
path('edit-registration/<int:pk>/', WEIUpdateRegistrationView.as_view(), name="wei_update_registration"),
path('validate/<int:pk>/', WEIValidateRegistrationView.as_view(), name="validate_registration"),
path('survey/<int:pk>/', WEISurveyView.as_view(), name="wei_survey"),
]

View File

@ -9,6 +9,7 @@ from django.db.models import Q
from django.urls import reverse_lazy
from django.views.generic import DetailView, UpdateView, CreateView, RedirectView
from django.utils.translation import gettext_lazy as _
from django.views.generic.edit import BaseFormView
from django_tables2 import SingleTableView
from member.models import Membership, Club
from note.models import Transaction, NoteClub
@ -17,7 +18,7 @@ from permission.backends import PermissionBackend
from permission.views import ProtectQuerysetMixin
from .models import WEIClub, WEIRegistration, WEIMembership, Bus, BusTeam, WEIRole
from .forms import WEIForm, WEIRegistrationForm, BusForm, BusTeamForm, WEIMembershipForm
from .forms import WEIForm, WEIRegistrationForm, BusForm, BusTeamForm, WEIMembershipForm, CurrentSurvey
from .tables import WEITable, WEIRegistrationTable, BusTable, BusTeamTable, WEIMembershipTable
@ -302,8 +303,7 @@ class WEIRegister1AView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
def get_success_url(self):
self.object.refresh_from_db()
# TODO Replace it with the link of the survey
return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.wei.pk})
return reverse_lazy("wei:wei_survey", kwargs={"pk": self.object.pk})
class WEIRegister2AView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
@ -342,7 +342,7 @@ class WEIRegister2AView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
def get_success_url(self):
self.object.refresh_from_db()
return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.wei.pk})
return reverse_lazy("wei:wei_survey", kwargs={"pk": self.object.pk})
class WEIUpdateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
@ -447,3 +447,36 @@ class WEIValidateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, Crea
def get_success_url(self):
self.object.refresh_from_db()
return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.club.pk})
class WEISurveyView(BaseFormView, DetailView):
model = WEIRegistration
template_name = "wei/survey.html"
survey = None
def setup(self, request, *args, **kwargs):
ret = super().setup(request, *args, **kwargs)
return ret
def get_form_class(self):
if not self.survey:
self.survey = CurrentSurvey(self.get_object())
return self.survey.get_form_class()
def get_form(self, form_class=None):
form = super().get_form(form_class)
self.survey.update_form(form)
return form
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["club"] = self.object.wei
context["title"] = _("Survey WEI")
return context
def form_valid(self, form):
self.survey.form_valid(form)
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy('wei:wei_survey', args=(self.get_object().pk,))