From 91cb1778f925e52660199cbdfcc677e265afe951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Ja=C5=A1ek?= Date: Tue, 20 Aug 2024 09:12:05 +0200 Subject: [PATCH] use translations when populating anpa category on ingest (#2666) SDCP-819 --- superdesk/io/commands/update_ingest.py | 12 ++++++++++++ tests/io/update_ingest_tests.py | 9 ++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/superdesk/io/commands/update_ingest.py b/superdesk/io/commands/update_ingest.py index 45f6914870..ae925f6e83 100644 --- a/superdesk/io/commands/update_ingest.py +++ b/superdesk/io/commands/update_ingest.py @@ -369,6 +369,11 @@ def process_anpa_category(item, provider): # make the case of the qcode match what we hold in our dictionary item_category["qcode"] = mapped_category[0]["qcode"] item_category["scheme"] = "categories" + if mapped_category[0].get("translations"): + item_category["translations"] = mapped_category[0]["translations"] + if item.get("language"): + set_subject_name_translation(item_category, item["language"]) + except Exception as ex: raise ProviderError.anpaError(ex, provider) @@ -774,4 +779,11 @@ def set_expiry(item, provider, parent_expiry=None): ) +def set_subject_name_translation(subject, language) -> None: + try: + subject["name"] = subject["translations"]["name"][language] + except (KeyError, TypeError): + pass + + superdesk.command("ingest:update", UpdateIngest()) diff --git a/tests/io/update_ingest_tests.py b/tests/io/update_ingest_tests.py index d5474a1235..179f13dae0 100644 --- a/tests/io/update_ingest_tests.py +++ b/tests/io/update_ingest_tests.py @@ -380,7 +380,12 @@ def test_subject_to_anpa_category_derived_ingest(self): "_id": "categories", "items": [ {"is_active": True, "name": "Australian Weather", "qcode": "b", "subject": "17000000"}, - {"is_active": True, "name": "Finance", "qcode": "f"}, + { + "is_active": True, + "name": "Finance", + "qcode": "f", + "translations": {"name": {"fr": "Finance FR"}}, + }, ], }, {"_id": "genre", "items": [{"qcode": "feature", "name": "feature"}]}, @@ -399,10 +404,12 @@ def test_subject_to_anpa_category_derived_ingest(self): for item in items: item["ingest_provider"] = provider["_id"] item["expiry"] = utcnow() + timedelta(hours=11) + item["language"] = "fr" # ingest the items and check the subject code has been derived self.ingest_items(items, provider, provider_service) self.assertEqual(items[0]["anpa_category"][0]["qcode"], "f") + self.assertEqual(items[0]["anpa_category"][0]["name"], "Finance FR") def test_subject_to_anpa_category_derived_ingest_ignores_inactive_map_entries(self): vocab = [