def setUp(self):
        ResourceInstance.objects.all().delete()

        skos = SKOSReader()
        rdf = skos.read_file(
            'tests/fixtures/data/concept_label_test_scheme.xml')
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file(
            'tests/fixtures/data/concept_label_test_collection.xml')
        ret = skos.save_concepts_from_skos(rdf)

        with open(
                os.path.join(
                    'tests/fixtures/data/json/cardinality_test_data/target.json'
                ), 'rU') as f:
            archesfile = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile['graph'])

        with open(
                os.path.join(
                    'tests/fixtures/data/json/cardinality_test_data/file-list.json'
                ), 'rU') as f:
            archesfile = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile['graph'])
Ejemplo n.º 2
0
    def setUp(self):
        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/data/concept_label_test_scheme.xml")
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/data/concept_label_test_collection.xml")
        ret = skos.save_concepts_from_skos(rdf)

        with open(os.path.join("tests/fixtures/resource_graphs/resource_export_test.json"), "rU") as f:
            archesfile = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile["graph"])
Ejemplo n.º 3
0
    def import_reference_data(self, data_source, overwrite='ignore', stage='stage'):
        if overwrite == '':
            overwrite = 'overwrite'

        skos = SKOSReader()
        rdf = skos.read_file(data_source)
        ret = skos.save_concepts_from_skos(rdf, overwrite, stage)
Ejemplo n.º 4
0
    def import_reference_data(self, data_source, overwrite='ignore', stage='stage'):
        if overwrite == '':
            overwrite = 'overwrite'

        skos = SKOSReader()
        rdf = skos.read_file(data_source)
        ret = skos.save_concepts_from_skos(rdf, overwrite, stage)
Ejemplo n.º 5
0
    def test_6_5126_collection_filter(self):
        # 2019-11-01 - Fails due to #5126, the concept is not checked against the collection

        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/jsonld_base/rdm/5126-thesaurus.xml")
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/jsonld_base/rdm/5126-collections.xml")
        ret = skos.save_concepts_from_skos(rdf)

        # Load up the models and data only once
        with open(os.path.join("tests/fixtures/jsonld_base/models/5126_collection_ambiguity.json"), "rU") as f:
            archesfile = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile["graph"])

        data = """
            {
                "@id": "http://*****:*****@type": "http://www.cidoc-crm.org/cidoc-crm/E22_Man-Made_Object",
                "http://www.cidoc-crm.org/cidoc-crm/P2_has_type": {
                    "@id": "http://vocab.getty.edu/aat/300404216",
                    "@type": "http://www.cidoc-crm.org/cidoc-crm/E55_Type",
                    "http://www.w3.org/2000/01/rdf-schema#label": "aquarelles (paintings)"
                }
            }
        """
        url = reverse(
            "resources_graphid",
            kwargs={"graphid": "09e3dc8a-c055-11e9-b4dc-0242ac160002", "resourceid": "69a4af50-c055-11e9-b4dc-0242ac160002"},
        )
        response = self.client.put(url, data=data, HTTP_AUTHORIZATION=f"Bearer {self.token}")
        print(f"Test 6 response: {response.content}")
        self.assertTrue(response.status_code == 201)
        js = response.json()
        if type(js) == list:
            js = js[0]

        print(f"Got JSON for test 6: {js}")
        self.assertTrue("@id" in js)
        self.assertTrue(js["@id"] == "http://*****:*****@id"] == "http://vocab.getty.edu/aat/300404216")
Ejemplo n.º 6
0
    def load_skos(self, skosfile):

        skos = SKOSReader()
        rdf = skos.read_file(skosfile)
        ret = skos.save_concepts_from_skos(rdf)

        schemeid = [i.id for i in ret.nodes
                    if i.nodetype == "ConceptScheme"][0]

        return schemeid
Ejemplo n.º 7
0
    def convert_v3_skos(self, package_dir, direct_import=False, verbose=False):

        uuid_collection_file = os.path.join(package_dir, "reference_data",
                                            "v3topconcept_lookup.json")
        if not os.path.isfile(uuid_collection_file):
            if verbose:
                print("creating new collection lookup file: " +
                      uuid_collection_file)
            with open(uuid_collection_file, "wb") as openfile:
                json.dump({}, openfile)
        try:
            if verbose:
                print("using existing collection lookup file: " +
                      uuid_collection_file)
            with open(uuid_collection_file, "rb") as openfile:
                uuid_data = json.loads(openfile.read())
        except ValueError as e:
            print("\n  -- JSON parse error in " + uuid_collection_file +
                  ":\n\n    " + e.message)
            exit()

        v3_ref_dir = os.path.join(package_dir, 'v3data', 'reference_data')
        v4_ref_dir = os.path.join(package_dir, 'reference_data')

        v3_skos_files = glob(os.path.join(v3_ref_dir, '*.xml'))
        if len(v3_skos_files) == 0:
            print("\nThere is no v3 data to import. Export your concept scheme"
                  " from v3 and place it in {}".format(v3_ref_dir))
            exit()

        if len(v3_skos_files) > 0:
            skos_file = v3_skos_files[0]

        if len(v3_skos_files) > 1:
            print("\nOnly one v3 file can be converted. This file will be used"
                  ":\n\n  {}".format(skos_file))

        skos_importer = v3SkosConverter(
            skos_file,
            name_space=settings.ARCHES_NAMESPACE_FOR_DATA_EXPORT,
            uuid_lookup=uuid_data,
            verbose=verbose)
        skos_importer.write_skos(v4_ref_dir)
        skos_importer.write_uuid_lookup(uuid_collection_file)

        if direct_import:

            theaurus_file = os.path.join(v4_ref_dir, "concepts",
                                         "thesaurus.xml")
            collection_file = os.path.join(v4_ref_dir, "collections",
                                           "collections.xml")
            for skosfile in [theaurus_file, collection_file]:
                skos = SKOSReader()
                rdf = skos.read_file(skosfile)
                ret = skos.save_concepts_from_skos(rdf, 'overwrite', 'keep')
    def load_v4_reference_data(self, dry_run=False):

        v4_thesaurus = os.path.join(self.pkg, 'reference_data', 'concepts', 'thesaurus.xml')
        v4_collections = os.path.join(self.pkg, 'reference_data', 'collections', 'collections.xml')
        for skosfile in [v4_thesaurus, v4_collections]:
            self.assertTrue(os.path.isfile(skosfile))
            if dry_run:
                continue
            skos = SKOSReader()
            rdf = skos.read_file(skosfile)
            ret = skos.save_concepts_from_skos(rdf, 'overwrite', 'keep')
Ejemplo n.º 9
0
    def setUpClass(cls):
        ResourceInstance.objects.all().delete()

        for skospath in ["tests/fixtures/data/rdf_export_thesaurus.xml", "tests/fixtures/data/rdf_export_collections.xml"]:
            skos = SKOSReader()
            rdf = skos.read_file(skospath)
            ret = skos.save_concepts_from_skos(rdf)

        # Models
        for model_name in ["object_model", "document_model"]:
            with open(os.path.join("tests/fixtures/resource_graphs/rdf_export_{0}.json".format(model_name)), "rU") as f:
                archesfile = JSONDeserializer().deserialize(f)
            ResourceGraphImporter(archesfile["graph"])
Ejemplo n.º 10
0
    def setUp(self):
        ResourceInstance.objects.all().delete()

        skos = SKOSReader()
        rdf = skos.read_file(
            'tests/fixtures/data/concept_label_test_scheme.xml')
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file(
            'tests/fixtures/data/concept_label_test_collection.xml')
        ret = skos.save_concepts_from_skos(rdf)

        with open(
                os.path.join(
                    'tests/fixtures/resource_graphs/resource_export_test.json'
                ), 'rU') as f:
            archesfile = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile['graph'])

        BusinessDataImporter('tests/fixtures/data/csv/resource_export_test.csv'
                             ).import_business_data()
Ejemplo n.º 11
0
    def setUp(self):
        ResourceInstance.objects.all().delete()

        skos = SKOSReader()
        rdf = skos.read_file('tests/fixtures/data/concept_label_test_scheme.xml')
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file('tests/fixtures/data/concept_label_test_collection.xml')
        ret = skos.save_concepts_from_skos(rdf)

        with open(os.path.join('tests/fixtures/resource_graphs/resource_export_test.json'), 'rU') as f:
            archesfile = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile['graph'])

        # loading RDF/JSONLD export fixtures
        skos = SKOSReader()
        rdf = skos.read_file('tests/fixtures/data/rdf_export_thesaurus.xml')
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file('tests/fixtures/data/rdf_export_collections.xml')
        ret = skos.save_concepts_from_skos(rdf)

        # Models
        for model_name in ['object_model', 'document_model']:
            with open(os.path.join(
                    'tests/fixtures/resource_graphs/rdf_export_{0}.json'.format(model_name)), 'rU') as f:
                archesfile = JSONDeserializer().deserialize(f)
            ResourceGraphImporter(archesfile['graph'])
        # Fixture Instance Data for tests
        for instance_name in ['document', 'object']:
            BusinessDataImporter(
                    'tests/fixtures/data/rdf_export_{0}.json'.format(instance_name)).import_business_data()

        # for RDF/JSON-LD export tests
        self.DT = DataTypeFactory()
        self.archesproject = Namespace(test_settings.ARCHES_NAMESPACE_FOR_DATA_EXPORT)
        self.cidoc = Namespace("http://www.cidoc-crm.org/cidoc-crm/")
Ejemplo n.º 12
0
    def setUpClass(cls):
        # This runs once per instantiation
        cls.loadOntology()
        cls.factory = RequestFactory()
        cls.client = Client()

        #cls.client.login(username='******', password='******')
        #cls.user = User.objects.get(username='******')

        skos = SKOSReader()
        rdf = skos.read_file(
            'tests/fixtures/jsonld_base/rdm/jsonld_test_thesaurus.xml')
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file(
            'tests/fixtures/jsonld_base/rdm/jsonld_test_collections.xml')
        ret = skos.save_concepts_from_skos(rdf)

        # Load up the models and data only once
        with open(
                os.path.join(
                    'tests/fixtures/jsonld_base/models/test_1_basic_object.json'
                ), 'rU') as f:
            archesfile = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile['graph'])
        BusinessDataImporter(
            'tests/fixtures/jsonld_base/data/test_1_instance.json'
        ).import_business_data()

        with open(
                os.path.join(
                    'tests/fixtures/jsonld_base/models/test_2_complex_object.json'
                ), 'rU') as f:
            archesfile2 = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile2['graph'])
        BusinessDataImporter(
            'tests/fixtures/jsonld_base/data/test_2_instances.json'
        ).import_business_data()

        with open(
                os.path.join(
                    'tests/fixtures/jsonld_base/models/5136_res_inst_plus_res_inst.json'
                ), 'rU') as f:
            archesfile2 = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile2['graph'])
        BusinessDataImporter(
            'tests/fixtures/jsonld_base/data/test_3_instances.json'
        ).import_business_data()

        with open(
                os.path.join(
                    'tests/fixtures/jsonld_base/models/nesting_test.json'),
                'rU') as f:
            archesfile2 = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile2['graph'])
        BusinessDataImporter(
            'tests/fixtures/jsonld_base/data/test_nest_instances.json'
        ).import_business_data()

        with open(
                os.path.join(
                    'tests/fixtures/jsonld_base/models/4564-person.json'),
                'rU') as f:
            archesfile2 = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile2['graph'])

        with open(
                os.path.join(
                    'tests/fixtures/jsonld_base/models/4564-group.json'),
                'rU') as f:
            archesfile2 = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile2['graph'])

        with open(
                os.path.join(
                    'tests/fixtures/jsonld_base/models/4564-referenced.json'),
                'rU') as f:
            archesfile2 = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile2['graph'])
        BusinessDataImporter(
            'tests/fixtures/jsonld_base/data/test_4564_group.json'
        ).import_business_data()
        BusinessDataImporter(
            'tests/fixtures/jsonld_base/data/test_4564_reference.json'
        ).import_business_data()

        management.call_command('datatype',
                                'register',
                                source='tests/fixtures/datatypes/color.py')
        management.call_command(
            'datatype',
            'register',
            source='tests/fixtures/datatypes/semantic_like.py')

        with open(
                os.path.join(
                    'tests/fixtures/jsonld_base/models/5299-basic.json'),
                'rU') as f:
            archesfile2 = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile2['graph'])
        BusinessDataImporter(
            'tests/fixtures/jsonld_base/data/test_5299_instances.json'
        ).import_business_data()

        with open(
                os.path.join(
                    'tests/fixtures/jsonld_base/models/5299_complex.json'),
                'rU') as f:
            archesfile2 = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile2['graph'])
        BusinessDataImporter(
            'tests/fixtures/jsonld_base/data/test_5299_complex.json'
        ).import_business_data()
Ejemplo n.º 13
0
def concept(request, conceptid):
    f = request.GET.get('f', 'json')
    mode = request.GET.get('mode', '')
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    pretty = request.GET.get('pretty', False)

    if request.method == 'GET':
        include_subconcepts = request.GET.get('include_subconcepts', 'true') == 'true'
        include_parentconcepts = request.GET.get('include_parentconcepts', 'true') == 'true'
        include_relatedconcepts = request.GET.get('include_relatedconcepts', 'true') == 'true'
        emulate_elastic_search = request.GET.get('emulate_elastic_search', 'false') == 'true'
        depth_limit = request.GET.get('depth_limit', None)

        depth_limit = 1
        if not conceptid:
            return render(request, 'views/rdm/concept-report.htm', {
                'lang': lang,
                'concept_count': models.Concept.objects.filter(nodetype='Concept').count(),
                'collection_count': models.Concept.objects.filter(nodetype='Collection').count(),
                'scheme_count': models.Concept.objects.filter(nodetype='ConceptScheme').count(),
                'entitytype_count': models.Concept.objects.filter(nodetype='EntityType').count(),
                'default_report': True
            })


        labels = []

        concept_graph = Concept().get(id=conceptid, include_subconcepts=include_subconcepts,
            include_parentconcepts=include_parentconcepts, include_relatedconcepts=include_relatedconcepts,
            depth_limit=depth_limit, up_depth_limit=None, lang=lang, semantic=(mode == 'semantic' or mode == ''))

        languages = sort_languages(models.DLanguage.objects.all(), lang)

        valuetypes = models.DValueType.objects.all()
        relationtypes = models.DRelationType.objects.all()
        prefLabel = concept_graph.get_preflabel(lang=lang)
        for subconcept in concept_graph.subconcepts:
            subconcept.prefLabel = subconcept.get_preflabel(lang=lang)
        for relatedconcept in concept_graph.relatedconcepts:
            relatedconcept.prefLabel = relatedconcept.get_preflabel(lang=lang)
        for value in concept_graph.values:
            if value.category == 'label':
                labels.append(value)

        if (mode == 'semantic' or mode == '') and (concept_graph.nodetype == 'Concept' or concept_graph.nodetype == 'ConceptScheme' or concept_graph.nodetype == 'EntityType'):
            if concept_graph.nodetype == 'ConceptScheme':
                parent_relations = relationtypes.filter(category='Properties')
            else:
                parent_relations = relationtypes.filter(category='Semantic Relations').exclude(relationtype = 'related').exclude(relationtype='broader').exclude(relationtype='broaderTransitive')
            return render(request, 'views/rdm/concept-report.htm', {
                'lang': lang,
                'prefLabel': prefLabel,
                'labels': labels,
                'concept': concept_graph,
                'languages': languages,
                'sparql_providers': get_sparql_providers(),
                'valuetype_labels': valuetypes.filter(category='label'),
                'valuetype_notes': valuetypes.filter(category='note'),
                'valuetype_related_values': valuetypes.filter(category__in=['undefined','identifiers']),
                'parent_relations': parent_relations,
                'related_relations': relationtypes.filter(Q(category='Mapping Properties') | Q(relationtype = 'related')),
                'concept_paths': concept_graph.get_paths(lang=lang),
                'graph_json': JSONSerializer().serialize(concept_graph.get_node_and_links(lang=lang)),
                'direct_parents': [parent.get_preflabel(lang=lang) for parent in concept_graph.parentconcepts]
            })
        elif mode == 'collections':
            return render(request, 'views/rdm/entitytype-report.htm', {
                'lang': lang,
                'prefLabel': prefLabel,
                'labels': labels,
                'concept': concept_graph,
                'languages': languages,
                'valuetype_labels': valuetypes.filter(category='label'),
                'valuetype_notes': valuetypes.filter(category='note'),
                'valuetype_related_values': valuetypes.filter(category__in=['undefined','identifiers']),
                'related_relations': relationtypes.filter(relationtype = 'member'),
                'concept_paths': concept_graph.get_paths(lang=lang)
            })

    if request.method == 'POST':

        if len(request.FILES) > 0:
            skosfile = request.FILES.get('skosfile', None)
            imagefile = request.FILES.get('file', None)

            if imagefile:
                value = models.FileValue(valueid = str(uuid.uuid4()), value = request.FILES.get('file', None), concept_id = conceptid, valuetype_id = 'image',language_id = settings.LANGUAGE_CODE)
                value.save()
                return JSONResponse(value)

            elif skosfile:
                overwrite_options = request.POST.get('overwrite_options', None)
                staging_options = request.POST.get('staging_options', None)
                skos = SKOSReader()
                try:
                    rdf = skos.read_file(skosfile)
                    ret = skos.save_concepts_from_skos(rdf, overwrite_options, staging_options)
                    return JSONResponse(ret)
                except:
                    return JSONResponse({'message':{'title': _('Unable to Load SKOS File'), 'text': _('There was an issue saving the contents of the file to Arches.')}}, status=500)

        else:
            data = JSONDeserializer().deserialize(request.body)
            if data:
                with transaction.atomic():
                    concept = Concept(data)
                    concept.save()
                    concept.index()

                    return JSONResponse(concept)


    if request.method == 'DELETE':
        data = JSONDeserializer().deserialize(request.body)
        if data:
            with transaction.atomic():
                concept = Concept(data)
                delete_self = data['delete_self'] if 'delete_self' in data else False
                if not (delete_self and concept.id in CORE_CONCEPTS):
                    if concept.nodetype == 'Collection':
                        concept.delete(delete_self=delete_self)
                    else:
                        in_use = False
                        if delete_self:
                            check_concept = Concept().get(data['id'], include_subconcepts=True)
                            in_use = check_concept.check_if_concept_in_use()
                        if 'subconcepts' in data:
                            for subconcept in data['subconcepts']:
                                if in_use == False:
                                    check_concept = Concept().get(subconcept['id'], include_subconcepts=True)
                                    in_use = check_concept.check_if_concept_in_use()

                        if in_use == False:
                            concept.delete_index(delete_self=delete_self)
                            concept.delete(delete_self=delete_self)
                        else:
                            return JSONResponse({"in_use": in_use, 'message':{'title': _('Unable to Delete'), 'text': _('This concept or one of it\'s subconcepts is already in use by an existing resource.')}})

                return JSONResponse(concept)

    return HttpResponseNotFound
Ejemplo n.º 14
0
    def setUpClass(cls):
        # This runs once per instantiation
        cls.loadOntology()
        cls.factory = RequestFactory()
        cls.token = "abc123"
        cls.client = Client(HTTP_AUTHORIZATION="Bearer %s" % cls.token)

        sql_str = CREATE_TOKEN_SQL.format(token=cls.token, user_id=1)
        cursor = connection.cursor()
        cursor.execute(sql_str)

        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/jsonld_base/rdm/jsonld_test_thesaurus.xml")
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/jsonld_base/rdm/jsonld_test_collections.xml")
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/jsonld_base/rdm/5098-thesaurus.xml")
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/jsonld_base/rdm/5098-collections.xml")
        ret = skos.save_concepts_from_skos(rdf)

        # Load up the models and data only once
        with open(os.path.join("tests/fixtures/jsonld_base/models/test_1_basic_object.json"), "rU") as f:
            archesfile = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile["graph"])

        with open(os.path.join("tests/fixtures/jsonld_base/models/test_2_complex_object.json"), "rU") as f:
            archesfile2 = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile2["graph"])

        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/jsonld_base/rdm/5098-thesaurus.xml")
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/jsonld_base/rdm/5098-collections.xml")
        ret = skos.save_concepts_from_skos(rdf)

        with open(os.path.join("tests/fixtures/jsonld_base/models/5098_concept_list.json"), "rU") as f:
            archesfile = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile["graph"])

        management.call_command("datatype", "register", source="tests/fixtures/datatypes/color.py")
        management.call_command("datatype", "register", source="tests/fixtures/datatypes/semantic_like.py")

        with open(os.path.join("tests/fixtures/jsonld_base/models/5299-basic.json"), "rU") as f:
            archesfile2 = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile2["graph"])
        with open(os.path.join("tests/fixtures/jsonld_base/models/5299_complex.json"), "rU") as f:
            archesfile2 = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile2["graph"])

        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/jsonld_base/rdm/5600-external-thesaurus.xml")
        ret = skos.save_concepts_from_skos(rdf)

        skos = SKOSReader()
        rdf = skos.read_file("tests/fixtures/jsonld_base/rdm/5600-external-collections.xml")
        ret = skos.save_concepts_from_skos(rdf)

        # Load up the models and data only once
        with open(os.path.join("tests/fixtures/jsonld_base/models/5121_false_ambiguity.json"), "rU") as f:
            archesfile = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile["graph"])

        with open(os.path.join("tests/fixtures/jsonld_base/models/5121_external_model.json"), "rU") as f:
            archesfile = JSONDeserializer().deserialize(f)
        ResourceGraphImporter(archesfile["graph"])
Ejemplo n.º 15
0
def concept(request, conceptid):
    f = request.GET.get("f", "json")
    mode = request.GET.get("mode", "")
    lang = request.GET.get("lang", request.LANGUAGE_CODE)
    pretty = request.GET.get("pretty", False)

    if request.method == "GET":
        include_subconcepts = request.GET.get("include_subconcepts",
                                              "true") == "true"
        include_parentconcepts = request.GET.get("include_parentconcepts",
                                                 "true") == "true"
        include_relatedconcepts = request.GET.get("include_relatedconcepts",
                                                  "true") == "true"
        emulate_elastic_search = request.GET.get("emulate_elastic_search",
                                                 "false") == "true"
        depth_limit = request.GET.get("depth_limit", None)

        depth_limit = 1
        if not conceptid:
            return render(
                request,
                "views/rdm/concept-report.htm",
                {
                    "lang":
                    lang,
                    "concept_count":
                    models.Concept.objects.filter(nodetype="Concept").count(),
                    "collection_count":
                    models.Concept.objects.filter(
                        nodetype="Collection").count(),
                    "scheme_count":
                    models.Concept.objects.filter(
                        nodetype="ConceptScheme").count(),
                    "entitytype_count":
                    models.Concept.objects.filter(
                        nodetype="EntityType").count(),
                    "default_report":
                    True,
                },
            )

        labels = []

        concept_graph = Concept().get(
            id=conceptid,
            include_subconcepts=include_subconcepts,
            include_parentconcepts=include_parentconcepts,
            include_relatedconcepts=include_relatedconcepts,
            depth_limit=depth_limit,
            up_depth_limit=None,
            lang=lang,
            semantic=(mode == "semantic" or mode == ""),
        )

        languages = sort_languages(models.DLanguage.objects.all(), lang)

        valuetypes = models.DValueType.objects.all()
        relationtypes = models.DRelationType.objects.all()
        prefLabel = concept_graph.get_preflabel(lang=lang)
        for subconcept in concept_graph.subconcepts:
            subconcept.prefLabel = subconcept.get_preflabel(lang=lang)
        for relatedconcept in concept_graph.relatedconcepts:
            relatedconcept.prefLabel = relatedconcept.get_preflabel(lang=lang)
        for value in concept_graph.values:
            if value.category == "label":
                labels.append(value)
            if value.type == "image":
                value.full_image_url = (
                    (settings.FORCE_SCRIPT_NAME
                     if settings.FORCE_SCRIPT_NAME is not None else "") +
                    settings.MEDIA_URL + value.value).replace("//", "/")

        if (mode == "semantic"
                or mode == "") and (concept_graph.nodetype == "Concept" or
                                    concept_graph.nodetype == "ConceptScheme"
                                    or concept_graph.nodetype == "EntityType"):
            if concept_graph.nodetype == "ConceptScheme":
                parent_relations = relationtypes.filter(category="Properties")
            else:
                parent_relations = (relationtypes.filter(
                    category="Semantic Relations").exclude(
                        relationtype="related").exclude(
                            relationtype="broader").exclude(
                                relationtype="broaderTransitive"))
            return render(
                request,
                "views/rdm/concept-report.htm",
                {
                    "FORCE_SCRIPT_NAME":
                    settings.FORCE_SCRIPT_NAME,
                    "lang":
                    lang,
                    "prefLabel":
                    prefLabel,
                    "labels":
                    labels,
                    "concept":
                    concept_graph,
                    "languages":
                    languages,
                    "sparql_providers":
                    get_sparql_providers(),
                    "valuetype_labels":
                    valuetypes.filter(category="label"),
                    "valuetype_notes":
                    valuetypes.filter(category="note"),
                    "valuetype_related_values":
                    valuetypes.filter(
                        category__in=["undefined", "identifiers"]),
                    "parent_relations":
                    parent_relations,
                    "related_relations":
                    relationtypes.filter(
                        Q(category="Mapping Properties")
                        | Q(relationtype="related")),
                    "concept_paths":
                    concept_graph.get_paths(lang=lang),
                    "graph_json":
                    JSONSerializer().serialize(
                        concept_graph.get_node_and_links(lang=lang)),
                    "direct_parents": [
                        parent.get_preflabel(lang=lang)
                        for parent in concept_graph.parentconcepts
                    ],
                },
            )
        elif mode == "collections":
            return render(
                request,
                "views/rdm/entitytype-report.htm",
                {
                    "lang":
                    lang,
                    "prefLabel":
                    prefLabel,
                    "labels":
                    labels,
                    "concept":
                    concept_graph,
                    "languages":
                    languages,
                    "valuetype_labels":
                    valuetypes.filter(category="label"),
                    "valuetype_notes":
                    valuetypes.filter(category="note"),
                    "valuetype_related_values":
                    valuetypes.filter(
                        category__in=["undefined", "identifiers"]),
                    "related_relations":
                    relationtypes.filter(relationtype="member"),
                    "concept_paths":
                    concept_graph.get_paths(lang=lang),
                },
            )

    if request.method == "POST":

        if len(request.FILES) > 0:
            skosfile = request.FILES.get("skosfile", None)
            imagefile = request.FILES.get("file", None)

            if imagefile:
                value = models.FileValue(
                    valueid=str(uuid.uuid4()),
                    value=request.FILES.get("file", None),
                    concept_id=conceptid,
                    valuetype_id="image",
                    language_id=lang,
                )
                value.save()
                return JSONResponse(value)

            elif skosfile:
                overwrite_options = request.POST.get("overwrite_options", None)
                staging_options = request.POST.get("staging_options", None)
                skos = SKOSReader()
                try:
                    rdf = skos.read_file(skosfile)
                    ret = skos.save_concepts_from_skos(rdf, overwrite_options,
                                                       staging_options)
                    return JSONResponse(ret)
                except Exception as e:
                    return JSONErrorResponse(
                        _('Unable to Load SKOS File'),
                        _('There was an issue saving the contents of the file to Arches. '
                          ) + str(e))

        else:
            data = JSONDeserializer().deserialize(request.body)
            if data:
                with transaction.atomic():
                    concept = Concept(data)
                    concept.save()
                    concept.index()

                    return JSONResponse(concept)

    if request.method == "DELETE":
        data = JSONDeserializer().deserialize(request.body)
        if data:
            with transaction.atomic():
                concept = Concept(data)
                delete_self = data[
                    "delete_self"] if "delete_self" in data else False
                if not (delete_self and concept.id in CORE_CONCEPTS):
                    if concept.nodetype == "Collection":
                        concept.delete(delete_self=delete_self)
                    else:
                        in_use = False
                        if delete_self:
                            check_concept = Concept().get(
                                data["id"], include_subconcepts=True)
                            in_use = check_concept.check_if_concept_in_use()
                        if "subconcepts" in data:
                            for subconcept in data["subconcepts"]:
                                if in_use == False:
                                    check_concept = Concept().get(
                                        subconcept["id"],
                                        include_subconcepts=True)
                                    in_use = check_concept.check_if_concept_in_use(
                                    )

                        if in_use == False:
                            concept.delete_index(delete_self=delete_self)
                            concept.delete(delete_self=delete_self)
                        else:
                            return JSONErrorResponse(
                                _('Unable to Delete'),
                                _('This concept or one of it\'s subconcepts is already in use by an existing resource.'
                                  ), {"in_use": in_use})

                return JSONResponse(concept)

    return HttpResponseNotFound
Ejemplo n.º 16
0
def concept(request, conceptid):
    f = request.GET.get('f', 'json')
    mode = request.GET.get('mode', '')
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    pretty = request.GET.get('pretty', False)

    if request.method == 'GET':
        include_subconcepts = request.GET.get('include_subconcepts', 'true') == 'true'
        include_parentconcepts = request.GET.get('include_parentconcepts', 'true') == 'true'
        include_relatedconcepts = request.GET.get('include_relatedconcepts', 'true') == 'true'
        emulate_elastic_search = request.GET.get('emulate_elastic_search', 'false') == 'true'
        depth_limit = request.GET.get('depth_limit', None)

        if f == 'html':
            depth_limit = 1
            if not conceptid:
                return render(request, 'views/rdm/concept-report.htm', {
                    'lang': lang,
                    'concept_count': models.Concept.objects.filter(nodetype='Concept').count(),
                    'collection_count': models.Concept.objects.filter(nodetype='Collection').count(),
                    'scheme_count': models.Concept.objects.filter(nodetype='ConceptScheme').count(),
                    'entitytype_count': models.Concept.objects.filter(nodetype='EntityType').count(),
                    'default_report': True
                })


            labels = []

            concept_graph = Concept().get(id=conceptid, include_subconcepts=include_subconcepts,
                include_parentconcepts=include_parentconcepts, include_relatedconcepts=include_relatedconcepts,
                depth_limit=depth_limit, up_depth_limit=None, lang=lang, semantic=(mode == 'semantic' or mode == ''))

            languages = sort_languages(models.DLanguage.objects.all(), lang)

            valuetypes = models.DValueType.objects.all()
            relationtypes = models.DRelationType.objects.all()
            prefLabel = concept_graph.get_preflabel(lang=lang)
            for subconcept in concept_graph.subconcepts:
                subconcept.prefLabel = subconcept.get_preflabel(lang=lang)
            for relatedconcept in concept_graph.relatedconcepts:
                relatedconcept.prefLabel = relatedconcept.get_preflabel(lang=lang)
            for value in concept_graph.values:
                if value.category == 'label':
                    labels.append(value)

            if (mode == 'semantic' or mode == '') and (concept_graph.nodetype == 'Concept' or concept_graph.nodetype == 'ConceptScheme' or concept_graph.nodetype == 'EntityType'):
                if concept_graph.nodetype == 'ConceptScheme':
                    parent_relations = relationtypes.filter(category='Properties')
                else:
                    parent_relations = relationtypes.filter(category='Semantic Relations').exclude(relationtype = 'related').exclude(relationtype='broader').exclude(relationtype='broaderTransitive')
                return render(request, 'views/rdm/concept-report.htm', {
                    'lang': lang,
                    'prefLabel': prefLabel,
                    'labels': labels,
                    'concept': concept_graph,
                    'languages': languages,
                    'sparql_providers': get_sparql_providers(),
                    'valuetype_labels': valuetypes.filter(category='label'),
                    'valuetype_notes': valuetypes.filter(category='note'),
                    'valuetype_related_values': valuetypes.filter(category__in=['undefined','identifiers']),
                    'parent_relations': parent_relations,
                    'related_relations': relationtypes.filter(Q(category='Mapping Properties') | Q(relationtype = 'related')),
                    'concept_paths': concept_graph.get_paths(lang=lang),
                    'graph_json': JSONSerializer().serialize(concept_graph.get_node_and_links(lang=lang)),
                    'direct_parents': [parent.get_preflabel(lang=lang) for parent in concept_graph.parentconcepts]
                })
            elif mode == 'collections':
                return render(request, 'views/rdm/entitytype-report.htm', {
                    'lang': lang,
                    'prefLabel': prefLabel,
                    'labels': labels,
                    'concept': concept_graph,
                    'languages': languages,
                    'valuetype_labels': valuetypes.filter(category='label'),
                    'valuetype_notes': valuetypes.filter(category='note'),
                    'valuetype_related_values': valuetypes.filter(category__in=['undefined','identifiers']),
                    'related_relations': relationtypes.filter(relationtype = 'member'),
                    'concept_paths': concept_graph.get_paths(lang=lang)
                })


        ret = []
        concept_graph = Concept().get(id=conceptid, include_subconcepts=include_subconcepts,
                include_parentconcepts=include_parentconcepts, include_relatedconcepts=include_relatedconcepts,
                depth_limit=depth_limit, up_depth_limit=None, lang=lang)

        if emulate_elastic_search:
            ret.append({'_type': id, '_source': concept_graph})
        else:
            ret.append(concept_graph)

        if emulate_elastic_search:
            ret = {'hits':{'hits':ret}}

        return JSONResponse(ret, indent=4 if pretty else None)

    if request.method == 'POST':

        if len(request.FILES) > 0:
            skosfile = request.FILES.get('skosfile', None)
            imagefile = request.FILES.get('file', None)

            if imagefile:
                value = models.FileValue(valueid = str(uuid.uuid4()), value = request.FILES.get('file', None), concept_id = conceptid, valuetype_id = 'image',language_id = settings.LANGUAGE_CODE)
                value.save()
                return JSONResponse(value)

            elif skosfile:
                overwrite_options = request.POST.get('overwrite_options', None)
                staging_options = request.POST.get('staging_options', None)
                skos = SKOSReader()
                try:
                    rdf = skos.read_file(skosfile)
                    ret = skos.save_concepts_from_skos(rdf, overwrite_options, staging_options)
                    return JSONResponse(ret)
                except:
                    return JSONResponse({'message':{'title': _('Unable to Load SKOS File'), 'text': _('There was an issue saving the contents of the file to Arches.')}}, status=500)

        else:
            data = JSONDeserializer().deserialize(request.body)
            if data:
                with transaction.atomic():
                    concept = Concept(data)
                    concept.save()
                    concept.index()

                    return JSONResponse(concept)


    if request.method == 'DELETE':
        data = JSONDeserializer().deserialize(request.body)
        if data:
            with transaction.atomic():
                concept = Concept(data)
                delete_self = data['delete_self'] if 'delete_self' in data else False
                if not (delete_self and concept.id in CORE_CONCEPTS):
                    if concept.nodetype == 'Collection':
                        concept.delete(delete_self=delete_self)
                    else:
                        in_use = False
                        if delete_self:
                            check_concept = Concept().get(data['id'], include_subconcepts=True)
                            in_use = check_concept.check_if_concept_in_use()
                        if 'subconcepts' in data:
                            for subconcept in data['subconcepts']:
                                if in_use == False:
                                    check_concept = Concept().get(subconcept['id'], include_subconcepts=True)
                                    in_use = check_concept.check_if_concept_in_use()

                        if in_use == False:
                            concept.delete_index(delete_self=delete_self)
                            concept.delete(delete_self=delete_self)
                        else:
                            return JSONResponse({"in_use": in_use, 'message':{'title': _('Unable to Delete'), 'text': _('This concept or one of it\'s subconcepts is already in use by an existing resource.')}})

                return JSONResponse(concept)

    return HttpResponseNotFound