Skip to content

Commit

Permalink
Missing fields in NTB NINJS [SDNTB-861] (#468)
Browse files Browse the repository at this point in the history
* add NTBKilde attribute in ninjs output [SDNTB-861]

* udpate tests

* refactore code via Black

* update test for copyright text

* reformat code via black

* fix flake
  • Loading branch information
devketanpro authored Dec 28, 2023
1 parent b23c60a commit c9ce84f
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 74 deletions.
50 changes: 32 additions & 18 deletions server/ntb/publish/ntb_ninjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def convert_dicts_to_lists(ninjs):
for field in ("renditions", "associations"):
if ninjs.get(field):
ninjs[field] = [
format_array_value(value, key)
for key, value
in ninjs[field].items()
format_array_value(value, key) for key, value in ninjs[field].items()
]
elif field in ninjs:
ninjs.pop(field)
Expand Down Expand Up @@ -77,6 +75,9 @@ def _transform_to_ninjs(self, article, subscriber, recursive=True):
if ninjs.get("guid"):
ninjs.setdefault("uri", ninjs["guid"])

if ninjs.get("extra", {}).get("ntb_pub_name"):
ninjs["NTBKilde"] = ninjs["extra"]["ntb_pub_name"]

# removed items which mapped according to Ninjs v2 properties
ninjs_properties = [
"headlines",
Expand Down Expand Up @@ -116,6 +117,7 @@ def _transform_to_ninjs(self, article, subscriber, recursive=True):
"rightsinfo",
"service",
"infosources",
"NTBKilde",
]

for key in list(ninjs.keys()):
Expand All @@ -127,10 +129,12 @@ def _transform_to_ninjs(self, article, subscriber, recursive=True):
]

if article.get("family_id"):
ninjs["altids"].extend([
{"role": "NTB-ID", "value": utils.get_ntb_id(article)},
{"role": "DOC-ID", "value": utils.get_doc_id(article)},
])
ninjs["altids"].extend(
[
{"role": "NTB-ID", "value": utils.get_ntb_id(article)},
{"role": "DOC-ID", "value": utils.get_doc_id(article)},
]
)

if article.get("assignment_id"):
self._format_planning_ids(ninjs, article)
Expand Down Expand Up @@ -171,7 +175,9 @@ def _format_place(self, article) -> List[Dict]:
ninjs_place["state-prov"] = cv_place["ntb_parent"]

if place.get("altids") and place["altids"].get("wikidata"):
ninjs_place["uri"] = "http://www.wikidata.org/entity/{}".format(place["altids"]["wikidata"])
ninjs_place["uri"] = "http://www.wikidata.org/entity/{}".format(
place["altids"]["wikidata"]
)
ninjs_place["literal"] = place["altids"]["wikidata"]
places.append(ninjs_place)
return places
Expand Down Expand Up @@ -247,15 +253,23 @@ def places(self):
return getattr(g, "_places")

def _format_planning_ids(self, ninjs, article):
assignment = get_resource_service("assignments").find_one(req=None, _id=article["assignment_id"])
assignment = get_resource_service("assignments").find_one(
req=None, _id=article["assignment_id"]
)
if assignment and assignment.get("planning_item"):
ninjs.setdefault("altids", []).append({
"role": "PLANNING-ID",
"value": assignment["planning_item"],
})
planning = get_resource_service("planning").find_one(req=None, _id=assignment["planning_item"])
ninjs.setdefault("altids", []).append(
{
"role": "PLANNING-ID",
"value": assignment["planning_item"],
}
)
planning = get_resource_service("planning").find_one(
req=None, _id=assignment["planning_item"]
)
if planning and planning.get("event_item"):
ninjs["altids"].append({
"role": "EVENT-ID",
"value": planning["event_item"],
})
ninjs["altids"].append(
{
"role": "EVENT-ID",
"value": planning["event_item"],
}
)
126 changes: 79 additions & 47 deletions server/ntb/tests/publish/ntb_ninjs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
<h3>intermediate line</h3>
<p>this element should have a txt class</p>
<p><a>test</a>NTBMEDIA TO REMOVE</p>
<p>(©MyCompany2023)</p>
""".strip()

with open(pathlib.Path(__file__).parent.joinpath("fixtures", "text-item-with-table.json")) as f:
with open(
pathlib.Path(__file__).parent.joinpath("fixtures", "text-item-with-table.json")
) as f:
text_item_with_table = json.load(f)


Expand Down Expand Up @@ -132,15 +136,15 @@ class Ninjs2FormatterTest(TestCase):
"qcode": "Global",
},
{
'aliases': [],
'altids': {
'imatrics': '34017822-d341-3826-ab94-71d226d639c4',
"aliases": [],
"altids": {
"imatrics": "34017822-d341-3826-ab94-71d226d639c4",
},
'name': 'Genève',
'original_source': None,
'qcode': '34017822-d341-3826-ab94-71d226d639c4',
'scheme': 'place_custom',
'source': 'imatrics',
"name": "Genève",
"original_source": None,
"qcode": "34017822-d341-3826-ab94-71d226d639c4",
"scheme": "place_custom",
"source": "imatrics",
},
],
"object": [
Expand Down Expand Up @@ -232,17 +236,24 @@ class Ninjs2FormatterTest(TestCase):
"parent": "05000000",
"scheme": None,
"name": "further education",
"qcode": "05002000"
"qcode": "05002000",
},
],
},
},
"extra": {
"ntb_pub_name": "test ntb_pub_name",
},
}

def setUp(self):
super().setUp()
self.formatter = NTBNINJSFormatter()
with open(pathlib.Path(__file__).parent.parent.parent.parent.joinpath("data/vocabularies.json")) as json_file:
with open(
pathlib.Path(__file__).parent.parent.parent.parent.joinpath(
"data/vocabularies.json"
)
) as json_file:
json_cvs = json.load(json_file)
for cv in json_cvs:
if cv.get("_id") == "place_custom":
Expand Down Expand Up @@ -310,8 +321,8 @@ def test_format_item(self):
],
"bodies": [
{
"charcount": 132,
"wordcount": 25,
"charcount": 148,
"wordcount": 26,
"value": TEST_BODY_EXPECTED,
"contenttype": "text/html",
}
Expand Down Expand Up @@ -343,8 +354,8 @@ def test_format_item(self):
"literal": "Global",
},
{
"name": 'Genève',
"literal": '34017822-d341-3826-ab94-71d226d639c4',
"name": "Genève",
"literal": "34017822-d341-3826-ab94-71d226d639c4",
},
],
"taglines": [
Expand All @@ -361,40 +372,56 @@ def test_format_item(self):
"infosources": [{"name": "NTB"}],
"copyrightholder": "NTB",
"by": "byline",
"NTBKilde": "test ntb_pub_name",
}

self.assertEqual(ninjs, expected_item)

self.assertEqual(assoc, [{
"name": "featuremedia",
"altids": [
{"role": "GUID", "value": "test_id"},
],
"descriptions": [
{"contenttype": "text/plain", "value": "test feature media"},
],
"uri": "test_id",
"headlines": [
{"contenttype": "text/plain", "value": "feature headline"},
],
"pubstatus": "usable",
"taglines": [],
"type": "picture",
"version": "1",
"versioncreated": "2023-03-01T12:10:00+0000",
"subjects": [
{"name": "further education", "uri": "topics:05002000"},
self.assertEqual(
assoc,
[
{
"name": "featuremedia",
"altids": [
{"role": "GUID", "value": "test_id"},
],
"descriptions": [
{"contenttype": "text/plain", "value": "test feature media"},
],
"uri": "test_id",
"headlines": [
{"contenttype": "text/plain", "value": "feature headline"},
],
"pubstatus": "usable",
"taglines": [],
"type": "picture",
"version": "1",
"versioncreated": "2023-03-01T12:10:00+0000",
"subjects": [
{"name": "further education", "uri": "topics:05002000"},
],
"copyrightholder": "NTB",
}
],
"copyrightholder": "NTB",
}])
)

def test_planning_ids(self):
self.app.data.insert("assignments", [
{"_id": "assignment-id", "coverage_item": self.article["guid"], "planning_item": "planning-id"},
])
self.app.data.insert("planning", [
{"_id": "planning-id", "event_item": "event-id"},
])
self.app.data.insert(
"assignments",
[
{
"_id": "assignment-id",
"coverage_item": self.article["guid"],
"planning_item": "planning-id",
},
],
)
self.app.data.insert(
"planning",
[
{"_id": "planning-id", "event_item": "event-id"},
],
)

ninjs = self.format()

Expand All @@ -413,9 +440,14 @@ def test_empty_assocations_renditions(self):
assert "associations" not in ninjs, ninjs.get("associations")

def test_publish_table(self):
ninjs = self.format({
"fields_meta": text_item_with_table["fields_meta"],
"body_html": text_item_with_table["body_html"],
})
ninjs = self.format(
{
"fields_meta": text_item_with_table["fields_meta"],
"body_html": text_item_with_table["body_html"],
}
)
assert "<table>" in ninjs["bodies"][0]["value"]
assert text_item_with_table["body_html"].replace("&nbsp;", " ") == ninjs["bodies"][0]["value"]
assert (
text_item_with_table["body_html"].replace("&nbsp;", " ")
== ninjs["bodies"][0]["value"]
)
19 changes: 10 additions & 9 deletions server/ntb/tests/publish/ntb_nitf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
</video>
<figcaption>SCRIPT TO FOLLOW</figcaption>
</figure><!-- EMBED END Video {id: "embedded10005446043"} -->
<p>(©MyCompany2023)</p>
"""
)
ARTICLE = {
Expand Down Expand Up @@ -317,15 +318,15 @@
"original_source": "1013",
},
{
'aliases': [],
'altids': {
'imatrics': '34017822-d341-3826-ab94-71d226d639c4',
"aliases": [],
"altids": {
"imatrics": "34017822-d341-3826-ab94-71d226d639c4",
},
'name': 'Genève',
'original_source': None,
'qcode': '34017822-d341-3826-ab94-71d226d639c4',
'scheme': 'place_custom',
'source': 'imatrics',
"name": "Genève",
"original_source": None,
"qcode": "34017822-d341-3826-ab94-71d226d639c4",
"scheme": "place_custom",
"source": "imatrics",
},
],
"object": [
Expand Down Expand Up @@ -442,7 +443,7 @@ def test_pubdata(self):
pubdata = self.nitf_xml.find("head/pubdata")
expected = NOW.astimezone(self.tz).strftime("%Y%m%dT%H%M%S")
self.assertEqual(pubdata.get("date.publication"), expected)
self.assertEqual(pubdata.get("item-length"), "121")
self.assertEqual(pubdata.get("item-length"), "137")
self.assertEqual(pubdata.get("unit-of-measure"), "character")

def test_dateline(self):
Expand Down

0 comments on commit c9ce84f

Please sign in to comment.