Skip to content

Commit 33ae49c

Browse files
authored
Merge pull request #1162 from open-dynaMIX/summary_fallback_languages
fix: Use fallback languages for analytics summaries
2 parents 4a050fc + 6bcf903 commit 33ae49c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

caluma/extensions/events/form.py

+21-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,26 @@ 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+
# This should not be necessary, because LOCALIZED_FIELDS_FALLBACKS is set
108+
# https://django-localized-fields.readthedocs.io/en/latest/settings.html#localized-fields-fallbacks
109+
lang = get_language()
110+
fallback_langs = ["en", "de"]
111+
fallback_langs.remove(lang)
112+
translated = getattr(value, lang)
113+
if not translated:
114+
for fl in fallback_langs:
115+
translated = getattr(value, fl)
116+
if translated:
117+
break
118+
return translated
119+
120+
105121
def _make_csv_summary(table_answer):
106122
def get_answer_value(answer):
107123
value = answer.value or answer.date
108124
if options := answer.selected_options:
109-
value = ",".join([str(o.label) for o in options])
125+
value = ",".join([get_translation_with_fallback(o.label) for o in options])
110126
return value
111127

112128
def get_lines(ads, q_slugs_and_labels):
@@ -146,4 +162,7 @@ def get_lines(ads, q_slugs_and_labels):
146162

147163
def _sorted_form_question_slugs_and_labels(form):
148164
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]
165+
return [
166+
(fq.question.slug, get_translation_with_fallback(fq.question.label))
167+
for fq in fqs
168+
]

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)