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

add lang_header arg for specify language header values #215

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions src/formpack/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
# X | En | Fr
UNTRANSLATED = None

# This constant is used to signify that the arg header_lang is not set
UNSPECIFIED_HEADER_LANG = "UNSPECIFIED_HEADER_LANG"

# the column used to denote "or_other" in a select question type
# this is non-standard XLSForm
OR_OTHER_COLUMN = '_or_other'
Expand Down
3 changes: 3 additions & 0 deletions src/formpack/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .utils.expand_content import expand_content
from .utils.replace_aliases import replace_aliases
from .constants import UNSPECIFIED_TRANSLATION
from .constants import UNSPECIFIED_HEADER_LANG


class FormPack:
Expand Down Expand Up @@ -367,6 +368,7 @@ def export(
copy_fields=(),
title=None,
tag_cols_for_header=None,
header_lang=UNSPECIFIED_HEADER_LANG,
filter_fields=(),
xls_types_as_text=True,
include_media_url=False,
Expand All @@ -388,6 +390,7 @@ def export(
force_index=force_index,
copy_fields=copy_fields,
tag_cols_for_header=tag_cols_for_header,
header_lang=header_lang,
filter_fields=filter_fields,
xls_types_as_text=xls_types_as_text,
include_media_url=include_media_url,
Expand Down
10 changes: 9 additions & 1 deletion src/formpack/reporting/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
GEO_QUESTION_TYPES,
TAG_COLUMNS_AND_SEPARATORS,
UNSPECIFIED_TRANSLATION,
UNSPECIFIED_HEADER_LANG,
)
from ..schema import CopyField, FormField
from ..submission import FormSubmission
Expand Down Expand Up @@ -46,6 +47,7 @@ def __init__(
force_index=False,
title='submissions',
tag_cols_for_header=None,
header_lang=UNSPECIFIED_HEADER_LANG,
filter_fields=(),
xls_types_as_text=True,
include_media_url=False,
Expand All @@ -65,6 +67,9 @@ def __init__(
:param force_index: bool.
:param title: string
:param tag_cols_for_header: list
lang_header
:param header_lang: string, False (`constants.UNSPECIFIED_TRANSLATION`), or
None (`constants.UNTRANSLATED`), if not set, default value equal lang arg.
:param filter_fields: list
:param xls_types_as_text: bool
:param include_media_url: bool
Expand All @@ -73,6 +78,7 @@ def __init__(
self.formpack = formpack
self.analysis_form = formpack.analysis_form
self.lang = lang
self.header_lang = self.lang if header_lang == UNSPECIFIED_HEADER_LANG else header_lang
self.group_sep = group_sep
self.title = title
self.versions = form_versions
Expand Down Expand Up @@ -122,6 +128,7 @@ def __init__(
lang,
group_sep,
hierarchy_in_labels,
self.header_lang,
tag_cols_for_header,
)
self.sections, self.labels, self.tags = res
Expand Down Expand Up @@ -204,6 +211,7 @@ def get_fields_labels_tags_for_all_versions(
lang=UNSPECIFIED_TRANSLATION,
group_sep='/',
hierarchy_in_labels=False,
header_lang=UNSPECIFIED_TRANSLATION,
tag_cols_for_header=None,
):
"""
Expand Down Expand Up @@ -269,7 +277,7 @@ def get_fields_labels_tags_for_all_versions(
section_fields.setdefault(field.section.name, []).append(field)
section_labels.setdefault(field.section.name, []).append(
field.get_labels(
lang=lang,
lang=header_lang,
group_sep=group_sep,
hierarchy_in_labels=hierarchy_in_labels,
multiple_select=self.multiple_select,
Expand Down
109 changes: 109 additions & 0 deletions tests/test_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -2967,3 +2967,112 @@ def test_geojson_unflattened(self):
],
},
]

#https://github.com/kobotoolbox/formpack/pull/215
def test_header_label_list_label(self):
title, schemas, submissions = customer_satisfaction
fp = FormPack(schemas, title)
options = {'header_lang': None, 'lang' : None}
exported = fp.export(**options).to_dict(submissions)
expected = OrderedDict({
"Customer Satisfaction": {
'fields': ["Restaurant name",
"Did you enjoy your dining experience?"],
'data': [
["Felipes", "Yes"],
["Dunkin Donuts", "No"],
["McDonalds", "No"]
]
}
})
self.assertEqual(exported, expected)

def test_header_key_list_key(self):
title, schemas, submissions = customer_satisfaction
fp = FormPack(schemas, title)
options = {'header_lang': False, 'lang' : False}
exported = fp.export(**options).to_dict(submissions)
expected = OrderedDict({
"Customer Satisfaction": {
'fields': ["restaurant_name",
"customer_enjoyment"],
'data': [
["Felipes", "yes"],
["Dunkin Donuts", "no"],
["McDonalds", "no"]
]
}
})
self.assertEqual(exported, expected)

def test_header_key_list_label(self):
title, schemas, submissions = customer_satisfaction
fp = FormPack(schemas, title)
options = {'header_lang': False, 'lang' : None}
exported = fp.export(**options).to_dict(submissions)
expected = OrderedDict({
"Customer Satisfaction": {
'fields': ["restaurant_name",
"customer_enjoyment"],
'data': [
["Felipes", "Yes"],
["Dunkin Donuts", "No"],
["McDonalds", "No"]
]
}
})
self.assertEqual(exported, expected)

def test_header_Label_list_key(self):
title, schemas, submissions = customer_satisfaction
fp = FormPack(schemas, title)
options = {'header_lang': None, 'lang' : False}
exported = fp.export(**options).to_dict(submissions)
expected = OrderedDict({
"Customer Satisfaction": {
'fields': ["Restaurant name",
"Did you enjoy your dining experience?"],
'data': [
["Felipes", "yes"],
["Dunkin Donuts", "no"],
["McDonalds", "no"]
]
}
})
self.assertEqual(exported, expected)

def test_header_label_no_lang(self):
title, schemas, submissions = customer_satisfaction
fp = FormPack(schemas, title)
options = {'header_lang': None}
exported = fp.export(**options).to_dict(submissions)
expected = OrderedDict({
"Customer Satisfaction": {
'fields': ["Restaurant name",
"Did you enjoy your dining experience?"],
'data': [
["Felipes", "yes"],
["Dunkin Donuts", "no"],
["McDonalds", "no"]
]
}
})
self.assertEqual(exported, expected)

def test_header_key_no_lang(self):
title, schemas, submissions = customer_satisfaction
fp = FormPack(schemas, title)
options = {'header_lang': False}
exported = fp.export(**options).to_dict(submissions)
expected = OrderedDict({
"Customer Satisfaction": {
'fields': ["restaurant_name",
"customer_enjoyment"],
'data': [
["Felipes", "yes"],
["Dunkin Donuts", "no"],
["McDonalds", "no"]
]
}
})
self.assertEqual(exported, expected)