diff --git a/src/formpack/constants.py b/src/formpack/constants.py index c811296..3d50f1f 100644 --- a/src/formpack/constants.py +++ b/src/formpack/constants.py @@ -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' diff --git a/src/formpack/pack.py b/src/formpack/pack.py index 7cf6e1f..6465ee0 100644 --- a/src/formpack/pack.py +++ b/src/formpack/pack.py @@ -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: @@ -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, @@ -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, diff --git a/src/formpack/reporting/export.py b/src/formpack/reporting/export.py index 069d55b..17d0512 100644 --- a/src/formpack/reporting/export.py +++ b/src/formpack/reporting/export.py @@ -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 @@ -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, @@ -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 @@ -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 @@ -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 @@ -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, ): """ @@ -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, diff --git a/tests/test_exports.py b/tests/test_exports.py index b676051..99124bf 100644 --- a/tests/test_exports.py +++ b/tests/test_exports.py @@ -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)