Skip to content

Commit

Permalink
markup #197
Browse files Browse the repository at this point in the history
  • Loading branch information
BartSaelen committed Aug 5, 2016
1 parent c6ce435 commit 7cef1b8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
14 changes: 13 additions & 1 deletion atramhasis/mappers.py
Expand Up @@ -2,12 +2,22 @@
"""
Module containing mapping functions used by Atramhasis.
"""
import copy

from skosprovider_sqlalchemy.models import Label, Note, Source, Concept, Collection, Match
from sqlalchemy.orm.exc import NoResultFound


def is_html(value):
"""
Check if a value has html inside. Only tags checked <strong> <em> <a>.
:param value: a string
:return: a boolean
"""
tag_list = ['<strong>', '<em>', '<a>', '</strong>', '</em>', '</a>', '<a']
return any(tag in value for tag in tag_list)


def map_concept(concept, concept_json, skos_manager):
"""
Map a concept from json to the database.
Expand Down Expand Up @@ -60,6 +70,8 @@ def map_concept(concept, concept_json, skos_manager):
notes = concept_json.get('notes', [])
for n in notes:
note = Note(note=n.get('note', ''), notetype_id=n.get('type', ''), language_id=n.get('language', ''))
if is_html(note.note):
note.markup = 'HTML'
concept.notes.append(note)
concept.sources[:] = []
sources = concept_json.get('sources', [])
Expand Down
41 changes: 41 additions & 0 deletions tests/test_mappers.py
Expand Up @@ -81,6 +81,32 @@
"citation": "Atlas."
}]
}
test_json_html={
"narrower": [{"id": 8}, {"id": 7}, {"id": 9}],
"label": "Belgium",
"type": "concept",
"id": 4,
"broader": [{"id": 2}, {"id": 11}],
"related": [{"id": 5}, {"id": 12}],
"member_of": [{"id": 999}],
"labels": [{
"label": "Belgium",
"type": "prefLabel",
"language": "en"
}, {
"label": "België",
"type": "prefLabel",
"language": "nl"
}],
"notes": [{
"note": "een <a href='#'>notitie</a>",
"type": "note",
"language": "nl"
}],
"sources": [{
"citation": "Atlas."
}]
}



Expand Down Expand Up @@ -177,6 +203,7 @@ def test_mapping(self):
self.assertEqual(1, len(result_concept.notes))
self.assertEqual(1, len(result_concept.sources))
self.assertFalse(hasattr(result_concept, 'members'))
self.assertIsNone(result_concept.notes[0].markup)

def test_mapping_collections_filled(self):
label = Label(label='test', labeltype_id='altLabel', language_id='nl')
Expand Down Expand Up @@ -282,3 +309,17 @@ def test_mapping_conceptscheme(self):
self.assertEqual(1, len(result_conceptscheme.labels))
self.assertEqual(1, len(result_conceptscheme.notes))
self.assertEqual(1, len(result_conceptscheme.sources))


def test_mapping_html_note(self):
result_concept = map_concept(self.concept, test_json_html, self.skos_manager)
self.assertIsNotNone(result_concept)
self.assertEqual(3, len(result_concept.narrower_concepts))
self.assertEqual(2, len(result_concept.broader_concepts))
self.assertEqual(2, len(result_concept.related_concepts))
self.assertEqual(1, len(result_concept.member_of))
self.assertEqual(2, len(result_concept.labels))
self.assertEqual(1, len(result_concept.notes))
self.assertEqual(1, len(result_concept.sources))
self.assertFalse(hasattr(result_concept, 'members'))
self.assertEqual('HTML', result_concept.notes[0].markup)

0 comments on commit 7cef1b8

Please sign in to comment.