From 28b9690cd6132278887db3f9c51b17940144ad87 Mon Sep 17 00:00:00 2001 From: Ben Bonfil Date: Mon, 4 Dec 2023 10:44:25 +0100 Subject: [PATCH] dynamically populate signup languages field with known languages (#129) --- lab/gateway/urls.py | 2 + lab/gateway/views.py | 7 ++++ .../migrations/0007_default_languages.py | 37 ++++++++++++++++++ lab/participants/serializers.py | 11 +++++- parent/mailauth/views.py | 13 +++---- parent/parent/forms.py | 13 +------ .../templates/widgets/languages_widget.html | 38 ++++++------------- parent/parent/views.py | 11 ++++++ 8 files changed, 84 insertions(+), 48 deletions(-) create mode 100644 lab/participants/migrations/0007_default_languages.py diff --git a/lab/gateway/urls.py b/lab/gateway/urls.py index 387ad8c1..9a887385 100644 --- a/lab/gateway/urls.py +++ b/lab/gateway/urls.py @@ -26,4 +26,6 @@ path("survey_invites/", views.SurveyInvitesView.as_view()), # path("deactivate/", views.DeactivateView.as_view()), + # + path("languages/", views.LanguagesView.as_view()), ] diff --git a/lab/gateway/views.py b/lab/gateway/views.py index ec8ca8d0..2d4a5435 100644 --- a/lab/gateway/views.py +++ b/lab/gateway/views.py @@ -5,6 +5,8 @@ from rest_framework.response import Response from experiments.serializers import AppointmentSerializer +from participants.models import Language +from participants.serializers import LanguageSerializer from signups.models import Signup from signups.serializers import SignupSerializer from survey_admin.models import SurveyDefinition, SurveyResponse @@ -109,3 +111,8 @@ class DeactivateView(views.APIView): def post(self, request, *args, **kwargs): self.request.participant.deactivate() return Response(dict()) + + +class LanguagesView(generics.ListAPIView): + serializer_class = LanguageSerializer + queryset = Language.objects.all() diff --git a/lab/participants/migrations/0007_default_languages.py b/lab/participants/migrations/0007_default_languages.py new file mode 100644 index 00000000..d2d6bbbd --- /dev/null +++ b/lab/participants/migrations/0007_default_languages.py @@ -0,0 +1,37 @@ +# Generated by Django 4.0.7 on 2023-11-24 09:42 + +from django.db import migrations + +languages = [ + "Engels", + "Duits", + "Spaans", + "Frans", + "Italiaans", + "Turks", + "Fries", + "Arabisch", + "Russisch", + "Chinees", + "Grieks", + "Portugees", + "Hongaars", +] + + +def create_default_languages(apps, schema_editor): + Language = apps.get_model("participants", "Language") + for lang in languages: + Language.objects.create(name=lang) + + +def undo(apps, schema_editor): + pass + + +class Migration(migrations.Migration): + dependencies = [ + ("participants", "0006_participant_english_contact_participant_save_longer"), + ] + + operations = [migrations.RunPython(create_default_languages, undo)] diff --git a/lab/participants/serializers.py b/lab/participants/serializers.py index f483ac9b..6096d50f 100644 --- a/lab/participants/serializers.py +++ b/lab/participants/serializers.py @@ -1,8 +1,15 @@ from rest_framework import serializers -from .models import Participant +from .models import Participant, Language + class ParticipantSerializer(serializers.ModelSerializer): class Meta: model = Participant - fields = ['id', 'name'] + fields = ["id", "name"] + + +class LanguageSerializer(serializers.ModelSerializer): + class Meta: + model = Language + fields = ["name"] diff --git a/parent/mailauth/views.py b/parent/mailauth/views.py index 7bfb5d31..72a4f351 100644 --- a/parent/mailauth/views.py +++ b/parent/mailauth/views.py @@ -1,13 +1,14 @@ from cdh.rest import client as rest from django.contrib import messages from django.http.response import HttpResponse -from django.shortcuts import render, redirect -from django.views.generic.edit import FormView +from django.shortcuts import redirect, render from django.urls import reverse_lazy +from django.views.generic.edit import FormView -from .forms import LoginForm from parent.utils import gateway +from .forms import LoginForm + def link_verify(request, token): ok, response = gateway(request, f"/gateway/mailauth/token/{token}/") @@ -24,7 +25,7 @@ def link_verify(request, token): # ask the user to choose a specific participant return list_pps(request, response["possible_pps"]) - redirect_to = request.GET.get('redirect', '/overview') + redirect_to = request.GET.get("redirect", "/overview") return redirect(redirect_to) @@ -35,9 +36,7 @@ def list_pps(request, possible_pps): def resolve_pp(request, participant_id): """tell the lab app which participant we should refer to for the rest of the session""" - ok, response = gateway( - request, f"/gateway/mailauth/set_participant/", data=dict(participant_id=int(participant_id)) - ) + ok, response = gateway(request, "/gateway/mailauth/set_participant/", data=dict(participant_id=int(participant_id))) return redirect("/overview") diff --git a/parent/parent/forms.py b/parent/parent/forms.py index 38284e82..f2597ae0 100644 --- a/parent/parent/forms.py +++ b/parent/parent/forms.py @@ -1,4 +1,3 @@ -import json from datetime import date from cdh.core.forms import ( @@ -21,11 +20,6 @@ def value_from_datadict(self, data, files, name): value = data.getlist(name) return value - def get_context(self, name, value, attrs): - context = super().get_context(name, value, attrs) - - return context - class LanguagesField(forms.MultipleChoiceField): def to_python(self, value): @@ -88,12 +82,7 @@ class SignupForm(TemplatedForm): # but it should still support prepopulating with known label=_("parent:forms:signup:languages"), widget=LanguagesWidget, - choices=( - ("Nederlands", "Nederlands"), - ("Engels", "Engels"), - ("Duits", "Duits"), - ("Spaans", "Spaans"), - ), + choices=[], ) parent_header = TemplatedFormTextField(header=_("parent:forms:signup:parent_header")) parent_first_name = forms.CharField(label=_("parent:forms:signup:parent_first_name")) diff --git a/parent/parent/templates/widgets/languages_widget.html b/parent/parent/templates/widgets/languages_widget.html index aa42ada4..aec0f480 100644 --- a/parent/parent/templates/widgets/languages_widget.html +++ b/parent/parent/templates/widgets/languages_widget.html @@ -17,19 +17,11 @@
@@ -42,20 +34,12 @@
diff --git a/parent/parent/views.py b/parent/parent/views.py index 6dbfd2ac..9df7ba6d 100644 --- a/parent/parent/views.py +++ b/parent/parent/views.py @@ -58,6 +58,17 @@ def form_valid(self, form): log.error("Couldn't reach server while processing signup") return super().get(self.request) + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + ok, languages = gateway(self.request, "/gateway/languages/") + + choices = [] + for lang in languages: + choices.append((lang["name"], lang["name"])) + + context["form"].fields["languages"].choices = choices + return context + class SignupDone(TemplateView): template_name = "signup_done.html"