Skip to content

Commit a052463

Browse files
committed
fix: Use fallback languages for analytics summaries
1 parent e32a269 commit a052463

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

caluma/extensions/events/form.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from django.db import transaction
66
from django.db.models.signals import post_save
7+
from django.utils.translation import get_language
78

89
from caluma.caluma_core.events import on
910
from caluma.caluma_form import models as caluma_form_models
@@ -102,11 +103,24 @@ def update_table_summary_from_table_question(instance, *args, **kwargs):
102103
update_table_summary(instance=ad)
103104

104105

106+
def get_translation_with_fallback(value):
107+
lang = get_language()
108+
fallback_langs = ["en", "de"]
109+
fallback_langs.remove(lang)
110+
translated = getattr(value, lang)
111+
if not translated:
112+
for fl in fallback_langs:
113+
translated = getattr(value, fl)
114+
if translated:
115+
break
116+
return translated
117+
118+
105119
def _make_csv_summary(table_answer):
106120
def get_answer_value(answer):
107121
value = answer.value or answer.date
108122
if options := answer.selected_options:
109-
value = ",".join([str(o.label) for o in options])
123+
value = ",".join([get_translation_with_fallback(o.label) for o in options])
110124
return value
111125

112126
def get_lines(ads, q_slugs_and_labels):
@@ -146,4 +160,7 @@ def get_lines(ads, q_slugs_and_labels):
146160

147161
def _sorted_form_question_slugs_and_labels(form):
148162
fqs = caluma_form_models.FormQuestion.objects.filter(form=form).order_by("-sort")
149-
return [(fq.question.slug, str(fq.question.label)) for fq in fqs]
163+
return [
164+
(fq.question.slug, get_translation_with_fallback(fq.question.label))
165+
for fq in fqs
166+
]

caluma/extensions/tests/test_events.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,15 @@ def add_table_with_summary(
617617
)
618618
form_question_factory(form=row_form, question=row_question_2)
619619

620+
# Only set English label to test fallback
620621
row_question_3 = question_factory(
621622
type=Question.TYPE_CHOICE,
622623
slug="row3",
623-
label="row3_label",
624+
label={"en": "row3_label"},
624625
is_required="true",
625626
is_hidden="false",
626627
)
628+
627629
question_option_factory(
628630
question=row_question_3, option=option_factory(slug="o1", label="option1 label")
629631
)

0 commit comments

Comments
 (0)