Completed
Push — master ( e370ca...f86a7d )
by Bart
11s
created

is_html()   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
cc 2
rs 9.6666
1
# -*- coding: utf-8 -*-
2
"""
3
Module containing mapping functions used by Atramhasis.
4
"""
5
6
from skosprovider_sqlalchemy.models import Label, Note, Source, Concept, Collection, Match
7
from sqlalchemy.orm.exc import NoResultFound
8
9
10
def is_html(value):
11
    """
12
    Check if a value has html inside. Only tags checked <strong> <em> <a>.
13
14
    :param value: a string
15
    :return: a boolean
16
    """
17
    tag_list = ['<strong>', '<em>', '<a>', '</strong>', '</em>', '</a>', '<a']
18
    return any(tag in value for tag in tag_list)
19
20
21
def map_concept(concept, concept_json, skos_manager):
22
    """
23
    Map a concept from json to the database.
24
25
    :param skosprovider_sqlalchemy.models.Thing concept: A concept or
26
        collection as known to the database.
27
    :param dict concept_json: A dict representing the json sent to our REST
28
        service.
29
    :param skos_manager: A skos_manager to acces db operations
30
    :returns: The :class:`skosprovider_sqlalchemy.models.Thing` enhanced
31
        with the information from the json object.
32
    """
33
    concept_json_type = concept_json.get('type', None)
34
    if concept.type != concept_json_type:
35
36
        if concept_json_type == 'concept':
37
            members = concept.members
38
            concept = skos_manager.change_type(concept,
39
                                               concept.concept_id,
40
                                               concept.conceptscheme_id,
41
                                               concept_json_type)
42
            for member in members:
43
                if member.type == 'concept':
44
                    concept.narrower_concepts.add(member)
45
                elif member.type == 'collection':
46
                    concept.narrower_collections.add(member)
47
        elif concept_json_type == 'collection':
48
            narrower_concepts = concept.narrower_concepts
49
            narrower_collections = concept.narrower_collections
50
            concept = skos_manager.change_type(concept,
51
                                               concept.concept_id,
52
                                               concept.conceptscheme_id,
53
                                               concept_json_type)
54
            for narrower_concept in narrower_concepts:
55
                concept.members.add(narrower_concept)
56
            for narrower_collection in narrower_collections:
57
                concept.members.add(narrower_collection)
58
    elif concept_json_type == 'collection':
59
        concept.members.clear()
60
    elif concept_json_type == 'concept':
61
        concept.narrower_collections.clear()
62
        concept.narrower_concepts.clear()
63
    if concept.type in ('concept', 'collection'):
64
        concept.labels[:] = []
65
        labels = concept_json.get('labels', [])
66
        for l in labels:
67
            label = Label(label=l.get('label', ''), labeltype_id=l.get('type', ''), language_id=l.get('language', ''))
68
            concept.labels.append(label)
69
        concept.notes[:] = []
70
        notes = concept_json.get('notes', [])
71
        for n in notes:
72
            note = Note(note=n.get('note', ''), notetype_id=n.get('type', ''), language_id=n.get('language', ''))
73
            if is_html(note.note):
74
                note.markup = 'HTML'
75
            concept.notes.append(note)
76
        concept.sources[:] = []
77
        sources = concept_json.get('sources', [])
78
        for s in sources:
79
            source = Source(citation=s.get('citation', ''))
80
            concept.sources.append(source)
81
82
        concept.member_of.clear()
83
        member_of = concept_json.get('member_of', [])
84
        for memberof in member_of:
85
            try:
86
                memberof_collection = skos_manager.get_thing(
87
                    concept_id=memberof['id'],
88
                    conceptscheme_id=concept.conceptscheme_id)
89
            except NoResultFound:
90
                memberof_collection = Collection(concept_id=memberof['id'], conceptscheme_id=concept.conceptscheme_id)
91
            concept.member_of.add(memberof_collection)
92
93
        if concept.type == 'concept':
94
            concept.related_concepts.clear()
95
            related = concept_json.get('related', [])
96
            for related in related:
97
                try:
98
                    related_concept = skos_manager.get_thing(
99
                        concept_id=related['id'],
100
                        conceptscheme_id=concept.conceptscheme_id)
101
                except NoResultFound:
102
                    related_concept = Concept(concept_id=related['id'], conceptscheme_id=concept.conceptscheme_id)
103
                concept.related_concepts.add(related_concept)
104
105
            concept.broader_concepts.clear()
106
            broader = concept_json.get('broader', [])
107
            for broader in broader:
108
                try:
109
                    broader_concept = skos_manager.get_thing(
110
                        concept_id=broader['id'],
111
                        conceptscheme_id=concept.conceptscheme_id)
112
                except NoResultFound:
113
                    broader_concept = Concept(concept_id=broader['id'], conceptscheme_id=concept.conceptscheme_id)
114
                concept.broader_concepts.add(broader_concept)
115
            narrower = concept_json.get('narrower', [])
116
            for narrower in narrower:
117
                try:
118
                    narrower_concept = skos_manager.get_thing(
119
                        concept_id=narrower['id'],
120
                        conceptscheme_id=concept.conceptscheme_id)
121
                except NoResultFound:
122
                    narrower_concept = Concept(concept_id=narrower['id'], conceptscheme_id=concept.conceptscheme_id)
123
                concept.narrower_concepts.add(narrower_concept)
124
125
            matches = []
126
            matchdict = concept_json.get('matches', {})
127
            for type in matchdict:
128
                db_type = type + "Match"
129
                matchtype = skos_manager.get_match_type(db_type)
130
                for uri in matchdict[type]:
131
                    concept_id = concept_json.get('id', -1)
132
                    try:
133
                        match = skos_manager.get_match(uri=uri, matchtype_id=matchtype.name,
134
                                                       concept_id=concept_id)
135
                    except NoResultFound:
136
                        match = Match()
137
                        match.matchtype = matchtype
138
                        match.uri = uri
139
                    matches.append(match)
140
            concept.matches = matches
141
142
            narrower_collections = concept_json.get('subordinate_arrays', [])
143
            for narrower in narrower_collections:
144
                try:
145
                    narrower_collection = skos_manager.get_thing(
146
                        concept_id=narrower['id'],
147
                        conceptscheme_id=concept.conceptscheme_id)
148
                except NoResultFound:
149
                    narrower_collection = Collection(concept_id=narrower['id'],
150
                                                     conceptscheme_id=concept.conceptscheme_id)
151
                concept.narrower_collections.add(narrower_collection)
152
153
        if concept.type == 'collection':
154
            members = concept_json.get('members', [])
155
            for member in members:
156
                try:
157
                    member_concept = skos_manager.get_thing(
158
                        concept_id=member['id'],
159
                        conceptscheme_id=concept.conceptscheme_id)
160
                except NoResultFound:
161
                    member_concept = Concept(concept_id=member['id'], conceptscheme_id=concept.conceptscheme_id)
162
                concept.members.add(member_concept)
163
164
            concept.broader_concepts.clear()
165
            broader_concepts = concept_json.get('superordinates', [])
166
            for broader in broader_concepts:
167
                try:
168
                    broader_concept = skos_manager.get_thing(
169
                        concept_id=broader['id'],
170
                        conceptscheme_id=concept.conceptscheme_id)
171
                except NoResultFound:
172
                    broader_concept = Concept(concept_id=broader['id'], conceptscheme_id=concept.conceptscheme_id)
173
                concept.broader_concepts.add(broader_concept)
174
    return concept
175
176
177
def map_conceptscheme(conceptscheme, conceptscheme_json):
178
    """
179
    Map a conceptscheme from json to the database.
180
181
    :param skosprovider_sqlalchemy.models.ConceptScheme conceptscheme: A conceptscheme as known to the database.
182
    :param dict conceptscheme_json: A dict representing the json sent to our REST
183
        service.
184
    :returns: The :class:`skosprovider_sqlalchemy.models.ConceptScheme` enhanced
185
        with the information from the json object.
186
    """
187
    conceptscheme.labels[:] = []
188
    labels = conceptscheme_json.get('labels', [])
189
    for l in labels:
190
        label = Label(label=l.get('label', ''), labeltype_id=l.get('type', ''), language_id=l.get('language', ''))
191
        conceptscheme.labels.append(label)
192
    conceptscheme.notes[:] = []
193
    notes = conceptscheme_json.get('notes', [])
194
    for n in notes:
195
        note = Note(note=n.get('note', ''), notetype_id=n.get('type', ''), language_id=n.get('language', ''))
196
        conceptscheme.notes.append(note)
197
    conceptscheme.sources[:] = []
198
    sources = conceptscheme_json.get('sources', [])
199
    for s in sources:
200
        source = Source(citation=s.get('citation', ''))
201
        conceptscheme.sources.append(source)
202
    return conceptscheme
203