Exemplo n.º 1
0
    def test_prefer_preflabel_with_just_lang_code_match_over_exact_match_with_altlabel(self):
        """
        Given a language and region, test should pick the preflabel even if that preflabel specifies a language only 
        and even if an altlabel exists with the exact match
        
        """

        concept = Concept()
        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en',
                'language': 'en'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        self.assertEqual(concept.get_preflabel(lang='en-US').value, 'test pref label en')
Exemplo n.º 2
0
    def test_get_region_specific_preflabel_when_language_only_version_does_not_exist(self):
        """
        Given a language only and the system only has values that with regions specified, then 
        the system should pick the first preflabel with the same language code 
        
        """

        concept = Concept()
        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en-US',
                'language': 'en-US'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        self.assertEqual(concept.get_preflabel(lang='en').value, 'test pref label en-US')
Exemplo n.º 3
0
    def test_get_label_no_exact_match(self):
        """
        Given no match on language code or region, return the first prefLabel found

        """

        concept = Concept()
        concept.values = [
            ConceptValue({
                'type': 'prefLabel', 
                'value': 'bier', 
                'language': 'nl'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'beer',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        pl = concept.get_preflabel('fr-BE')

        self.assertEquals(pl.type,'prefLabel')
        self.assertEquals(pl.value,'bier' or 'beer')
        self.assertEquals(pl.language,'nl' or 'es-SP')
Exemplo n.º 4
0
def confirm_delete(request, conceptid):
    lang = request.GET.get("lang", settings.LANGUAGE_CODE)
    concept = Concept().get(id=conceptid)
    concepts_to_delete = [
        concept.get_preflabel(lang=lang).value
        for key, concept in Concept.gather_concepts_to_delete(concept, lang=lang).iteritems()
    ]
    # return HttpResponse('<div>Showing only 50 of %s concepts</div><ul><li>%s</ul>' % (len(concepts_to_delete), '<li>'.join(concepts_to_delete[:50]) + ''))
    return HttpResponse("<ul><li>%s</ul>" % ("<li>".join(concepts_to_delete) + ""))
Exemplo n.º 5
0
def confirm_delete(request, conceptid):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE) 
    try:
        concept = Concept().get(id=conceptid)
    except Exception as es1:
        return render_to_response('404.htm', {
            'main_script': '404',
            'active_page': '404'
        },
        context_instance=RequestContext(request))
    concepts_to_delete = [concept.get_preflabel(lang=lang).value for key, concept in Concept.gather_concepts_to_delete(concept, lang=lang).iteritems()]
    #return HttpResponse('<div>Showing only 50 of %s concepts</div><ul><li>%s</ul>' % (len(concepts_to_delete), '<li>'.join(concepts_to_delete[:50]) + ''))
    return HttpResponse('<ul><li>%s</ul>' % ('<li>'.join(concepts_to_delete) + ''))
Exemplo n.º 6
0
    def test_prefer_preflabel_over_altlabel(self):
        """
        Test to confirm the proper retrieval of the prefLabel based on different language requirements

        """

        concept = Concept()
        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en-US',
                'language': 'en-US'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en',
                'language': 'en'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es-SP',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        self.assertEqual(concept.get_preflabel(lang='en-US').value, 'test pref label en-US')
        self.assertEqual(concept.get_preflabel(lang='en').value, 'test pref label en')
        self.assertEqual(concept.get_preflabel().value, 'test pref label %s' % (test_settings.LANGUAGE_CODE))
Exemplo n.º 7
0
    def test_get_label_no_exact_match(self):
        """
        Given no match on language code or region, return the first prefLabel found

        """

        values = [
            {'type': 'prefLabel', 'value': 'bier', 'language': 'nl'},
            {'type': 'prefLabel', 'value': 'beer', 'language': 'en'},
        ]
        c = Concept({'values': values})
        pl = c.get_preflabel('fr-BE')
        self.assertEquals(pl.type,'prefLabel')
        self.assertEquals(pl.value,'bier')
        self.assertEquals(pl.language,'nl')
Exemplo n.º 8
0
    def test_get_altlabel_when_no_preflabel_exists(self):
        """
        Given a language and region, test should pick the altlabel when no preflabel exists
        
        """

        concept = Concept()
        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        self.assertEqual(concept.get_preflabel(lang='en-US').value, 'test alt label en-US')
Exemplo n.º 9
0
    def test_get_altlabel_when_no_preflabel_exists(self):
        """
        Given a language and region, test should pick the altlabel when no preflabel exists
        
        """

        concept = Concept()
        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        self.assertEqual(
            concept.get_preflabel(lang='en-US').value, 'test alt label en-US')
Exemplo n.º 10
0
    def test_get_altlabel_when_no_preflabel_exists_and_altlabel_only_specifies_lang_code(self):
        """
        Given a language and region and the system only has values that specify a language code, the 
        the system should pick the altlabel even if the altlabel doesn't specifiy a region
        
        """

        concept = Concept()
        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en',
                'language': 'en'
            })
        ]

        self.assertEqual(concept.get_preflabel(lang='en-US').value, 'test alt label en')
Exemplo n.º 11
0
def confirm_delete(request, conceptid):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    concept = Concept().get(id=conceptid)
    concepts_to_delete = [concept.get_preflabel(lang=lang).value for key, concept in Concept.gather_concepts_to_delete(concept, lang=lang).iteritems()]
    #return HttpResponse('<div>Showing only 50 of %s concepts</div><ul><li>%s</ul>' % (len(concepts_to_delete), '<li>'.join(concepts_to_delete[:50]) + ''))
    return HttpResponse('<ul><li>%s</ul>' % ('<li>'.join(concepts_to_delete) + ''))
Exemplo n.º 12
0
def create_mapping_configuration_file(graphid, data_dir=None):
    files_for_export = []
    graphid = uuid.UUID(graphid)
    nodes = []
    values = {}
    export_json = OrderedDict()
    if graphid != False:
        if graphid == None or graphid == 'all' or graphid == ['']:
            node_query = Node.objects.filter(graph_id__isresource=True).exclude(pk=settings.SYSTEM_SETTINGS_RESOURCE_MODEL_ID).order_by('name')
        else:
            node_query = Node.objects.filter(graph_id=graphid).exclude(datatype='semantic').order_by('name')

        export_json['resource_model_id'] = str(node_query[0].graph_id)
        export_json['resource_model_name'] = JSONSerializer().serializeToPython(Graph.objects.filter(graphid=export_json['resource_model_id']))[0]['name']
        export_json['nodes'] = []
        for node in node_query:
            export_node = OrderedDict()
            export_node['arches_nodeid'] = str(node.nodeid)
            export_node['arches_node_name'] = node.name
            export_node['file_field_name'] = ""
            if node.datatype in ['concept', 'concept-list', 'domain-value', 'domain-value-list']:
                export_node['concept_export_value'] = "label"
            # export_node['value_type'] = ""
            # export_node['data_length'] = ""
            export_node['data_type'] = node.datatype
            export_node['export'] = False

            export_json['nodes'].append(export_node)

            concept_export={}

            def get_values(concept, values):
                for subconcept in concept.subconcepts:
                    for value in subconcept.values:
                        values[value.id] = value.value
                    get_values(subconcept, values)
                return values

            if node.datatype in ['concept', 'concept-list', 'domain-value', 'domain-value-list']:
                    if node.datatype in ['concept', 'concept-list']:
                        if node.config['rdmCollection'] != None:
                            rdmCollection = node.config['rdmCollection']
                        try:
                            concept = Concept().get(node.config['rdmCollection'], include_subconcepts=True, semantic=False)
                            rdmCollectionLabel = concept.get_preflabel().value
                            collection_values = {}
                            concepts = OrderedDict(sorted(get_values(concept, collection_values).items(), key=itemgetter(1)))
                            values[rdmCollectionLabel] = concepts
                        except:
                            values[node.name] = node.name + ' does not appear to be configured with a valid concept collectionid'
                    elif node.datatype in ['domain-value', 'domain-value-list']:
                        concepts = {}
                        if node.config['options']:
                            for concept in node.config['options']:
                                concepts[concept['id']] = concept['text']

                        values[node.name] = OrderedDict(sorted(concepts.items(), key=itemgetter(1)))

    file_name_prefix = export_json['resource_model_name']

    # Concept lookup file
    file_name = os.path.join('{0}_{1}.{2}'.format(file_name_prefix, 'concepts', 'json'))
    dest = StringIO()
    dest.write(json.dumps(values, indent=4))
    files_for_export.append({'name':file_name, 'outputfile': dest})

    # Import/Export mapping file
    file_name = os.path.join('{0}.{1}'.format(file_name_prefix, 'mapping'))
    dest = StringIO()
    dest.write(json.dumps(export_json, indent=4))
    files_for_export.append({'name':file_name, 'outputfile': dest})


    if data_dir != None:
        with open(os.path.join(data_dir), 'w') as config_file:
            json.dump(export_json, config_file, indent=4)

        file_name = Graph.objects.get(graphid=graphid).name

        buffer = StringIO()

        with zipfile.ZipFile(buffer, 'w', zipfile.ZIP_DEFLATED) as zip:
            for f in files_for_export:
                f['outputfile'].seek(0)
                zip.writestr(f['name'], f['outputfile'].read())

        zip.close()
        buffer.flush()
        zip_stream = buffer.getvalue()
        buffer.close()

        with open(os.path.join(data_dir), 'w') as archive:
            archive.write(zip_stream)
    else:
        return files_for_export
Exemplo n.º 13
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
Exemplo n.º 14
0
def create_mapping_configuration_file(graphid,
                                      include_concepts=True,
                                      data_dir=None):
    files_for_export = []
    graphid = uuid.UUID(graphid)
    nodes = []
    values = {}
    export_json = OrderedDict()
    if graphid != False:
        if graphid is None or graphid == "all" or graphid == [""]:
            node_query = (Node.objects.filter(
                graph_id__isresource=True).exclude(
                    pk=settings.SYSTEM_SETTINGS_RESOURCE_MODEL_ID).order_by(
                        "name"))
        else:
            node_query = Node.objects.filter(graph_id=graphid).exclude(
                datatype="semantic").order_by("name")

        export_json["resource_model_id"] = str(node_query[0].graph_id)
        export_json["resource_model_name"] = JSONSerializer(
        ).serializeToPython(
            Graph.objects.filter(
                graphid=export_json["resource_model_id"]))[0]["name"]
        export_json["nodes"] = []
        file_name_prefix = export_json["resource_model_name"]

        for node in node_query:
            export_node = OrderedDict()
            export_node["arches_nodeid"] = str(node.nodeid)
            export_node["arches_node_name"] = node.name
            export_node["file_field_name"] = node.name
            export_node["data_type"] = node.datatype
            if node.datatype in [
                    "concept", "concept-list", "domain-value",
                    "domain-value-list"
            ]:
                export_node["concept_export_value"] = "label"
            # export_node['value_type'] = ""
            # export_node['data_length'] = ""
            export_node["export"] = True

            export_json["nodes"].append(export_node)

            if include_concepts == True:
                concept_export = {}

                def get_values(concept, values):
                    for subconcept in concept.subconcepts:
                        for value in subconcept.values:
                            if value.type == "prefLabel":
                                values[value.id] = value.value
                        get_values(subconcept, values)
                    return values

                if node.datatype in [
                        "concept", "concept-list", "domain-value",
                        "domain-value-list"
                ]:
                    if node.datatype in ["concept", "concept-list"]:
                        if node.config["rdmCollection"] is not None:
                            rdmCollection = node.config["rdmCollection"]
                        try:
                            concept = Concept().get(
                                node.config["rdmCollection"],
                                include_subconcepts=True,
                                semantic=False)
                            rdmCollectionLabel = concept.get_preflabel().value
                            collection_values = {}
                            concepts = OrderedDict(
                                sorted(list(
                                    get_values(concept,
                                               collection_values).items()),
                                       key=itemgetter(1)))
                            values[rdmCollectionLabel] = concepts
                        except:
                            values[
                                node.
                                name] = node.name + " does not appear to be configured with a valid concept collectionid"
                    elif node.datatype in [
                            "domain-value", "domain-value-list"
                    ]:
                        concepts = {}
                        if node.config["options"]:
                            for concept in node.config["options"]:
                                concepts[concept["id"]] = concept["text"]

                        values[node.name] = OrderedDict(
                            sorted(list(concepts.items()), key=itemgetter(1)))

        if include_concepts == True:
            try:
                relation_concepts = OrderedDict(
                    sorted(
                        list(
                            get_values(
                                Concept().get(
                                    "00000000-0000-0000-0000-000000000005",
                                    include_subconcepts=True,
                                    semantic=False), {}).items()),
                        key=itemgetter(1),
                    ))
            except:
                relations_concepts = "You do not appear to have values for resource to resource relationships in your rdm."
            values[
                "Resource to Resource Relationship Types"] = relation_concepts

    # Concept lookup file
    if include_concepts == True:
        file_name = os.path.join("{0}_{1}.{2}".format(file_name_prefix,
                                                      "concepts", "json"))
        dest = StringIO()
        dest.write(json.dumps(values, indent=4))
        files_for_export.append({"name": file_name, "outputfile": dest})

    # Import/Export mapping file
    file_name = os.path.join("{0}.{1}".format(file_name_prefix, "mapping"))
    dest = StringIO()
    dest.write(json.dumps(export_json, indent=4))
    files_for_export.append({"name": file_name, "outputfile": dest})

    if data_dir is not None:
        with open(os.path.join(data_dir), "w") as config_file:
            json.dump(export_json, config_file, indent=4)

        file_name = Graph.objects.get(graphid=graphid).name
        buffer = StringIO()
        with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as zip:
            for f in files_for_export:
                f["outputfile"].seek(0)
                zip.writestr(f["name"], f["outputfile"].read())

        zip.close()
        buffer.flush()
        zip_stream = buffer.getvalue()
        buffer.close()
        with open(os.path.join(data_dir), "w") as archive:
            archive.write(zip_stream)
    else:
        return files_for_export
Exemplo n.º 15
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_to_response(
                    'views/rdm/concept-report.htm', {
                        'lang':
                        lang,
                        'concept_count':
                        models.Concepts.objects.filter(
                            nodetype='Concept').count(),
                        'collection_count':
                        models.Concepts.objects.filter(
                            nodetype='Collection').count(),
                        'scheme_count':
                        models.Concepts.objects.filter(
                            nodetype='ConceptScheme').count(),
                        'entitytype_count':
                        models.Concepts.objects.filter(
                            nodetype='EntityType').count(),
                        'default_report':
                        True
                    },
                    context_instance=RequestContext(request))

        ret = []
        labels = []
        this_concept = Concept().get(id=conceptid)

        if f == 'html':
            if mode == '' and (this_concept.nodetype == 'Concept'
                               or this_concept.nodetype == 'ConceptScheme'
                               or this_concept.nodetype == 'EntityType'):
                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)
            else:
                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=False)

            languages = models.DLanguages.objects.all()
            valuetypes = models.ValueTypes.objects.all()
            relationtypes = models.DRelationtypes.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 == '' and (this_concept.nodetype == 'Concept'
                               or this_concept.nodetype == 'ConceptScheme'
                               or this_concept.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_to_response(
                    'views/rdm/concept-report.htm', {
                        'lang':
                        lang,
                        'prefLabel':
                        prefLabel,
                        'labels':
                        labels,
                        'concept':
                        concept_graph,
                        'languages':
                        languages,
                        'sparql_providers':
                        sparql_providers,
                        'valuetype_labels':
                        valuetypes.filter(category='label'),
                        'valuetype_notes':
                        valuetypes.filter(category='note'),
                        'valuetype_related_values':
                        valuetypes.filter(category='undefined'),
                        '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
                        ]
                    },
                    context_instance=RequestContext(request))
            else:
                return render_to_response(
                    '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='undefined'),
                        'related_relations':
                        relationtypes.filter(relationtype='member'),
                        'concept_paths':
                        concept_graph.get_paths(lang=lang)
                    },
                    context_instance=RequestContext(request))

        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 f == 'skos':
            include_parentconcepts = False
            include_subconcepts = True
            depth_limit = None
            skos = SKOSWriter()
            return HttpResponse(skos.write(concept_graph, format="pretty-xml"),
                                content_type="application/xml")

        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.FileValues(valueid=str(uuid.uuid4()),
                                          value=request.FILES.get(
                                              'file', None),
                                          conceptid_id=conceptid,
                                          valuetype_id='image',
                                          languageid_id=settings.LANGUAGE_CODE)
                value.save()
                return JSONResponse(value)

            elif skosfile:
                skos = SKOSReader()
                rdf = skos.read_file(skosfile)
                ret = skos.save_concepts_from_skos(rdf)
                return JSONResponse(ret)

        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):
                    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})

                return JSONResponse(concept)

    return HttpResponseNotFound
Exemplo n.º 16
0
    def test_prefLabel(self):
        """
        Test to confirm the proper retrieval of the prefLabel based on different language requirements

        """

        concept = Concept()
        concept.nodetype = 'Concept'
        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en-US',
                'language': 'en-US'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en',
                'language': 'en'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es-SP',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        self.assertEqual(
            concept.get_preflabel(lang='en-US').value, 'test pref label en-US')
        self.assertEqual(
            concept.get_preflabel(lang='en').value, 'test pref label en')
        self.assertEqual(concept.get_preflabel().value,
                         'test pref label %s' % (test_settings.LANGUAGE_CODE))

        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en',
                'language': 'en'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        # should pick the base language if it can't find the more specific version
        self.assertEqual(
            concept.get_preflabel(lang='en-US').value, 'test pref label en')

        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        self.assertEqual(
            concept.get_preflabel(lang='en-US').value, 'test alt label en-US')

        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en',
                'language': 'en'
            })
        ]

        self.assertEqual(
            concept.get_preflabel(lang='en-US').value, 'test alt label en')

        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en-US',
                'language': 'en-US'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        self.assertEqual(
            concept.get_preflabel(lang='en').value, 'test pref label en-US')
Exemplo n.º 17
0
def create_mapping_configuration_file(graphid,
                                      include_concepts=True,
                                      data_dir=None):
    files_for_export = []
    graphid = uuid.UUID(graphid)
    nodes = []
    values = {}
    export_json = OrderedDict()
    if graphid != False:
        if graphid == None or graphid == 'all' or graphid == ['']:
            node_query = Node.objects.filter(
                graph_id__isresource=True).exclude(
                    pk=settings.SYSTEM_SETTINGS_RESOURCE_MODEL_ID).order_by(
                        'name')
        else:
            node_query = Node.objects.filter(graph_id=graphid).exclude(
                datatype='semantic').order_by('name')

        export_json['resource_model_id'] = str(node_query[0].graph_id)
        export_json['resource_model_name'] = JSONSerializer(
        ).serializeToPython(
            Graph.objects.filter(
                graphid=export_json['resource_model_id']))[0]['name']
        export_json['nodes'] = []
        file_name_prefix = export_json['resource_model_name']

        for node in node_query:
            export_node = OrderedDict()
            export_node['arches_nodeid'] = str(node.nodeid)
            export_node['arches_node_name'] = node.name
            export_node['file_field_name'] = node.name
            export_node['data_type'] = node.datatype
            if node.datatype in [
                    'concept', 'concept-list', 'domain-value',
                    'domain-value-list'
            ]:
                export_node['concept_export_value'] = "label"
            # export_node['value_type'] = ""
            # export_node['data_length'] = ""
            export_node['export'] = False

            export_json['nodes'].append(export_node)

            if include_concepts == True:
                concept_export = {}

                def get_values(concept, values):
                    for subconcept in concept.subconcepts:
                        for value in subconcept.values:
                            if value.type == 'prefLabel':
                                values[value.id] = value.value
                        get_values(subconcept, values)
                    return values

                if node.datatype in [
                        'concept', 'concept-list', 'domain-value',
                        'domain-value-list'
                ]:
                    if node.datatype in ['concept', 'concept-list']:
                        if node.config['rdmCollection'] != None:
                            rdmCollection = node.config['rdmCollection']
                        try:
                            concept = Concept().get(
                                node.config['rdmCollection'],
                                include_subconcepts=True,
                                semantic=False)
                            rdmCollectionLabel = concept.get_preflabel().value
                            collection_values = {}
                            concepts = OrderedDict(
                                sorted(get_values(concept,
                                                  collection_values).items(),
                                       key=itemgetter(1)))
                            values[rdmCollectionLabel] = concepts
                        except:
                            values[
                                node.
                                name] = node.name + ' does not appear to be configured with a valid concept collectionid'
                    elif node.datatype in [
                            'domain-value', 'domain-value-list'
                    ]:
                        concepts = {}
                        if node.config['options']:
                            for concept in node.config['options']:
                                concepts[concept['id']] = concept['text']

                        values[node.name] = OrderedDict(
                            sorted(concepts.items(), key=itemgetter(1)))

        if include_concepts == True:
            try:
                relation_concepts = OrderedDict(
                    sorted(get_values(
                        Concept().get('00000000-0000-0000-0000-000000000005',
                                      include_subconcepts=True,
                                      semantic=False), {}).items(),
                           key=itemgetter(1)))
            except:
                relations_concepts = 'You do not appear to have values for resource to resource relationships in your rdm.'
            values[
                'Resource to Resource Relationship Types'] = relation_concepts

    # Concept lookup file
    if include_concepts == True:
        file_name = os.path.join('{0}_{1}.{2}'.format(file_name_prefix,
                                                      'concepts', 'json'))
        dest = StringIO()
        dest.write(json.dumps(values, indent=4))
        files_for_export.append({'name': file_name, 'outputfile': dest})

    # Import/Export mapping file
    file_name = os.path.join('{0}.{1}'.format(file_name_prefix, 'mapping'))
    dest = StringIO()
    dest.write(json.dumps(export_json, indent=4))
    files_for_export.append({'name': file_name, 'outputfile': dest})

    if data_dir != None:
        with open(os.path.join(data_dir), 'w') as config_file:
            json.dump(export_json, config_file, indent=4)

        file_name = Graph.objects.get(graphid=graphid).name

        buffer = StringIO()

        with zipfile.ZipFile(buffer, 'w', zipfile.ZIP_DEFLATED) as zip:
            for f in files_for_export:
                f['outputfile'].seek(0)
                zip.writestr(f['name'], f['outputfile'].read())

        zip.close()
        buffer.flush()
        zip_stream = buffer.getvalue()
        buffer.close()

        with open(os.path.join(data_dir), 'w') as archive:
            archive.write(zip_stream)
    else:
        return files_for_export
Exemplo n.º 18
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_to_response(
                    "views/rdm/concept-report.htm",
                    {
                        "lang": lang,
                        "concept_count": models.Concepts.objects.filter(nodetype="Concept").count(),
                        "collection_count": models.Concepts.objects.filter(nodetype="Collection").count(),
                        "scheme_count": models.Concepts.objects.filter(nodetype="ConceptScheme").count(),
                        "entitytype_count": models.Concepts.objects.filter(nodetype="EntityType").count(),
                        "default_report": True,
                    },
                    context_instance=RequestContext(request),
                )

        ret = []
        labels = []
        this_concept = Concept().get(id=conceptid)

        if f == "html":
            if mode == "" and (
                this_concept.nodetype == "Concept"
                or this_concept.nodetype == "ConceptScheme"
                or this_concept.nodetype == "EntityType"
            ):
                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,
                )
            else:
                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=False,
                )

            languages = models.DLanguages.objects.all()
            valuetypes = models.ValueTypes.objects.all()
            relationtypes = models.DRelationtypes.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 == "" and (
                this_concept.nodetype == "Concept"
                or this_concept.nodetype == "ConceptScheme"
                or this_concept.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_to_response(
                    "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="undefined"),
                        "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],
                    },
                    context_instance=RequestContext(request),
                )
            else:
                return render_to_response(
                    "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="undefined"),
                        "related_relations": relationtypes.filter(relationtype="member"),
                        "concept_paths": concept_graph.get_paths(lang=lang),
                    },
                    context_instance=RequestContext(request),
                )

        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 f == "skos":
            include_parentconcepts = False
            include_subconcepts = True
            depth_limit = None
            skos = SKOSWriter()
            return HttpResponse(skos.write(concept_graph, format="pretty-xml"), content_type="application/xml")

        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.FileValues(
                    valueid=str(uuid.uuid4()),
                    value=request.FILES.get("file", None),
                    conceptid_id=conceptid,
                    valuetype_id="image",
                    languageid_id=settings.LANGUAGE_CODE,
                )
                value.save()
                return JSONResponse(value)

            elif skosfile:
                skos = SKOSReader()
                rdf = skos.read_file(skosfile)
                ret = skos.save_concepts_from_skos(rdf)
                return JSONResponse(ret)

        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):
                    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})

                return JSONResponse(concept)

    return HttpResponseNotFound
Exemplo n.º 19
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
                })

        ret = []
        labels = []
        this_concept = Concept().get(id=conceptid)

        if f == 'html':
            if mode == '' and (this_concept.nodetype == 'Concept' or this_concept.nodetype == 'ConceptScheme' or this_concept.nodetype == 'EntityType'):
                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)
            else:
                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=False)

            languages = models.DLanguage.objects.all()
            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 == '' and (this_concept.nodetype == 'Concept' or this_concept.nodetype == 'ConceptScheme' or this_concept.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='undefined'),
                    '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]
                })
            else:
                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='undefined'),
                    'related_relations': relationtypes.filter(relationtype = 'member'),
                    'concept_paths': concept_graph.get_paths(lang=lang)
                })


        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 f == 'skos':
            include_parentconcepts = False
            include_subconcepts = True
            depth_limit = None
            skos = SKOSWriter()
            return HttpResponse(skos.write(concept_graph, format="pretty-xml"), content_type="application/xml")

        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), conceptid_id = conceptid, valuetype_id = 'image',languageid_id = settings.LANGUAGE_CODE)
                value.save()
                return JSONResponse(value)

            elif skosfile:
                skos = SKOSReader()
                rdf = skos.read_file(skosfile)
                ret = skos.save_concepts_from_skos(rdf)
                return JSONResponse(ret)

        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):
                    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})

                return JSONResponse(concept)

    return HttpResponseNotFound
Exemplo n.º 20
0
    def test_prefLabel(self):
        """
        Test to confirm the proper retrieval of the prefLabel based on different language requirements

        """

        concept = Concept()
        concept.nodetype = 'Concept'
        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en-US',
                'language': 'en-US'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en',
                'language': 'en'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es-SP',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        self.assertEqual(concept.get_preflabel(lang='en-US').value, 'test pref label en-US')
        self.assertEqual(concept.get_preflabel(lang='en').value, 'test pref label en')
        self.assertEqual(concept.get_preflabel().value, 'test pref label %s' % (test_settings.LANGUAGE_CODE))

        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en',
                'language': 'en'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        # should pick the base language if it can't find the more specific version
        self.assertEqual(concept.get_preflabel(lang='en-US').value, 'test pref label en')
        
        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        self.assertEqual(concept.get_preflabel(lang='en-US').value, 'test alt label en-US')
                
        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en',
                'language': 'en'
            })
        ]

        self.assertEqual(concept.get_preflabel(lang='en-US').value, 'test alt label en')
        
        concept.values = [
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label en-US',
                'language': 'en-US'
            }),
            ConceptValue({
                'type': 'prefLabel',
                'category': 'label',
                'value': 'test pref label es',
                'language': 'es-SP'
            }),
            ConceptValue({
                'type': 'altLabel',
                'category': 'label',
                'value': 'test alt label en-US',
                'language': 'en-US'
            })
        ]

        self.assertEqual(concept.get_preflabel(lang='en').value, 'test pref label en-US')
Exemplo n.º 21
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