예제 #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.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)
예제 #2
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)
예제 #3
0
 def test_mapping_check_db_lookup_member_of(self):
     result_concept = map_concept(self.concept, test_json,
                                  self.skos_manager)
     for member_of in result_concept.member_of:
         self.assertIsNotNone(member_of)
         if member_of.concept_id == 7:
             self.assertIsNone(member_of.id)
예제 #4
0
 def test_mapping_check_db_lookup(self):
     result_concept = map_concept(self.concept, test_json,
                                  self.skos_manager)
     for narrower_concept in result_concept.narrower_concepts:
         self.assertIsNotNone(narrower_concept)
         if narrower_concept.concept_id == 7:
             self.assertIsNone(narrower_concept.id)
예제 #5
0
 def test_mapping_collection_to_concept(self):
     result_concept = map_concept(self.collection, test_json,
                                  self.skos_manager)
     self.assertIsNotNone(result_concept)
     self.assertFalse(hasattr(result_concept, 'members'))
     self.assertTrue(hasattr(result_concept, 'related_concepts'))
     self.assertTrue(hasattr(result_concept, 'narrower_concepts'))
     self.assertTrue(hasattr(result_concept, 'narrower_collections'))
예제 #6
0
 def test_mapping_subordinate_arrays_no_result(self):
     test_json["subordinate_arrays"] = [{"id": 11}]
     result_concept = map_concept(self.concept, test_json, self.request.db)
     self.assertIsNotNone(result_concept)
     self.assertTrue(hasattr(result_concept, 'narrower_collections'))
     self.assertEqual(1, len(result_concept.narrower_collections))
     self.assertEqual([c for c in result_concept.narrower_collections
                       ][0].concept_id, 11)
예제 #7
0
 def test_mapping_superordinates(self):
     json_collection["superordinates"] = [{"id": 12}]
     result_collection = map_concept(self.collection, json_collection,
                                     self.skos_manager)
     self.assertIsNotNone(result_collection)
     self.assertTrue(hasattr(result_collection, 'broader_concepts'))
     self.assertEqual(1, len(result_collection.broader_concepts))
     self.assertEqual([c for c in result_collection.broader_concepts
                       ][0].concept_id, 12)
예제 #8
0
 def test_mapping_collection(self):
     result_collection = map_concept(self.collection, json_collection,
                                     self.skos_manager)
     self.assertIsNotNone(result_collection)
     self.assertEqual(3, len(result_collection.members))
     self.assertEqual(2, len(result_collection.member_of))
     self.assertEqual(1, len(result_collection.labels))
     self.assertEqual(1, len(result_collection.notes))
     self.assertFalse(hasattr(result_collection, 'related_concepts'))
예제 #9
0
 def test_mapping(self):
     result_concept = map_concept(self.concept, test_json, self.request.db)
     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.assertFalse(hasattr(result_concept, 'members'))
예제 #10
0
    def edit_concept(self):
        """
        Edit an existing concept

        :raises atramhasis.errors.ConceptNotFoundException: If the concept can't be found
        :raises atramhasis.errors.ValidationError: If the provided json can't be validated
        """
        c_id = self.request.matchdict['c_id']
        validated_json_concept = self._validate_concept(self._get_json_body(), self.provider.conceptscheme_id)
        try:
            concept = self.skos_manager.get_thing(c_id, self.provider.conceptscheme_id)
        except NoResultFound:
            raise ConceptNotFoundException(c_id)
        map_concept(concept, validated_json_concept, self.skos_manager)

        invalidate_scheme_cache(self.scheme_id)

        self.request.response.status = '200'
        return from_thing(concept)
예제 #11
0
 def test_mapping_matches(self):
     test_json["matches"] = {
         "exact": ["urn:sample:666"],
         "broad": ["urn:somewhere:93"]
     }
     result_concept = map_concept(self.concept, test_json, self.request.db)
     self.assertIsNotNone(result_concept)
     self.assertTrue(hasattr(result_concept, 'matches'))
     self.assertEqual(2, len(result_concept.matches))
     self.assertIn(result_concept.matches[0].uri,
                   ["urn:sample:666", "urn:somewhere:93"])
예제 #12
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))
예제 #13
0
파일: crud.py 프로젝트: JDeVos/atramhasis
    def edit_concept(self):
        '''
        Edit an existing concept

        :raises atramhasis.errors.ConceptNotFoundException: If the concept can't be found
        :raises atramhasis.errors.ValidationError: If the provided json can't be validated
        '''
        c_id = self.request.matchdict['c_id']
        validated_json_concept = self._validate_concept(
            self._get_json_body(), self.provider.conceptscheme_id)
        try:
            concept = self.db.query(Thing).filter_by(
                concept_id=c_id,
                conceptscheme_id=self.provider.conceptscheme_id).one()
        except NoResultFound:
            raise ConceptNotFoundException(c_id)
        map_concept(concept, validated_json_concept, self.request.db)

        invalidate_scheme_cache(self.scheme_id)

        self.request.response.status = '200'
        return from_thing(concept)
예제 #14
0
 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)
     self.assertEqual('HTML', result_concept.sources[0].markup)
예제 #15
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))