Skip to content

Commit

Permalink
Merge pull request #97 from devketanpro/fix_ritzau_mapping
Browse files Browse the repository at this point in the history
update section cv mapping to item[subject] [BOR-28]
  • Loading branch information
devketanpro authored Nov 27, 2024
2 parents d96f362 + 66562f8 commit a76a7db
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
23 changes: 14 additions & 9 deletions server/borsen/io/feed_parsers/ritzau.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,35 @@ class RitzauFeedParser(BaseRitzauFeedParser):
def __init__(self):
super().__init__()
self.default_mapping.update(
{"anpa_category": {"xpath": "sectionID/text()", "filter": self.section_category_filter}}
{
"subject": {
"xpath": "sectionID/text()",
"filter": self.section_category_filter,
}
}
)

def do_mapping(self, item, item_xml, setting_param_name=None, namespaces=None):
item = super().do_mapping(item, item_xml, setting_param_name, namespaces)

# Add Default Category:
sections_cv_items = self.get_cv_items("sections")
default_section = [section for section in sections_cv_items if section["qcode"] == "generelt"]
categories_cv_items = self.get_cv_items("categories")
default_section = [section for section in categories_cv_items if str(section["qcode"]) == "generelt"]

if default_section:
item["anpa_category"].append(default_section[0])
item.setdefault("anpa_category", []).append(default_section[0])

return item

def section_category_filter(self, category):
voc_categories = self.get_cv_items("sections")
if voc_categories:
categories_cv = {str(i["ritzau_section_id"]): i for i in voc_categories if "ritzau_section_id" in i}
cv_sections_items = self.get_cv_items("sections")
if cv_sections_items:
section_cv = {str(i["ritzau_section_id"]): i for i in cv_sections_items if "ritzau_section_id" in i}
else:
categories_cv = {}
section_cv = {}

populated_categories = []
match = categories_cv.get(category)
match = section_cv.get(str(category))
if match:
populated_categories.append(match)
return populated_categories
Expand Down
15 changes: 11 additions & 4 deletions server/tests/io/feed_parsers/ritzau_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class BaseRitzauTestCase(TestCase):
def setUp(self):
super().setUp()
voc_file = os.path.join(
os.path.abspath(os.path.dirname(os.path.dirname(borsen.__file__))), "tests/io/fixtures", "vocabularies.json"
os.path.abspath(os.path.dirname(os.path.dirname(borsen.__file__))),
"tests/io/fixtures",
"vocabularies.json",
)
AppPopulateCommand().run(voc_file)
for key in dir(settings):
Expand Down Expand Up @@ -57,7 +59,7 @@ def test_can_parse(self):
def test_content(self):
item = self.item
self.assertEqual(
item["anpa_category"],
item["subject"],
[
{
"name": "Sandbox",
Expand All @@ -66,14 +68,19 @@ def test_content(self):
"translations": {"name": {"da": "Sandbox"}},
"ritzau_section_id": "4",
"scheme": "sections",
},
}
],
)
self.assertEqual(
item["anpa_category"],
[
{
"name": "Generelt",
"qcode": "generelt",
"subject": None,
"translations": {"name": {"da": "Generelt"}},
"ritzau_section_id": "",
"scheme": "sections",
"scheme": "categories",
},
],
)
Expand Down
28 changes: 14 additions & 14 deletions server/tests/io/fixtures/vocabularies.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"is_active": true,
"name": "foo",
"qcode": "f1",
"ritzau_section_id": 1,
"ritzau_section_id": "1",
"subject": "",
"translations": {
"name": {
Expand All @@ -24,14 +24,26 @@
"is_active": true,
"name": "bar",
"qcode": "f2",
"ritzau_section_id": 2,
"ritzau_section_id": "2",
"subject": "",
"translations": {
"name": {
"fr": "article-fr",
"es": "article-es"
}
}
},
{
"is_active":true,
"name":"Generelt",
"qcode":"generelt",
"subject":null,
"translations":{
"name":{
"da":"Generelt"
}
},
"ritzau_section_id":""
}
],
"schema": {
Expand Down Expand Up @@ -110,18 +122,6 @@
}
},
"ritzau_section_id": ""
},
{
"is_active":true,
"name":"Generelt",
"qcode":"generelt",
"subject":null,
"translations":{
"name":{
"da":"Generelt"
}
},
"ritzau_section_id":""
}
],
"schema":{
Expand Down

0 comments on commit a76a7db

Please sign in to comment.