Skip to content

Commit

Permalink
dynamically populate signup languages field with known languages (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbonf authored Dec 4, 2023
1 parent 9aeb4ff commit 28b9690
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 48 deletions.
2 changes: 2 additions & 0 deletions lab/gateway/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@
path("survey_invites/", views.SurveyInvitesView.as_view()),
#
path("deactivate/", views.DeactivateView.as_view()),
#
path("languages/", views.LanguagesView.as_view()),
]
7 changes: 7 additions & 0 deletions lab/gateway/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
37 changes: 37 additions & 0 deletions lab/participants/migrations/0007_default_languages.py
Original file line number Diff line number Diff line change
@@ -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)]
11 changes: 9 additions & 2 deletions lab/participants/serializers.py
Original file line number Diff line number Diff line change
@@ -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"]
13 changes: 6 additions & 7 deletions parent/mailauth/views.py
Original file line number Diff line number Diff line change
@@ -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}/")
Expand All @@ -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)


Expand All @@ -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")


Expand Down
13 changes: 1 addition & 12 deletions parent/parent/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from datetime import date

from cdh.core.forms import (
Expand All @@ -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):
Expand Down Expand Up @@ -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"))
Expand Down
38 changes: 11 additions & 27 deletions parent/parent/templates/widgets/languages_widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,11 @@
<div class="uu-form-field">
<select class="w-100" id="id_languages_mono_select" name="languages" disabled>
<option></option>
<option>Engels</option>
<option>Duits</option>
<option>Spaans</option>
<option>Frans</option>
<option>Italiaans</option>
<option>Turks</option>
<option>Fries</option>
<option>Arabisch</option>
<option>Russisch</option>
<option>Chinees</option>
<option>Grieks</option>
<option>Portugees</option>
<option>Hongaars</option>
{% for group_name, group_choices, group_index in widget.optgroups %}
{% for option in group_choices %}
<option value="{{ option.value }}">{{ option.label }}</option>
{% endfor %}
{% endfor %}
</select>
</div>

Expand All @@ -42,20 +34,12 @@

<div class="uu-form-field">
<select class="w-100" id="id_languages_multi_select" name="languages" multiple="true" disabled>
<option>Nederlands</option>
<option>Engels</option>
<option>Duits</option>
<option>Spaans</option>
<option>Frans</option>
<option>Italiaans</option>
<option>Turks</option>
<option>Fries</option>
<option>Arabisch</option>
<option>Russisch</option>
<option>Chinees</option>
<option>Grieks</option>
<option>Portugees</option>
<option>Hongaars</option>
<option></option>
{% for group_name, group_choices, group_index in widget.optgroups %}
{% for option in group_choices %}
<option value="{{ option.value }}">{{ option.label }}</option>
{% endfor %}
{% endfor %}
</select>
</div>

Expand Down
11 changes: 11 additions & 0 deletions parent/parent/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 28b9690

Please sign in to comment.