Skip to content

Commit

Permalink
refactore code via Black
Browse files Browse the repository at this point in the history
  • Loading branch information
devketanpro committed Dec 26, 2023
1 parent 9113970 commit 94d0cbd
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 66 deletions.
46 changes: 28 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 @@ -131,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 @@ -175,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 @@ -251,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"],
}
)
122 changes: 74 additions & 48 deletions server/ntb/tests/publish/ntb_ninjs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
<p><a>test</a>NTBMEDIA TO REMOVE</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 +134,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,20 +234,24 @@ class Ninjs2FormatterTest(TestCase):
"parent": "05000000",
"scheme": None,
"name": "further education",
"qcode": "05002000"
"qcode": "05002000",
},
],
},
},
"extra":{
"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 @@ -346,8 +352,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 @@ -364,41 +370,56 @@ def test_format_item(self):
"infosources": [{"name": "NTB"}],
"copyrightholder": "NTB",
"by": "byline",
"NTBKilde": "test ntb_pub_name"
"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 @@ -417,9 +438,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"]
)

0 comments on commit 94d0cbd

Please sign in to comment.