示例#1
0
    def add_concept(self):
        '''
        Add a new concept to a conceptscheme

        :raises atramhasis.errors.ValidationError: If the provided json can't be validated
        '''
        validated_json_concept = self._validate_concept(self._get_json_body(), self.provider.conceptscheme_id)
        cid = self.db.query(
            func.max(Thing.concept_id)
        ).filter_by(conceptscheme_id=self.provider.conceptscheme_id).first()[0]
        if not cid:
            cid = 0
        cid += 1
        if validated_json_concept['type'] == 'concept':
            concept = Concept()
        else:
            concept = Collection()
        concept.concept_id = cid
        concept.conceptscheme_id = self.provider.conceptscheme_id
        concept.uri = self.provider.uri_generator.generate(id=cid)
        map_concept(concept, validated_json_concept, self.request.db)
        self.db.add(concept)

        invalidate_scheme_cache(self.scheme_id)

        self.request.response.status = '201'
        self.request.response.location = self.request.route_path(
            'skosprovider.c', scheme_id=self.scheme_id, c_id=concept.concept_id)
        return from_thing(concept)
示例#2
0
    def add_concept(self):
        """
        Add a new concept to a conceptscheme

        :raises atramhasis.errors.ValidationError: If the provided json can't be validated
        """
        validated_json_concept = self._validate_concept(self._get_json_body(), self.provider.conceptscheme_id)
        cid = self.skos_manager.get_next_cid(self.provider.conceptscheme_id)
        if not cid:
            cid = 0
        cid += 1
        if validated_json_concept["type"] == "concept":
            concept = Concept()
        else:
            concept = Collection()
        concept.concept_id = cid
        concept.conceptscheme_id = self.provider.conceptscheme_id
        concept.uri = self.provider.uri_generator.generate(id=concept.concept_id)
        map_concept(concept, validated_json_concept, self.skos_manager)
        concept = self.skos_manager.save(concept)

        invalidate_scheme_cache(self.scheme_id)

        self.request.response.status = "201"
        self.request.response.location = self.request.route_path(
            "skosprovider.c", scheme_id=self.scheme_id, c_id=concept.concept_id
        )
        return from_thing(concept)
 def change_type(self, thing, concept_id, conceptscheme_id, new_type):
     self.delete_thing(thing)
     self.session.flush()
     thing = Concept() if new_type == 'concept' else Collection()
     thing.type = new_type
     thing.concept_id = concept_id
     thing.conceptscheme_id = conceptscheme_id
     self.save(thing)
     return thing
示例#4
0
 def test_update_last_visited_concepts_max(self):
     for id in range(50):
         c = Concept()
         c.id = id
         c.labels = [Label('test', language_id='en-us')]
         update_last_visited_concepts(self.request, {'label': c.label(), 'url': 'http://test.test/{0}'.format(id)})
     self.assertEqual(4, len(self.request.session['last_visited']))
     last = self.request.session['last_visited'].pop()
     self.assertEqual('http://test.test/49', last['url'])
示例#5
0
def get_all_mock():
    a_concept = Concept(concept_id=7895, conceptscheme_id=1, type='concept')
    a_labels = [Label(label='De Paardekastanje', language_id='nl'), Label(label='The Chestnut', language_id='en'),
                Label(label='la châtaigne', language_id='fr')]
    a_concept.labels = a_labels
    b_concept = Concept(concept_id=9863, conceptscheme_id=1, type='concept')
    b_labels = [Label(label='test', language_id='nl')]
    b_concept.labels = b_labels
    all_mock = Mock(return_value=[a_concept, b_concept])
    return all_mock
示例#6
0
def filter_by_mock_concept(**kwargs):
    filter_mock = Mock()
    concept_id = None
    conceptscheme_id = None
    if 'concept_id' in kwargs:
        concept_id = kwargs['concept_id']
    if 'conceptscheme_id' in kwargs:
        conceptscheme_id = kwargs['conceptscheme_id']
    if concept_id == '1':
        c = Concept(concept_id=concept_id, conceptscheme_id=conceptscheme_id)
        c.type = 'concept'
        filter_mock.one = Mock(return_value=c)
    elif concept_id == '3':
        c = Collection(concept_id=concept_id, conceptscheme_id=conceptscheme_id)
        c.type = 'collection'
        filter_mock.one = Mock(return_value=c)
    elif concept_id == '555':
        c = Thing(concept_id=concept_id, conceptscheme_id=conceptscheme_id)
        filter_mock.one = Mock(return_value=c)
    elif concept_id == '666':
        raise NoResultFound

    a_concept = Concept(concept_id=7895, conceptscheme_id=conceptscheme_id, type='concept')
    a_labels = [Label(label='De Paardekastanje', language_id='nl'), Label(label='The Chestnut', language_id='en'),
                Label(label='la châtaigne', language_id='fr')]
    a_concept.labels = a_labels
    b_concept = Concept(concept_id=9863, conceptscheme_id=conceptscheme_id, type='concept')
    b_labels = [Label(label='test', language_id='nl')]
    b_concept.labels = b_labels
    filter_mock.all = Mock(return_value=[a_concept, b_concept])
    return filter_mock
示例#7
0
 def test_update_last_visited_concepts_max(self):
     for id in range(50):
         c = Concept()
         c.id = id
         c.labels = [Label('test', language_id='en-us')]
         update_last_visited_concepts(
             self.request, {
                 'label': c.label(),
                 'url': 'http://test.test/{0}'.format(id)
             })
     self.assertEqual(4, len(self.request.session['last_visited']))
     last = self.request.session['last_visited'].pop()
     self.assertEqual('http://test.test/49', last['url'])
 def _get_concept(self):
     from skosprovider_sqlalchemy.models import Concept
     return Concept(
         id=1,
         concept_id=1,
         uri='urn:x-skosprovider:birds:8300'
     )
 def _get_concept(self):
     from skosprovider_sqlalchemy.models import Concept, Label
     return Concept(
         id=2,
         concept_id=456,
         labels=[Label('Cathedrals', 'prefLabel', 'en')]
     )
示例#10
0
 def test_update_last_visited_concepts(self):
     c = Concept()
     c.id = 2
     c.labels = [Label('test', language_id='en-us')]
     update_last_visited_concepts(self.request, {
         'label': c.label(),
         'url': 'http://test.test/{0}'.format(55)
     })
     c = Concept()
     c.id = 33
     c.labels = [Label('test', language_id='nl-be')]
     update_last_visited_concepts(self.request, {
         'label': c.label(),
         'url': 'http://test.test/{0}'.format(2)
     })
     self.assertEqual(2, len(self.request.session['last_visited']))
示例#11
0
 def test_update_last_visited_concepts(self):
     c = Concept()
     c.id= 2
     c.labels = [Label('test', language_id='en-us')]
     update_last_visited_concepts(self.request, {'label': c.label(), 'url': 'http://test.test/{0}'.format(55)})
     c = Concept()
     c.id= 33
     c.labels = [Label('test', language_id='nl-be')]
     update_last_visited_concepts(self.request, {'label': c.label(), 'url': 'http://test.test/{0}'.format(2)})
     self.assertEqual(2, len(self.request.session['last_visited']))
示例#12
0
    def setUp(self):
        conceptscheme = ConceptScheme()
        conceptscheme.uri = 'urn:x-atramhasis-demo'
        conceptscheme.id = 1
        self.concept = Concept()
        self.concept.type = 'concept'
        self.concept.id = 11
        self.concept.concept_id = 101
        self.concept.uri = 'urn:x-atramhasis-demo:TREES:101'
        self.concept.conceptscheme_id = 1
        self.concept.conceptscheme = conceptscheme

        notes = []
        note = Note(note='test note', notetype_id='example', language_id='en')
        note2 = Note(note='note def',
                     notetype_id='definition',
                     language_id='en')
        notes.append(note)
        notes.append(note2)
        self.concept.notes = notes

        labels = []
        label = Label(label='een label',
                      labeltype_id='prefLabel',
                      language_id='nl')
        label2 = Label(label='other label',
                       labeltype_id='altLabel',
                       language_id='en')
        label3 = Label(label='and some other label',
                       labeltype_id='altLabel',
                       language_id='en')
        labels.append(label)
        labels.append(label2)
        labels.append(label3)
        self.concept.labels = labels

        matches = []
        match1 = Match()
        match1.uri = 'urn:test'
        match1.concept = self.concept
        match1.matchtype = MatchType(name='closeMatch', description='')
        match2 = Match()
        match2.uri = 'urn:test'
        match2.concept = self.concept
        match2.matchtype = MatchType(name='closeMatch', description='')
        matches.append(match1)
        matches.append(match2)
        self.matches = matches

        self.collection = Collection()
        self.collection.type = 'collection'
        self.collection.id = 12
        self.collection.concept_id = 102
        self.collection.uri = 'urn:x-atramhasis-demo:TREES:102'
        self.collection.conceptscheme_id = 1
        self.collection.conceptscheme = conceptscheme
示例#13
0
    def setUp(self):
        self.concept = Concept()
        self.concept.type = 'concept'
        self.concept.id = 11
        self.concept.concept_id = 101
        self.concept.uri = 'urn:x-atramhasis-demo:TREES:101'
        self.concept.conceptscheme_id = 1

        notes = []
        note = Note(note='test note', notetype_id='example', language_id='en')
        note2 = Note(note='note def',
                     notetype_id='definition',
                     language_id='en')
        notes.append(note)
        notes.append(note2)
        self.concept.notes = notes

        labels = []
        label = Label(label='een label',
                      labeltype_id='prefLabel',
                      language_id='nl')
        label2 = Label(label='other label',
                       labeltype_id='altLabel',
                       language_id='en')
        label3 = Label(label='and some other label',
                       labeltype_id='altLabel',
                       language_id='en')
        labels.append(label)
        labels.append(label2)
        labels.append(label3)
        self.concept.labels = labels

        matches = []
        match = Match()
        match.matchtype = MatchType(name='closeMatch', description='test')
        match.uri = 'urn:somethingelse:st1'
        matches.append(match)
        match2 = Match()
        match2.matchtype = MatchType(name='closeMatch', description='test')
        match2.uri = 'urn:somethingelse:st2'
        matches.append(match2)
        match3 = Match()
        match3.matchtype = MatchType(name='exactMatch', description='test')
        match3.uri = 'urn:something:thingy'
        matches.append(match3)
        self.concept.matches = matches

        self.collection = Collection()
        self.collection.type = 'collection'
        self.collection.id = 12
        self.collection.concept_id = 102
        self.collection.uri = 'urn:x-atramhasis-demo:TREES:102'
        self.collection.conceptscheme_id = 1

        self.concept.member_of.add(self.collection)
        self.collection.members.add(self.concept)
示例#14
0
 def setUp(self):
     self.config = testing.setUp()
     self.request = testing.DummyRequest()
     self.request.db = db(self.request)
     self.concept = Concept()
     self.concept.concept_id = 1
     self.concept.conceptscheme_id = 1
     self.collection = Collection()
     self.collection.concept_id = 0
     self.collection.conceptscheme_id = 1
示例#15
0
 def setUp(self):
     self.skos_manager = SkosManager(session_maker())
     self.concept = Concept()
     self.concept.concept_id = 1
     self.concept.conceptscheme_id = 1
     self.collection = Collection()
     self.collection.concept_id = 0
     self.collection.conceptscheme_id = 1
     self.conceptscheme = ConceptScheme()
     self.conceptscheme.id = 1
     member_concept_1 = Concept()
     member_concept_1.concept_id = 5
     member_concept_1.conceptscheme_id = 1
     member_concept_2 = Collection()
     member_concept_2.concept_id = 6
     member_concept_2.conceptscheme_id = 1
     self.collection.members.add(member_concept_1)
     self.collection.members.add(member_concept_2)
     self.concept.narrower_concepts.add(member_concept_1)
     self.concept.narrower_collections.add(member_concept_2)
示例#16
0
 def setUp(self):
     self.skos_manager = SkosManager(session_maker())
     self.concept = Concept()
     self.concept.concept_id = 1
     self.concept.conceptscheme_id = 1
     self.collection = Collection()
     self.collection.concept_id = 0
     self.collection.conceptscheme_id = 1
     self.conceptscheme = ConceptScheme()
     self.conceptscheme.id = 1
     member_concept_1 = Concept()
     member_concept_1.concept_id = 5
     member_concept_1.conceptscheme_id = 1
     member_concept_2 = Collection()
     member_concept_2.concept_id = 6
     member_concept_2.conceptscheme_id = 1
     self.collection.members.add(member_concept_1)
     self.collection.members.add(member_concept_2)
     self.concept.narrower_concepts.add(member_concept_1)
     self.concept.narrower_collections.add(member_concept_2)
示例#17
0
 def test_mapping_collections_filled(self):
     label = Label(label='test', labeltype_id='altLabel', language_id='nl')
     self.concept.labels.append(label)
     related_concept = Concept(concept_id=6, conceptscheme_id=1)
     self.concept.related_concepts.add(related_concept)
     result_concept = map_concept(self.concept, test_json, self.request.db)
     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))
示例#18
0
文件: crud.py 项目: JDeVos/atramhasis
    def add_concept(self):
        '''
        Add a new concept to a conceptscheme

        :raises atramhasis.errors.ValidationError: If the provided json can't be validated
        '''
        validated_json_concept = self._validate_concept(
            self._get_json_body(), self.provider.conceptscheme_id)
        cid = self.db.query(func.max(Thing.concept_id)).filter_by(
            conceptscheme_id=self.provider.conceptscheme_id).first()[0]
        if not cid:
            cid = 0
        cid += 1
        if validated_json_concept['type'] == 'concept':
            concept = Concept()
        else:
            concept = Collection()
        concept.concept_id = cid
        concept.conceptscheme_id = self.provider.conceptscheme_id
        concept.uri = self.provider.uri_generator.generate(id=cid)
        map_concept(concept, validated_json_concept, self.request.db)
        self.db.add(concept)
        self.db.flush()

        invalidate_scheme_cache(self.scheme_id)

        self.request.response.status = '201'
        self.request.response.location = self.request.route_path(
            'skosprovider.c',
            scheme_id=self.scheme_id,
            c_id=concept.concept_id)
        return from_thing(concept)
示例#19
0
    def add_concept(self):
        """
        Add a new concept to a conceptscheme

        :raises atramhasis.errors.ValidationError: If the provided json can't be validated
        """
        validated_json_concept = self._validate_concept(self._get_json_body(), self.provider.conceptscheme_id)
        cid = self.skos_manager.get_next_cid(self.provider.conceptscheme_id)
        if not cid:
            cid = 0
        cid += 1
        if validated_json_concept['type'] == 'concept':
            concept = Concept()
        else:
            concept = Collection()
        concept.concept_id = cid
        concept.conceptscheme_id = self.provider.conceptscheme_id
        concept.uri = self.provider.uri_generator.generate(id=concept.concept_id)
        map_concept(concept, validated_json_concept, self.skos_manager)
        concept = self.skos_manager.save(concept)

        invalidate_scheme_cache(self.scheme_id)

        self.request.response.status = '201'
        self.request.response.location = self.request.route_path(
            'skosprovider.c', scheme_id=self.scheme_id, c_id=concept.concept_id)
        return from_thing(concept)
示例#20
0
def get_all_mock():
    a_concept = Concept(concept_id=7895, conceptscheme_id=1, type='concept')
    a_labels = [Label(label='De Paardekastanje', language_id='nl'), Label(label='The Chestnut', language_id='en'),
                Label(label='la châtaigne', language_id='fr')]
    a_concept.labels = a_labels
    b_concept = Concept(concept_id=9863, conceptscheme_id=1, type='concept')
    b_labels = [Label(label='test', language_id='nl')]
    b_concept.labels = b_labels
    all_mock = Mock(return_value=[a_concept, b_concept])
    return all_mock
示例#21
0
def filter_by_mock_concept(concept_id, conceptscheme_id):
    if concept_id in (7, 11, 12):
        raise NoResultFound()
    filter_mock = Mock()
    if concept_id in (999, 19):
        thing = Collection(id=concept_id,
                           concept_id=concept_id,
                           conceptscheme_id=conceptscheme_id)
        thing.type = 'collection'
    else:
        thing = Concept(id=concept_id,
                        concept_id=concept_id,
                        conceptscheme_id=conceptscheme_id)
        thing.type = 'concept'
    filter_mock.one = Mock(return_value=thing)
    return filter_mock
示例#22
0
 def test_mapping_collections_filled(self):
     label = Label(label='test', labeltype_id='altLabel', language_id='nl')
     self.concept.labels.append(label)
     related_concept = Concept(concept_id=6, conceptscheme_id=1)
     self.concept.related_concepts.add(related_concept)
     source = Source(citation='testCitation')
     self.concept.sources.append(source)
     source = Source(citation='AnotherTestCitation')
     self.concept.sources.append(source)
     result_concept = map_concept(self.concept, test_json,
                                  self.skos_manager)
     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))
示例#23
0
 def test_gen_uri(self):
     from skosprovider_sqlalchemy.models import Concept, ConceptScheme
     from skosprovider.uri import UriPatternGenerator
     # Set up provider
     provider = SQLAlchemyProvider({
         'id': 'SOORTEN',
         'conceptscheme_id': 99
     },
                                   self.session_maker,
                                   uri_generator=UriPatternGenerator(
                                       'http://id.example.com/trees/%s'))
     c1 = Concept(concept_id=1,
                  conceptscheme=ConceptScheme(
                      id=99, uri='http://id.example.com/trees'))
     session = self.session_maker()
     session.add(c1)
     session.commit()
     assert c1.uri is None
     c2 = provider.get_by_id(1)
     assert c2.uri == 'http://id.example.com/trees/1'
def filter_by_mock_concept(concept_id, conceptscheme_id):
    concept = Concept(concept_id=concept_id, conceptscheme_id=conceptscheme_id)
    concept.type = 'concept'
    if concept_id == 2:
        broader_concept = Concept(concept_id=1,
                                  conceptscheme_id=conceptscheme_id)
        broader_concepts = set()
        broader_concepts.add(broader_concept)
        concept.broader_concepts = broader_concepts
    if concept_id == 7:
        narrower_concept = Concept(concept_id=14,
                                   conceptscheme_id=conceptscheme_id)
        narrower_concepts = set()
        narrower_concepts.add(narrower_concept)
        concept.narrower_concepts = narrower_concepts
    if concept_id == 666:
        concept = Collection(concept_id=concept_id,
                             conceptscheme_id=conceptscheme_id)
        concept.type = 'collection'
    if concept_id == 667:
        concept = Collection(concept_id=concept_id,
                             conceptscheme_id=conceptscheme_id)
        concept.type = 'collection'
        memberof = Collection(concept_id=60, conceptscheme_id=conceptscheme_id)
        memberofs = set()
        memberofs.add(memberof)
        concept.member_of = memberofs
    if concept_id == 62:
        concept = Collection(concept_id=concept_id,
                             conceptscheme_id=conceptscheme_id)
        concept.type = 'collection'
        member = Collection(concept_id=666, conceptscheme_id=conceptscheme_id)
        members = set()
        members.add(member)
        concept.members = members
    if concept_id == 777:
        if conceptscheme_id != 3:
            raise NoResultFound()
    filter_mock = Mock()
    filter_mock.one = Mock(return_value=concept)
    return filter_mock
    def setUp(self):
        self.concept = Concept()
        self.concept.type = 'concept'
        self.concept.id = 11
        self.concept.concept_id = 101
        self.concept.uri = 'urn:x-atramhasis-demo:TREES:101'
        self.concept.conceptscheme_id = 1

        notes = []
        note = Note(note='test note', notetype_id='example', language_id='en')
        note2 = Note(note='note def',
                     notetype_id='definition',
                     language_id='en')
        notes.append(note)
        notes.append(note2)
        self.concept.notes = notes

        labels = []
        label = Label(label='een label',
                      labeltype_id='prefLabel',
                      language_id='nl')
        label2 = Label(label='other label',
                       labeltype_id='altLabel',
                       language_id='en')
        label3 = Label(label='and some other label',
                       labeltype_id='altLabel',
                       language_id='en')
        labels.append(label)
        labels.append(label2)
        labels.append(label3)
        self.concept.labels = labels

        sources = []
        source = Source('Van Daele K. 2009')
        sources.append(source)
        self.concept.sources = sources

        matches = []
        match = Match()
        match.matchtype = MatchType(name='closeMatch', description='test')
        match.uri = 'urn:somethingelse:st1'
        matches.append(match)
        match2 = Match()
        match2.matchtype = MatchType(name='closeMatch', description='test')
        match2.uri = 'urn:somethingelse:st2'
        matches.append(match2)
        match3 = Match()
        match3.matchtype = MatchType(name='exactMatch', description='test')
        match3.uri = 'urn:something:thingy'
        matches.append(match3)
        self.concept.matches = matches

        self.collection = Collection()
        self.collection.type = 'collection'
        self.collection.id = 12
        self.collection.concept_id = 102
        self.collection.uri = 'urn:x-atramhasis-demo:TREES:102'
        self.collection.conceptscheme_id = 1

        self.conceptscheme = ConceptScheme()
        self.conceptscheme.id = 1
        self.conceptscheme.labels = labels
        self.conceptscheme.notes = notes
        self.conceptscheme.sources = sources
        self.conceptscheme.uri = None

        self.regis = Registry()
        self.regis.register_provider(trees)
        self.request = testing.DummyRequest()
        self.request.skos_registry = self.regis
        self.request.matchdict = {'scheme_id': 'TREES'}
        self.request.locale_name = 'nl'

        self.concept.member_of.add(self.collection)
        self.collection.members.add(self.concept)
示例#26
0
 def test_save(self):
     thing = Concept()
     thing.concept_id = 123
     thing.conceptscheme_id = 1
     thing = self.skos_manager.save(thing)
     self.assertIsNotNone(thing.id)
def filter_by_mock_concept(concept_id, conceptscheme_id):
    concept = Concept(concept_id=concept_id, conceptscheme_id=conceptscheme_id)
    concept.type = 'concept'
    if concept_id == 2:
        broader_concept = Concept(concept_id=1, conceptscheme_id=conceptscheme_id)
        broader_concepts = set()
        broader_concepts.add(broader_concept)
        concept.broader_concepts = broader_concepts
    if concept_id == 7:
        narrower_concept = Concept(concept_id=14, conceptscheme_id=conceptscheme_id)
        narrower_concepts = set()
        narrower_concepts.add(narrower_concept)
        concept.narrower_concepts = narrower_concepts
    if concept_id == 666:
        concept = Collection(concept_id=concept_id, conceptscheme_id=conceptscheme_id)
        concept.type = 'collection'
    if concept_id == 667:
        concept = Collection(concept_id=concept_id, conceptscheme_id=conceptscheme_id)
        concept.type = 'collection'
        memberof = Collection(concept_id=60, conceptscheme_id=conceptscheme_id)
        memberofs = set()
        memberofs.add(memberof)
        concept.member_of = memberofs
    if concept_id == 62:
        concept = Collection(concept_id=concept_id, conceptscheme_id=conceptscheme_id)
        concept.type = 'collection'
        member = Collection(concept_id=666, conceptscheme_id=conceptscheme_id)
        members = set()
        members.add(member)
        concept.members = members
    if concept_id == 777:
        if conceptscheme_id != 3:
            raise NoResultFound()
    filter_mock = Mock()
    filter_mock.one = Mock(return_value=concept)
    return filter_mock
示例#28
0
def map_concept(concept, concept_json, skos_manager):
    """
    Map a concept from json to the database.

    :param skosprovider_sqlalchemy.models.Thing concept: A concept or
        collection as known to the database.
    :param dict concept_json: A dict representing the json sent to our REST
        service.
    :param skos_manager: A skos_manager to acces db operations
    :returns: The :class:`skosprovider_sqlalchemy.models.Thing` enhanced
        with the information from the json object.
    """
    concept_json_type = concept_json.get('type', None)
    if concept.type != concept_json_type:

        if concept_json_type == 'concept':
            members = concept.members
            concept = skos_manager.change_type(concept,
                                               concept.concept_id,
                                               concept.conceptscheme_id,
                                               concept_json_type)
            for member in members:
                if member.type == 'concept':
                    concept.narrower_concepts.add(member)
                elif member.type == 'collection':
                    concept.narrower_collections.add(member)
        elif concept_json_type == 'collection':
            narrower_concepts = concept.narrower_concepts
            narrower_collections = concept.narrower_collections
            concept = skos_manager.change_type(concept,
                                               concept.concept_id,
                                               concept.conceptscheme_id,
                                               concept_json_type)
            for narrower_concept in narrower_concepts:
                concept.members.add(narrower_concept)
            for narrower_collection in narrower_collections:
                concept.members.add(narrower_collection)
    elif concept_json_type == 'collection':
        concept.members.clear()
    elif concept_json_type == 'concept':
        concept.narrower_collections.clear()
        concept.narrower_concepts.clear()
    if concept.type in ('concept', 'collection'):
        concept.labels[:] = []
        labels = concept_json.get('labels', [])
        for l in labels:
            label = Label(label=l.get('label', ''), labeltype_id=l.get('type', ''), language_id=l.get('language', ''))
            concept.labels.append(label)
        concept.notes[:] = []
        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', [])
        for s in sources:
            source = Source(citation=s.get('citation', ''))
            if is_html(source.citation):
                source.markup = 'HTML'
            concept.sources.append(source)

        concept.member_of.clear()
        member_of = concept_json.get('member_of', [])
        for memberof in member_of:
            try:
                memberof_collection = skos_manager.get_thing(
                    concept_id=memberof['id'],
                    conceptscheme_id=concept.conceptscheme_id)
            except NoResultFound:
                memberof_collection = Collection(concept_id=memberof['id'], conceptscheme_id=concept.conceptscheme_id)
            concept.member_of.add(memberof_collection)

        if concept.type == 'concept':
            concept.related_concepts.clear()
            related = concept_json.get('related', [])
            for related in related:
                try:
                    related_concept = skos_manager.get_thing(
                        concept_id=related['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                except NoResultFound:
                    related_concept = Concept(concept_id=related['id'], conceptscheme_id=concept.conceptscheme_id)
                concept.related_concepts.add(related_concept)

            concept.broader_concepts.clear()
            broader = concept_json.get('broader', [])
            for broader in broader:
                try:
                    broader_concept = skos_manager.get_thing(
                        concept_id=broader['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                except NoResultFound:
                    broader_concept = Concept(concept_id=broader['id'], conceptscheme_id=concept.conceptscheme_id)
                concept.broader_concepts.add(broader_concept)
            narrower = concept_json.get('narrower', [])
            for narrower in narrower:
                try:
                    narrower_concept = skos_manager.get_thing(
                        concept_id=narrower['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                except NoResultFound:
                    narrower_concept = Concept(concept_id=narrower['id'], conceptscheme_id=concept.conceptscheme_id)
                concept.narrower_concepts.add(narrower_concept)

            matches = []
            matchdict = concept_json.get('matches', {})
            for type in matchdict:
                db_type = type + "Match"
                matchtype = skos_manager.get_match_type(db_type)
                for uri in matchdict[type]:
                    concept_id = concept_json.get('id', -1)
                    try:
                        match = skos_manager.get_match(uri=uri, matchtype_id=matchtype.name,
                                                       concept_id=concept_id)
                    except NoResultFound:
                        match = Match()
                        match.matchtype = matchtype
                        match.uri = uri
                    matches.append(match)
            concept.matches = matches

            narrower_collections = concept_json.get('subordinate_arrays', [])
            for narrower in narrower_collections:
                try:
                    narrower_collection = skos_manager.get_thing(
                        concept_id=narrower['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                except NoResultFound:
                    narrower_collection = Collection(concept_id=narrower['id'],
                                                     conceptscheme_id=concept.conceptscheme_id)
                concept.narrower_collections.add(narrower_collection)

        if concept.type == 'collection':
            members = concept_json.get('members', [])
            for member in members:
                try:
                    member_concept = skos_manager.get_thing(
                        concept_id=member['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                except NoResultFound:
                    member_concept = Concept(concept_id=member['id'], conceptscheme_id=concept.conceptscheme_id)
                concept.members.add(member_concept)

            concept.broader_concepts.clear()
            broader_concepts = concept_json.get('superordinates', [])
            for broader in broader_concepts:
                try:
                    broader_concept = skos_manager.get_thing(
                        concept_id=broader['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                except NoResultFound:
                    broader_concept = Concept(concept_id=broader['id'], conceptscheme_id=concept.conceptscheme_id)
                concept.broader_concepts.add(broader_concept)
    return concept
 def test_save(self):
     thing = Concept()
     thing.concept_id = 123
     thing.conceptscheme_id = 1
     thing = self.skos_manager.save(thing)
     self.assertIsNotNone(thing.id)
示例#30
0
def map_concept(concept, concept_json, db_session):
    '''
    Map a concept from json to the database.

    :param skosprovider_sqlalchemy.models.Thing concept: A concept or 
        collection as known to the database.
    :param dict concept_json: A dict representing the json sent to our REST 
        service.
    :param session: A :class:`sqlalchemy.orm.session.Session`.
    :returns: The :class:`skosprovider_sqlalchemy.models.Thing` enhanced 
        with the information from the json object.
    '''
    concept.type = concept_json.get('type', None)
    if concept.type in ('concept', 'collection'):
        for label in concept.labels:
            concept.labels.remove(label)
        labels = concept_json.get('labels', [])
        for l in labels:
            label = Label(label=l.get('label', ''),
                          labeltype_id=l.get('type', ''),
                          language_id=l.get('language', ''))
            concept.labels.append(label)
        for note in concept.notes:
            concept.notes.remove(note)
        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', ''))
            concept.notes.append(note)

        concept.member_of.clear()
        member_of = concept_json.get('member_of', [])
        for memberof in member_of:
            try:
                memberof_collection = db_session.query(Collection)\
                    .filter_by(concept_id=memberof['id'], conceptscheme_id=concept.conceptscheme_id).one()
            except NoResultFound:
                memberof_collection = Collection(
                    concept_id=memberof['id'],
                    conceptscheme_id=concept.conceptscheme_id)
            concept.member_of.add(memberof_collection)

        if concept.type == 'concept':
            concept.related_concepts.clear()
            related = concept_json.get('related', [])
            for related in related:
                try:
                    related_concept = db_session.query(Concept).filter_by(
                        concept_id=related['id'],
                        conceptscheme_id=concept.conceptscheme_id).one()
                except NoResultFound:
                    related_concept = Concept(
                        concept_id=related['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                concept.related_concepts.add(related_concept)
            concept.narrower_concepts.clear()

            concept.broader_concepts.clear()
            broader = concept_json.get('broader', [])
            for broader in broader:
                try:
                    broader_concept = db_session.query(Concept).filter_by(
                        concept_id=broader['id'],
                        conceptscheme_id=concept.conceptscheme_id).one()
                except NoResultFound:
                    broader_concept = Concept(
                        concept_id=broader['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                concept.broader_concepts.add(broader_concept)
            narrower = concept_json.get('narrower', [])
            for narrower in narrower:
                try:
                    narrower_concept = db_session.query(Concept).filter_by(
                        concept_id=narrower['id'],
                        conceptscheme_id=concept.conceptscheme_id).one()
                except NoResultFound:
                    narrower_concept = Concept(
                        concept_id=narrower['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                concept.narrower_concepts.add(narrower_concept)

            matches = []
            matchdict = concept_json.get('matches', {})
            for type in matchdict:
                db_type = type + "Match"
                matchtype = db_session.query(MatchType).filter_by(
                    name=db_type).one()
                for uri in matchdict[type]:
                    concept_id = concept_json.get('id', -1)
                    try:
                        match = db_session.query(Match).filter_by(
                            uri=uri,
                            matchtype_id=matchtype.name,
                            concept_id=concept_id).one()
                    except NoResultFound:
                        match = Match()
                        match.matchtype = matchtype
                        match.uri = uri
                    matches.append(match)
            concept.matches = matches

            concept.narrower_collections.clear()
            narrower_collections = concept_json.get('subordinate_arrays', [])
            for narrower in narrower_collections:
                try:
                    narrower_collection = db_session.query(Collection)\
                        .filter_by(concept_id=narrower['id'], conceptscheme_id=concept.conceptscheme_id).one()
                except NoResultFound:
                    narrower_collection = Collection(
                        concept_id=narrower['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                concept.narrower_collections.add(narrower_collection)

        if concept.type == 'collection':
            concept.members.clear()
            members = concept_json.get('members', [])
            for member in members:
                try:
                    member_concept = db_session.query(Thing).filter_by(
                        concept_id=member['id'],
                        conceptscheme_id=concept.conceptscheme_id).one()
                except NoResultFound:
                    member_concept = Concept(
                        concept_id=member['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                concept.members.add(member_concept)

            concept.broader_concepts.clear()
            broader_concepts = concept_json.get('superordinates', [])
            for broader in broader_concepts:
                try:
                    broader_concept = db_session.query(Concept)\
                        .filter_by(concept_id=broader['id'], conceptscheme_id=concept.conceptscheme_id).one()
                except NoResultFound:
                    broader_concept = Concept(
                        concept_id=broader['id'],
                        conceptscheme_id=concept.conceptscheme_id)
                concept.broader_concepts.add(broader_concept)

    return concept
示例#31
0
def create_data(session):
    from skosprovider_sqlalchemy.models import (Concept, ConceptScheme,
                                                Collection, Label, Note, Match,
                                                Language)
    en = session.query(Language).get('en')
    nl = session.query(Language).get('nl')
    cs = ConceptScheme(id=1, uri='urn:x-skosprovider:test', languages=[en, nl])
    session.add(cs)
    con = Concept(id=10,
                  uri='urn:x-skosprovider:test:1',
                  concept_id=1,
                  conceptscheme=cs)
    session.add(con)
    l = Label('Churches', 'prefLabel', 'en')
    con.labels.append(l)
    l = Label('Kerken', 'prefLabel', 'nl')
    con.labels.append(l)
    col = Collection(id=20,
                     uri='urn:x-skosprovider:test:2',
                     concept_id=2,
                     conceptscheme=cs)
    col.broader_concepts.add(con)
    n = Note(
        'Churches organised by function, as opposed to by shape or religion.',
        'scopeNote', 'en')
    col.notes.append(n)
    l = Label('Churches by function', 'prefLabel', 'en')
    col.labels.append(l)
    l = Label('111sortmefirst', 'sortLabel', 'en')
    col.labels.append(l)
    session.add(col)
    chap = Concept(id=30,
                   uri='urn:x-skosprovider:test:3',
                   concept_id=3,
                   conceptscheme=cs)
    l = Label('Chapels', 'prefLabel', 'en')
    chap.labels.append(l)
    session.add(chap)
    chap.related_concepts.add(con)
    tchap = Concept(id=50,
                    uri='urn:x-skosprovider:test:5',
                    concept_id=5,
                    conceptscheme=cs)
    tchap.labels.append(Label('Boomkapellen', 'prefLabel', 'nl'))
    session.add(tchap)
    tchap.broader_concepts.add(chap)
    cath = Concept(id=40,
                   uri='urn:x-skosprovider:test:4',
                   concept_id=4,
                   conceptscheme=cs)
    l = Label('Cathedrals', 'prefLabel', 'en')
    cath.labels.append(l)
    n = Note('A cathedral is a church which contains the seat of a bishop.',
             'definition', 'en')
    cath.notes.append(n)
    session.add(cath)
    cath.broader_concepts.add(con)
    cath.member_of.add(col)
    match = Match(matchtype_id='closeMatch',
                  uri='http://vocab.getty.edu/aat/300007501')
    cath.matches.append(match)
    session.commit()
示例#32
0
def filter_by_mock_concept(**kwargs):
    filter_mock = Mock()
    concept_id = None
    conceptscheme_id = None
    if 'concept_id' in kwargs:
        concept_id = kwargs['concept_id']
    if 'conceptscheme_id' in kwargs:
        conceptscheme_id = kwargs['conceptscheme_id']
    if concept_id == '1':
        c = Concept(concept_id=concept_id, conceptscheme_id=conceptscheme_id)
        c.type = 'concept'
        filter_mock.one = Mock(return_value=c)
    elif concept_id == '3':
        c = Collection(concept_id=concept_id,
                       conceptscheme_id=conceptscheme_id)
        c.type = 'collection'
        filter_mock.one = Mock(return_value=c)
    elif concept_id == '555':
        c = Thing(concept_id=concept_id, conceptscheme_id=conceptscheme_id)
        filter_mock.one = Mock(return_value=c)
    elif concept_id == '666':
        raise NoResultFound

    a_concept = Concept(concept_id=7895,
                        conceptscheme_id=conceptscheme_id,
                        type='concept')
    a_labels = [
        Label(label='De Paardekastanje', language_id='nl'),
        Label(label='The Chestnut', language_id='en'),
        Label(label='la châtaigne', language_id='fr')
    ]
    a_concept.labels = a_labels
    b_concept = Concept(concept_id=9863,
                        conceptscheme_id=conceptscheme_id,
                        type='concept')
    b_labels = [Label(label='test', language_id='nl')]
    b_concept.labels = b_labels
    filter_mock.all = Mock(return_value=[a_concept, b_concept])
    return filter_mock