Skip to content

Commit

Permalink
fix: Use fallback languages for analytics summaries
Browse files Browse the repository at this point in the history
  • Loading branch information
open-dynaMIX committed Nov 28, 2024
1 parent e32a269 commit a052463
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
21 changes: 19 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,24 @@ def update_table_summary_from_table_question(instance, *args, **kwargs):
update_table_summary(instance=ad)


def get_translation_with_fallback(value):
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 +160,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

0 comments on commit a052463

Please sign in to comment.