Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use fallback languages for analytics summaries #1162

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions caluma/extensions/events/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.db import transaction
from django.db.models.signals import post_save
from django.utils.translation import get_language

from caluma.caluma_core.events import on
from caluma.caluma_form import models as caluma_form_models
Expand Down Expand Up @@ -102,11 +103,26 @@ def update_table_summary_from_table_question(instance, *args, **kwargs):
update_table_summary(instance=ad)


def get_translation_with_fallback(value):
# This should not be necessary, because LOCALIZED_FIELDS_FALLBACKS is set
# https://django-localized-fields.readthedocs.io/en/latest/settings.html#localized-fields-fallbacks
lang = get_language()
fallback_langs = ["en", "de"]
fallback_langs.remove(lang)
translated = getattr(value, lang)
if not translated:
for fl in fallback_langs:
translated = getattr(value, fl)
if translated:
break
return translated


def _make_csv_summary(table_answer):
def get_answer_value(answer):
value = answer.value or answer.date
if options := answer.selected_options:
value = ",".join([str(o.label) for o in options])
value = ",".join([get_translation_with_fallback(o.label) for o in options])
return value

def get_lines(ads, q_slugs_and_labels):
Expand Down Expand Up @@ -146,4 +162,7 @@ def get_lines(ads, q_slugs_and_labels):

def _sorted_form_question_slugs_and_labels(form):
fqs = caluma_form_models.FormQuestion.objects.filter(form=form).order_by("-sort")
return [(fq.question.slug, str(fq.question.label)) for fq in fqs]
return [
(fq.question.slug, get_translation_with_fallback(fq.question.label))
for fq in fqs
]
4 changes: 3 additions & 1 deletion caluma/extensions/tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,13 +617,15 @@ def add_table_with_summary(
)
form_question_factory(form=row_form, question=row_question_2)

# Only set English label to test fallback
row_question_3 = question_factory(
type=Question.TYPE_CHOICE,
slug="row3",
label="row3_label",
label={"en": "row3_label"},
is_required="true",
is_hidden="false",
)

question_option_factory(
question=row_question_3, option=option_factory(slug="o1", label="option1 label")
)
Expand Down
Loading