Skip to content

Commit

Permalink
Merge pull request #389 from codeforIATI/localised_organisation_names…
Browse files Browse the repository at this point in the history
…_part2

Localised organisation names - part 2
  • Loading branch information
radix0000 authored Jan 24, 2023
2 parents b58c268 + e7ae038 commit 8296c3d
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 11 deletions.
49 changes: 44 additions & 5 deletions iati_datastore/iatilib/frontend/serialize/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ def transaction_org(field, transaction):
receiver_org = partial(transaction_org, 'receiver_org')


def transaction_org_name(field, transaction):
org = getattr(transaction, field)
if org:
locale = request.args.get("locale", "en")
if org.name_all_values and locale in org.name_all_values:
return org.name_all_values[locale]
elif org.name_all_values and 'default' in org.name_all_values:
return org.name_all_values['default']
else:
return org.name
else:
return ""


provider_org_name = partial(transaction_org_name, 'provider_org')
receiver_org_name = partial(transaction_org_name, 'receiver_org')


def title(activity):
"""Select most appropriate title for request locale"""
locale = request.args.get("locale", "en")
Expand Down Expand Up @@ -234,7 +252,13 @@ def reporting_org_ref(activity):

def reporting_org_name(activity):
try:
return activity.reporting_org.name
locale = request.args.get("locale", "en")
if activity.reporting_org.name_all_values and locale in activity.reporting_org.name_all_values:
return activity.reporting_org.name_all_values[locale]
elif activity.reporting_org.name_all_values and 'default' in activity.reporting_org.name_all_values:
return activity.reporting_org.name_all_values['default']
else:
return activity.reporting_org.name
except AttributeError:
return ""

Expand All @@ -253,13 +277,29 @@ def reporting_org_type_code(activity):
return ""


def participating_org_localised_name(org):
try:
locale = request.args.get("locale", "en")
if org.name_all_values and locale in org.name_all_values:
return org.name_all_values[locale]
elif org.name_all_values and 'default' in org.name_all_values:
return org.name_all_values['default']
else:
return org.name
except AttributeError:
return ""


def participating_org(attr, role, activity):
activity_by_role = dict(
[(a.role.value, a) for a in activity.participating_orgs])

participant = activity_by_role.get(role.value, "")
if participant:
return getattr(participant.organisation, attr)
if attr == "name":
return participating_org_localised_name(participant.organisation)
else:
return getattr(participant.organisation, attr)
else:
return ''

Expand Down Expand Up @@ -807,11 +847,11 @@ def wrapper(args):
(u'transaction_ref', lambda t: t.ref),
(u'transaction_value_currency', value_currency),
(u'transaction_value_value-date', lambda t: t.value_date),
(u'transaction_provider-org', lambda t: t.provider_org_text),
(u'transaction_provider-org', provider_org_name),
(u'transaction_provider-org_ref', provider_org),
(u'transaction_provider-org_provider-activity-id',
lambda t: t.provider_org_activity_id),
(u'transaction_receiver-org', lambda t: t.receiver_org_text),
(u'transaction_receiver-org', receiver_org_name),
(u'transaction_receiver-org_ref', receiver_org),
(u'transaction_receiver-org_receiver-activity-id',
lambda t: t.receiver_org_activity_id),
Expand All @@ -830,7 +870,6 @@ def wrapper(args):
(u"transaction_sector", sector),
(u"transaction_sector-vocabulary", sector_vocabulary),
(u"transaction_sector-vocabulary-code", sector_vocabulary_code),
# (u'reporting-org', lambda t: t.activity.reporting_org_ref),
)

_transaction_fields = (
Expand Down
6 changes: 6 additions & 0 deletions iati_datastore/iatilib/test/fixtures/localised-org-names.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<reporting-org ref="DE-1" secondary-reporter="0" type="10">
<narrative xml:lang="de">Bundesministerium für wirtschaftliche Zusammenarbeit und Entwicklung (BMZ)</narrative>
<narrative xml:lang="en">Federal Ministry for Economic Cooperation and Development (BMZ)</narrative>
<narrative xml:lang="fr">Ministère fédéral de la Coopération économique et du Développement (BMZ)</narrative>
</reporting-org>
<title>
<narrative xml:lang="pt">Esgotamento Sanitário Pernambuco</narrative>
Expand All @@ -12,6 +13,7 @@
<participating-org ref="DE-1" role="1" type="10">
<narrative xml:lang="de">Bundesministerium für wirtschaftliche Zusammenarbeit und Entwicklung (BMZ)</narrative>
<narrative xml:lang="en">Federal Ministry for Economic Cooperation and Development (BMZ)</narrative>
<narrative xml:lang="fr">Ministère fédéral de la Coopération économique et du Développement (BMZ)</narrative>
</participating-org>
<participating-org ref="XM-DAC-5-2" role="2">
<narrative xml:lang="de">KfW Bankengruppe (KfW)</narrative>
Expand All @@ -28,9 +30,11 @@
</description>
<provider-org ref="NO-BRC-971277882" type="10">
<narrative xml:lang="en">Norwegian Agency for Development Cooperation (NORAD)</narrative>
<narrative xml:lang="fr">Agence norvégienne de coopération au développement (NORAD)</narrative>
</provider-org>
<receiver-org ref="DK-CVR-12921047" receiver-activity-id="DK-CVR-12921047-2018-21LotCivSPA" type="22">
<narrative xml:lang="en">Care Danmark</narrative>
<narrative xml:lang="fr">Soins Danemark</narrative>
</receiver-org>
</transaction>
<transaction>
Expand All @@ -42,9 +46,11 @@
</description>
<provider-org ref="BD-NAB-0210" provider-activity-id="BD-NAB-0210-POWER" type="21">
<narrative xml:lang="en">ActionAid Bangladesh</narrative>
<narrative xml:lang="es">Ayuda en Acción Bangladesh</narrative>
</provider-org>
<receiver-org type="22">
<narrative xml:lang="en">SKS Foundation</narrative>
<narrative xml:lang="fr">Fondation SKS</narrative>
</receiver-org>
</transaction>
</iati-activity>
Expand Down
84 changes: 84 additions & 0 deletions iati_datastore/iatilib/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ def test_french_description_target_groups(self):
output[1][i]
)


class TestActivityCurrencyConversionOutput(ClientTestCase):
"""Test new functionality to output USD and EUR in activities"""

Expand Down Expand Up @@ -1097,3 +1098,86 @@ def test_eur_currency_fields(self):
self.assertEquals(u'3924408.74', output[4][csv_headers.index('budget-value-EUR')]) # 2019-12-10: 3343511 GBP
self.assertEquals(u'3999715.84', output[5][csv_headers.index('budget-value-EUR')]) # 2020-12-10: 3587780 GBP

class TestActivityLocalesOrganisationNames(ClientTestCase):
"""Test new functionality to output locale appropriate organisation names"""

base_url = '/api/1/access/activity.csv'

def test_csv_activity_count(self):
load_fix("localised-org-names.xml")
resp = self.client.get(self.base_url)
self.assertEquals(2, resp.get_data(as_text=True).count("\n"))

def test_english_reporting_org(self):
load_fix("localised-org-names.xml")
output = list(csv.reader(StringIO(self.client.get(self.base_url).get_data(as_text=True))))
csv_headers = output[0]
i = csv_headers.index('reporting-org')
self.assertEquals(
u'Federal Ministry for Economic Cooperation and Development (BMZ)',
output[1][i]
)

def test_french_reporting_org(self):
load_fix("localised-org-names.xml")
output = list(csv.reader(StringIO(self.client.get(self.base_url + '?locale=fr').get_data(as_text=True))))
csv_headers = output[0]
i = csv_headers.index('reporting-org')
self.assertEquals(
u"Ministère fédéral de la Coopération économique et du Développement (BMZ)",
output[1][i]
)

def test_english_participating_org(self):
load_fix("localised-org-names.xml")
output = list(csv.reader(StringIO(self.client.get(self.base_url).get_data(as_text=True))))
csv_headers = output[0]
i = csv_headers.index('participating-org (Funding)')
self.assertEquals(
u'Federal Ministry for Economic Cooperation and Development (BMZ)',
output[1][i]
)

def test_french_participating_org(self):
load_fix("localised-org-names.xml")
output = list(csv.reader(StringIO(self.client.get(self.base_url + '?locale=fr').get_data(as_text=True))))
csv_headers = output[0]
i = csv_headers.index('participating-org (Funding)')
self.assertEquals(
u'Ministère fédéral de la Coopération économique et du Développement (BMZ)',
output[1][i]
)


class TestTransactionLocalesOrganisationNames(ClientTestCase):
"""Test new functionality to output locale appropriate organisation names"""

base_url = '/api/1/access/transaction.csv'

def test_provider_org_output(self):
load_fix("localised-org-names.xml")
output = list(csv.reader(StringIO(self.client.get(self.base_url).get_data(as_text=True))))
csv_headers = output[0]
i = csv_headers.index('transaction_provider-org')
self.assertEquals(u'Norwegian Agency for Development Cooperation (NORAD)', output[1][i])

def test_provider_org_french_output(self):
load_fix("localised-org-names.xml")
output = list(csv.reader(StringIO(self.client.get(self.base_url + '?locale=fr').get_data(as_text=True))))
csv_headers = output[0]
i = csv_headers.index('transaction_provider-org')
self.assertEquals(u'Agence norvégienne de coopération au développement (NORAD)', output[1][i])

def test_receiver_org_output(self):
load_fix("localised-org-names.xml")
output = list(csv.reader(StringIO(self.client.get(self.base_url).get_data(as_text=True))))
csv_headers = output[0]
i = csv_headers.index('transaction_receiver-org')
self.assertEquals(u'SKS Foundation', output[2][i])

def test_receiver_org_french_output(self):
load_fix("localised-org-names.xml")
output = list(csv.reader(StringIO(self.client.get(self.base_url + '?locale=fr').get_data(as_text=True))))
csv_headers = output[0]
i = csv_headers.index('transaction_receiver-org')
self.assertEquals(u'Fondation SKS', output[2][i])
19 changes: 13 additions & 6 deletions iati_datastore/iatilib/test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,14 +1118,16 @@ def test_reporting_org_name(self):
self.assertEquals(
self.act.reporting_org.name_all_values,
{'de': 'Bundesministerium für wirtschaftliche Zusammenarbeit und Entwicklung (BMZ)',
'en': 'Federal Ministry for Economic Cooperation and Development (BMZ)'}
'en': 'Federal Ministry for Economic Cooperation and Development (BMZ)',
'fr': 'Ministère fédéral de la Coopération économique et du Développement (BMZ)'}
)

def test_participating_orgs(self):
self.assertEquals(
self.act.participating_orgs[0].organisation.name_all_values,
{"de": 'Bundesministerium für wirtschaftliche Zusammenarbeit und Entwicklung (BMZ)',
"en": 'Federal Ministry for Economic Cooperation and Development (BMZ)'}
"en": 'Federal Ministry for Economic Cooperation and Development (BMZ)',
"fr": 'Ministère fédéral de la Coopération économique et du Développement (BMZ)'}
)
self.assertEquals(
self.act.participating_orgs[1].organisation.name_all_values,
Expand All @@ -1138,16 +1140,20 @@ def test_participating_orgs(self):

def test_transaction_reciever_org_name(self):
self.assertEquals(self.act.transactions[0].provider_org.name_all_values,
{"en": 'Norwegian Agency for Development Cooperation (NORAD)'}
{"en": 'Norwegian Agency for Development Cooperation (NORAD)',
"fr": 'Agence norvégienne de coopération au développement (NORAD)'}
)
self.assertEquals(self.act.transactions[0].receiver_org.name_all_values,
{"en": 'Care Danmark'}
{"en": 'Care Danmark',
"fr": 'Soins Danemark'}
)
self.assertEquals(self.act.transactions[1].provider_org.name_all_values,
{"en": 'ActionAid Bangladesh'}
{"en": 'ActionAid Bangladesh',
"es": 'Ayuda en Acción Bangladesh'}
)
self.assertEquals(self.act.transactions[1].receiver_org.name_all_values,
{"en": 'SKS Foundation'}
{"en": 'SKS Foundation',
"fr": 'Fondation SKS'}
)

class TestLocalisedOrganisationNamesActivityDefault(AppTestCase):
Expand Down Expand Up @@ -1191,3 +1197,4 @@ def test_transaction_reciever_org_name(self):
self.assertEquals(self.act.transactions[1].receiver_org.name_all_values,
{"de": 'SKS-Stiftung'}
)

0 comments on commit 8296c3d

Please sign in to comment.