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

export subject name in item language #1762

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- run: pip install black
- run: pip install black~=23.0
- run: black --check server

flake8:
Expand Down
4 changes: 4 additions & 0 deletions server/planning/output_formatters/json_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from superdesk.publish.formatters import Formatter
from superdesk.utils import json_serialize_datetime_objectId
from .utils import expand_contact_info, get_matching_products
from .json_utils import format_subject


class JsonEventFormatter(Formatter):
Expand Down Expand Up @@ -74,6 +75,9 @@ def _format_item(self, item):

for f in self.remove_fields:
output_item.pop(f, None)

format_subject(output_item)

return output_item

def _get_files_for_publish(self, item):
Expand Down
4 changes: 4 additions & 0 deletions server/planning/output_formatters/json_planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from planning.common import ASSIGNMENT_WORKFLOW_STATE, WORKFLOW_STATE
from superdesk.metadata.item import CONTENT_STATE
from .utils import expand_contact_info, get_matching_products
from .json_utils import format_subject


class JsonPlanningFormatter(Formatter):
Expand Down Expand Up @@ -97,6 +98,9 @@ def _format_item(self, item):

output_item["agendas"] = self._expand_agendas(item)
output_item["products"] = get_matching_products(item)

format_subject(output_item)

return output_item

def _get_coverage_workflow_state(self, assignment_state):
Expand Down
13 changes: 13 additions & 0 deletions server/planning/output_formatters/json_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import Dict
from superdesk.publish.formatters.ninjs_formatter import get_locale_name


def format_subject(item) -> None:
if item.get("subject"):
item["subject"] = [_format_subject(subject, item) for subject in item["subject"]]


def _format_subject(subject: Dict, item: Dict) -> Dict:
if item.get("language") and subject.get("translations"):
subject["name"] = get_locale_name(subject, item["language"])
return subject
7 changes: 6 additions & 1 deletion server/planning/tests/output_formatters/json_event_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class JsonEventTestCase(TestCase):
"event_contact_info": ["5ab491271d41c88e98ad9336"],
"internal_note": "An internal Note",
"_etag": "5ce752b8907fba6b6f56e316f4722436bd3098ba",
"subject": [{"name": "tourism", "qcode": "10006000", "parent": "10000000"}],
"subject": [
{"name": "tourism", "qcode": "10006000", "parent": "10000000", "translations": {"name": {"en": "Tourism"}}}
],
"anpa_category": [{"name": "International News", "qcode": "i"}],
"occur_status": {
"label": "Confirmed",
Expand Down Expand Up @@ -75,6 +77,7 @@ class JsonEventTestCase(TestCase):
"lock_user": None,
"lock_time": None,
"expiry": None,
"language": "en",
}

def setUp(self):
Expand Down Expand Up @@ -138,6 +141,8 @@ def test_formatter(self):
self.assertEqual(output_item.get("internal_note"), "An internal Note")
self.assertEqual(output_item.get("ednote"), "An editorial Note")
self.assertEqual(output_item.get("products"), [{"code": 201, "name": "p-1"}])
self.assertEqual(output_item.get("subject")[0]["name"], "Tourism")
self.assertEqual(output_item.get("language"), "en")

def test_files_publishing(self):
init_app(self.app)
Expand Down
20 changes: 16 additions & 4 deletions server/planning/tests/output_formatters/json_planning_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
lambda self, subscriber: 1,
)
class JsonPlanningTestCase(TestCase):
maxDiff = None
item = {
"_id": "urn:newsml:localhost:2018-04-10T11:06:53.632085:e372d553-9ee1-4e62-8706-fd2eb678ce06",
"_planning_schedule": [
Expand Down Expand Up @@ -50,7 +51,9 @@ class JsonPlanningTestCase(TestCase):
],
"internal_note": "An internal Note",
"_etag": "639e18fc36d9ef6da577702de307aa9506b440e2",
"subject": [{"name": "tourism", "qcode": "10006000", "parent": "10000000"}],
"subject": [
{"name": "tourism", "qcode": "10006000", "parent": "10000000", "translations": {"name": {"en": "Tourism"}}}
],
"description_text": "The description of the event",
"anpa_category": [{"name": "International News", "qcode": "i"}],
"flags": {"marked_for_not_publication": False},
Expand Down Expand Up @@ -80,6 +83,7 @@ class JsonPlanningTestCase(TestCase):
"lock_time": None,
"urgency": 1,
"version_creator": "57bcfc5d1d41c82e8401dcc0",
"language": "en",
}
assignment = [
{
Expand Down Expand Up @@ -125,9 +129,17 @@ class JsonPlanningTestCase(TestCase):
}
]

def setUp(self):
super().setUp()
self.maxDiff = None
def format(self):
with self.app.app_context():
formatter = JsonPlanningFormatter()
output = formatter.format(self.item, {"name": "Test Subscriber"})[0]
output_item = json.loads(output[1])
return output_item

def test_formatting(self):
output_item = self.format()
self.assertEqual("en", output_item["language"])
self.assertEqual("Tourism", output_item["subject"][0]["name"])

def test_formatter_completed_coverage(self):
with self.app.app_context():
Expand Down