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
2 changes: 2 additions & 0 deletions src/formpack/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def to_json(self, **kwargs):
def export(self, lang=UNSPECIFIED_TRANSLATION, group_sep='/', hierarchy_in_labels=False,
versions=-1, multiple_select="both",
force_index=False, copy_fields=(), title=None,
header_lang=-1,
tag_cols_for_header=None):
"""
Create an export for given versions of the form
Expand All @@ -348,6 +349,7 @@ def export(self, lang=UNSPECIFIED_TRANSLATION, group_sep='/', hierarchy_in_label
version_id_keys=self.version_id_keys(versions),
title=title, multiple_select=multiple_select,
force_index=force_index, copy_fields=copy_fields,
header_lang=header_lang,
tag_cols_for_header=tag_cols_for_header)

def autoreport(self, versions=-1):
Expand Down
12 changes: 8 additions & 4 deletions src/formpack/reporting/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, formpack, form_versions, lang=UNSPECIFIED_TRANSLATION,
group_sep="/", hierarchy_in_labels=False,
version_id_keys=[],
multiple_select="both", copy_fields=(), force_index=False,
title="submissions", tag_cols_for_header=None):
title="submissions", tag_cols_for_header=None, header_lang=-1):
"""

:param formpack: FormPack
Expand All @@ -44,10 +44,13 @@ def __init__(self, formpack, form_versions, lang=UNSPECIFIED_TRANSLATION,
:param force_index: bool.
:param title: string
:param tag_cols_for_header: list
:param header_lang: string, False (`constants.UNSPECIFIED_TRANSLATION`), or
None (`constants.UNTRANSLATED`), if not set, default value equal lang arg.
"""

self.formpack = formpack
self.lang = lang
self.header_lang = self.lang if header_lang == -1 else header_lang
self.group_sep = group_sep
self.title = title
self.versions = form_versions
Expand Down Expand Up @@ -76,7 +79,7 @@ def __init__(self, formpack, form_versions, lang=UNSPECIFIED_TRANSLATION,
# this deals with merging all form versions headers and labels
params = (
lang, group_sep, hierarchy_in_labels, multiple_select,
tag_cols_for_header,
tag_cols_for_header, self.header_lang,
)
res = self.get_fields_labels_tags_for_all_versions(*params)
self.sections, self.labels, self.tags = res
Expand Down Expand Up @@ -157,7 +160,8 @@ def get_fields_labels_tags_for_all_versions(self,
group_sep="/",
hierarchy_in_labels=False,
multiple_select="both",
tag_cols_for_header=None):
tag_cols_for_header=None,
header_lang=UNSPECIFIED_TRANSLATION):
""" Return 3 mappings containing field, labels, and tags by section

This is needed because when making an export for several
Expand Down Expand Up @@ -199,7 +203,7 @@ def get_fields_labels_tags_for_all_versions(self,
for field in all_fields:
section_fields.setdefault(field.section.name, []).append(field)
section_labels.setdefault(field.section.name, []).append(
field.get_labels(lang, group_sep,
field.get_labels(header_lang, group_sep,
hierarchy_in_labels,
multiple_select)
)
Expand Down
114 changes: 114 additions & 0 deletions tests/test_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,9 @@ def test_xlsx(self):
fp.export(**options).to_xlsx(xls, submissions)
assert xls.isfile()

def test_jdu(self):
assert 1 == 1

def test_xlsx_long_sheet_names_and_invalid_chars(self):
title, schemas, submissions = build_fixture('long_names')
fp = FormPack(schemas, title)
Expand Down Expand Up @@ -1337,6 +1340,8 @@ def test_xlsx_with_tag_headers(self):
row_values = [cell.value for cell in sheet.row(1)]
assert row_values == ['#beneficiary', '', '']



def test_force_index(self):
title, schemas, submissions = customer_satisfaction

Expand Down Expand Up @@ -2040,3 +2045,112 @@ def test_geojson_invalid(self):
"type": "Polygon"
}
)

#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)