From 9b28ea0014f33d5222d163ab29cd911ca3ed6227 Mon Sep 17 00:00:00 2001 From: Ketan <73937490+devketanpro@users.noreply.github.com> Date: Thu, 7 Nov 2024 15:44:22 +0530 Subject: [PATCH] add ednote and associations description text fields in translated macro [SDNTB-875] (#517) * add ednote and associations description text fields in translated macro [SDNTB-875] * add tests --- server/ntb/macros/nob_NO_translate_macro.py | 18 +++++ .../macros/nob_NO_translate_macro_test.py | 67 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 server/ntb/tests/macros/nob_NO_translate_macro_test.py diff --git a/server/ntb/macros/nob_NO_translate_macro.py b/server/ntb/macros/nob_NO_translate_macro.py index 180774736..823b4fae2 100644 --- a/server/ntb/macros/nob_NO_translate_macro.py +++ b/server/ntb/macros/nob_NO_translate_macro.py @@ -30,9 +30,19 @@ def nob_NO_translate_macro(item, **kwargs): "description_html", "description_text", "evolvedfrom", + "ednote", ) } + # extract associations description_text from the item associations structure. + associations_desc_flat = { + f"associations_desc_{editor}": assoc["description_text"] + for editor, assoc in item.get("associations", {}).items() + if "description_text" in assoc + } + + payload.update(associations_desc_flat) + data = { "token": token, "document": payload, @@ -45,6 +55,14 @@ def nob_NO_translate_macro(item, **kwargs): if r.status_code == 200: response = r.json() item.update(response["document"]) + + # map translated `description_text` fields back to the associations. + for editor in item.get("associations", {}): + flat_key = f"associations_desc_{editor}" + if flat_key in response["document"]: + item["associations"][editor]["description_text"] = response["document"][ + flat_key + ] return item diff --git a/server/ntb/tests/macros/nob_NO_translate_macro_test.py b/server/ntb/tests/macros/nob_NO_translate_macro_test.py new file mode 100644 index 000000000..d4fd8ef0d --- /dev/null +++ b/server/ntb/tests/macros/nob_NO_translate_macro_test.py @@ -0,0 +1,67 @@ +from superdesk.tests import TestCase +from superdesk.metadata.item import CONTENT_STATE + +from ntb.macros import nob_NO_translate_macro +from unittest.mock import patch, MagicMock + + +class TranslateMacroTestCase(TestCase): + item = { + "headline": "TEAS EXPO", + "body_html": "

Hva synes du om Norge aftan_eftan.vok-a2e

", + "guid": "9d7ba4b4-69a6-4f45-be96-1b0f26a6f89a", + "abstract": "", + "description_text": "None", + "ednote": "Hva", + "associations": { + "editor_1": { + "guid": "9e7ba4b4-69a6-4f45-be96-1b0f26a6f89b", + "description_text": "Hva synes du om Norge", + } + }, + "state": CONTENT_STATE.INGESTED, + } + + user_preferences = { + "user_preferences": { + "macro_config": { + "fields": {"Formval nynorskrobot": "headline,description_text"} + } + } + } + + api_response_mock = { + "document": { + "headline": "TEA EXPO", + "body_html": "

Kva synest du om Noreg aftan_eftan.vok-a2e

", + "guid": "9d7ba4b4-69a6-4f45-be96-1b0f26a6f89a", + "abstract": "", + "description_text": "None", + "ednote": "Kva", + "associations_desc_editor_1": "Kva synest du om Noreg", + }, + "prefs": {"headline": True}, + "fileType": "html", + } + + @patch("ntb.macros.nob_NO_translate_macro.get_user", return_value=user_preferences) + @patch("ntb.macros.nob_NO_translate_macro.requests.post") + def test_associate_item_translated(self, mock_post, mock_get_user): + # Mock the API response + mock_post.return_value = MagicMock( + status_code=200, json=lambda: self.api_response_mock + ) + + item = self.item.copy() + + nob_NO_translate_macro.callback(item) + + self.assertEqual(item["headline"], "TEA EXPO") + self.assertEqual( + item["body_html"], "

Kva synest du om Noreg aftan_eftan.vok-a2e

" + ) + self.assertEqual(item["ednote"], "Kva") + self.assertEqual( + item["associations"]["editor_1"]["description_text"], + "Kva synest du om Noreg", + )