Skip to content

Commit

Permalink
fix NTB NINJS place countydist
Browse files Browse the repository at this point in the history
SDNTB-795
  • Loading branch information
petrjasek committed Feb 10, 2023
1 parent 3bf2263 commit 543bb32
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
14 changes: 13 additions & 1 deletion server/ntb/publish/ntb_ninjs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict, List
from superdesk import get_resource_service
from superdesk.publish.formatters.ninjs_formatter import NINJSFormatter

from . import utils
Expand All @@ -19,6 +20,7 @@ class NTBNINJSFormatter(NINJSFormatter):
def __init__(self):
super().__init__()
self.format_type = "ntb_ninjs"
self._places = None

def _transform_to_ninjs(self, article, subscriber, recursive=True):
ninjs = super()._transform_to_ninjs(article, subscriber, recursive)
Expand Down Expand Up @@ -112,9 +114,12 @@ def _format_place(self, article) -> List[Dict]:
ninjs_place = {
"name": place.get("name"),
"literal": place.get("qcode"),
"countydist": place.get("ntb_qcode") or place.get("qcode"),
}

cv_place = self.places.get(place["qcode"])
if cv_place and cv_place.get("ntb_qcode"):
ninjs_place["countydist"] = cv_place["ntb_qcode"]

if place.get("altids") and place["altids"].get("wikidata"):
ninjs_place["uri"] = "http://www.wikidata.org/entity/{}".format(place["altids"]["wikidata"])
places.append(ninjs_place)
Expand Down Expand Up @@ -168,3 +173,10 @@ def format_subjects(self, ninjs):
}
)
return subjects

@property
def places(self):
if self._places is None:
places = get_resource_service("vocabularies").get_items("place_custom")
self._places = {p["qcode"]: p for p in places}
return self._places
11 changes: 6 additions & 5 deletions server/ntb/tests/publish/ntb_ninjs_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import pathlib

from unittest import mock
from ntb.publish.ntb_ninjs import NTBNINJSFormatter
Expand Down Expand Up @@ -101,19 +102,15 @@ class Ninjs2FormatterTest(TestCase):
"aliases": ["Gjerdrum kommune"],
"scheme": "place_custom",
"name": "Gjerdrum",
"ntb_qcode": "Gjerdrum",
"description": "kommune i Viken",
"qcode": "b564b1e1-1a99-324e-b643-88e5398305c6",
"source": "imatrics",
"original_source": "1013",
},
{
"scheme": "place_custom",
"parent": None,
"ntb_parent": None,
"name": "Global",
"qcode": "Global",
"ntb_qcode": "County",
},
],
"object": [
Expand Down Expand Up @@ -178,6 +175,11 @@ def test_format_type(self):
self.assertEqual("ntb_ninjs", self.formatter.format_type)

def test_format_item(self):
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":
self.app.data.insert("vocabularies", [cv])
seq, doc = self.formatter.format(self.article, {"name": "Test Subscriber"})[0]
ninjs = json.loads(doc)
expected_item = {
Expand Down Expand Up @@ -259,7 +261,6 @@ def test_format_item(self):
{
"name": "Global",
"literal": "Global",
"countydist": "County",
},
],
"taglines": [
Expand Down

0 comments on commit 543bb32

Please sign in to comment.