From 925cea024b3584cd2bbfbf24f6984e0bba4c170f Mon Sep 17 00:00:00 2001 From: Rhodri Cusack Date: Wed, 17 Jan 2024 07:56:16 +0000 Subject: [PATCH] add filter-by-language to study list --- accounts/forms.py | 6 ++++++ studies/forms.py | 2 ++ studies/migrations/0097_study_language.py | 18 ++++++++++++++++++ studies/models.py | 2 ++ studies/templates/studies/_study_fields.html | 1 + web/templates/web/studies-list.html | 5 +++-- web/views.py | 11 ++++++++++- 7 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 studies/migrations/0097_study_language.py diff --git a/accounts/forms.py b/accounts/forms.py index 840c362c7..ba8720045 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -471,6 +471,11 @@ class StudyLocation(FormChoiceEnum): lookit = ("1", _("here on the Lookit platform")) external = ("2", _("on other websites")) + class StudyLanguage(FormChoiceEnum): + all = ("*", _("All languages")) + english = ("en", _("English")) + japanese = ("ja", _("Japanese")) + child = forms.ChoiceField(choices=Children.choices(), required=False) search = forms.CharField(required=False) hide_studies_we_have_done = forms.BooleanField( @@ -483,6 +488,7 @@ class StudyLocation(FormChoiceEnum): required=False, ) study_location = forms.ChoiceField(choices=StudyLocation.choices(), required=False) + study_language = forms.ChoiceField(choices=StudyLanguage.choices(), required=False) def __init__(self, *args, **kwargs): self.user = kwargs.pop("user", None) diff --git a/studies/forms.py b/studies/forms.py index 3bd8dc4bc..856651755 100644 --- a/studies/forms.py +++ b/studies/forms.py @@ -231,6 +231,7 @@ class Meta: "must_have_participated", "must_not_have_participated", "study_type", + "language", ] labels = { "name": "Study Name", @@ -246,6 +247,7 @@ class Meta: "study_type": "Experiment Type", "compensation_description": "Compensation", "priority": "Lab Page Priority", + "language": "Language Used For Study", } widgets = { "preview_summary": Textarea(attrs={"rows": 2}), diff --git a/studies/migrations/0097_study_language.py b/studies/migrations/0097_study_language.py new file mode 100644 index 000000000..0fc001bca --- /dev/null +++ b/studies/migrations/0097_study_language.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.11 on 2024-01-16 16:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('studies', '0096_response_survey_consent'), + ] + + operations = [ + migrations.AddField( + model_name='study', + name='language', + field=models.TextField(choices=[('en', 'English'), ('am', 'Amharic'), ('bn', 'Bengali'), ('bho', 'Bhojpuri'), ('my', 'Burmese'), ('ceb', 'Cebuano'), ('hne', 'Chhattisgarhi'), ('nl', 'Dutch'), ('egy', 'Egyptian Spoken Arabic'), ('fr', 'French'), ('gan', 'Gan'), ('de', 'German'), ('gu', 'Gujarati'), ('hak', 'Hakka'), ('ha', 'Hausa'), ('he', 'Hebrew'), ('hi', 'Hindi'), ('ig', 'Igbo'), ('id', 'Indonesian'), ('pes', 'Iranian Persian'), ('it', 'Italian'), ('ja', 'Japanese'), ('jv', 'Javanese'), ('cjy', 'Jinyu'), ('kn', 'Kannada'), ('km', 'Khmer'), ('ko', 'Korean'), ('mag', 'Magahi'), ('mai', 'Maithili'), ('ms', 'Malay'), ('ml', 'Malayalam'), ('cmn', 'Chinese (Mandarin)'), ('mr', 'Marathi'), ('nan', 'Min Nan'), ('mor', 'Moroccan Spoken Arabic'), ('pbu', 'Northern Pashto'), ('uzn', 'Northern Uzbek'), ('or', 'Odia'), ('pl', 'Polish'), ('pt', 'Portuguese'), ('ro', 'Romanian'), ('ru', 'Russian'), ('skr', 'Saraiki'), ('sd', 'Sindhi'), ('so', 'Somali'), ('es', 'Spanish'), ('su', 'Sunda'), ('tl', 'Tagalog'), ('ta', 'Tamil'), ('te', 'Telugu'), ('th', 'Thai'), ('tr', 'Turkish'), ('uk', 'Ukrainian'), ('ur', 'Urdu'), ('vi', 'Vietnamese'), ('lah', 'Western Punjabi'), ('wuu', 'Wu'), ('hsn', 'Xiang Chinese'), ('yo', 'Yoruba'), ('yue', 'Chinese (Cantonese)')], default='en'), + ), + ] diff --git a/studies/models.py b/studies/models.py index 41f15b55f..47229e022 100644 --- a/studies/models.py +++ b/studies/models.py @@ -43,6 +43,7 @@ create_groups_for_instance, ) from studies.tasks import delete_video_from_cloud +from studies.fields import LANGUAGES logger = logging.getLogger(__name__) date_parser = dateutil.parser @@ -366,6 +367,7 @@ class Study(models.Model): must_not_have_participated = models.ManyToManyField( "self", blank=True, symmetrical=False, related_name="expected_nonparticipation" ) + language = models.TextField(default="en", choices=LANGUAGES) # Groups # The related_name convention seems silly, but django complains about reverse diff --git a/studies/templates/studies/_study_fields.html b/studies/templates/studies/_study_fields.html index c8c79e354..18b81de8a 100644 --- a/studies/templates/studies/_study_fields.html +++ b/studies/templates/studies/_study_fields.html @@ -158,3 +158,4 @@ {% bootstrap_field form.criteria_expression label_class="form-label fw-bold" wrapper_class="mb-4" %}
{% bootstrap_field form.study_type label_class="form-label fw-bold" wrapper_class="mb-4" %} +{% bootstrap_field form.language label_class="form-label fw-bold" wrapper_class="mb-4" %} diff --git a/web/templates/web/studies-list.html b/web/templates/web/studies-list.html index 2d0469ae2..01b4b0398 100644 --- a/web/templates/web/studies-list.html +++ b/web/templates/web/studies-list.html @@ -15,8 +15,9 @@ class="row justify-content-center align-items-center g-lg-3"> {% csrf_token %} {{ form.study_list_tabs }} -
{% bootstrap_field form.child show_label=False %}
-
{% bootstrap_field form.study_location show_label=False %}
+
{% bootstrap_field form.child show_label=False %}
+
{% bootstrap_field form.study_location show_label=False %}
+
{% bootstrap_field form.study_language show_label=False %}
{% if user.is_authenticated %}
{% bootstrap_field form.hide_studies_we_have_done %}
{% else %} diff --git a/web/views.py b/web/views.py index 6055c453c..867167737 100644 --- a/web/views.py +++ b/web/views.py @@ -13,7 +13,7 @@ from django.dispatch import receiver from django.http import HttpResponseForbidden, HttpResponseRedirect from django.shortcuts import get_object_or_404, redirect, reverse -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import get_language, gettext_lazy as _ from django.views import generic from django.views.generic.edit import FormView from django_countries import countries @@ -351,6 +351,11 @@ def filter_studies(self, studies: QuerySet) -> QuerySet: hide_studies_we_have_done_value = session.get("hide_studies_we_have_done", "") tab_value = session.get("study_list_tabs", "0") study_location_value = session.get("study_location", "0") + if get_language()=='ja': + default_language = 'ja' + else: + default_language = '*' + study_language = session.get("study_language", default_language) # title search if search_value: @@ -370,6 +375,10 @@ def filter_studies(self, studies: QuerySet) -> QuerySet: studies = studies.filter(query) query = None + # filtering by study language + if not study_language == "*": + studies = studies.filter(language = study_language) + # Scheduled or unscheduled studies if tab_value == StudyListSearchForm.Tabs.synchronous_studies.value[0]: query = Q(