Skip to content

Commit

Permalink
add ednote and associations description text fields in translated mac…
Browse files Browse the repository at this point in the history
…ro [SDNTB-875] (#517)

* add ednote and associations description text fields in translated macro [SDNTB-875]

* add tests
  • Loading branch information
devketanpro authored Nov 7, 2024
1 parent 4a28e97 commit 9b28ea0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
18 changes: 18 additions & 0 deletions server/ntb/macros/nob_NO_translate_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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


Expand Down
67 changes: 67 additions & 0 deletions server/ntb/tests/macros/nob_NO_translate_macro_test.py
Original file line number Diff line number Diff line change
@@ -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": "<p>Hva synes du om Norge aftan_eftan.vok-a2e</p>",
"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": "<p>Kva synest du om Noreg aftan_eftan.vok-a2e</p>",
"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"], "<p>Kva synest du om Noreg aftan_eftan.vok-a2e</p>"
)
self.assertEqual(item["ednote"], "Kva")
self.assertEqual(
item["associations"]["editor_1"]["description_text"],
"Kva synest du om Noreg",
)

0 comments on commit 9b28ea0

Please sign in to comment.