def export(request, conceptid): concept_graphs = [Concept().get(id=conceptid, include_subconcepts=True, include_parentconcepts=False, include_relatedconcepts=True, depth_limit=None, up_depth_limit=None)] skos = SKOSWriter() return HttpResponse(skos.write(concept_graphs, format="pretty-xml"), content_type="application/xml")
def export_collections(request): concept_graphs = [] for concept in models.Concept.objects.filter(nodetype_id='Collection'): concept_graphs.append(Concept().get(id=concept.pk, include_subconcepts=True, include_parentconcepts=False, include_relatedconcepts=False, depth_limit=None, up_depth_limit=None, semantic=False)) skos = SKOSWriter() return HttpResponse(skos.write(concept_graphs, format="pretty-xml"), content_type="application/xml")
def get(self, request, conceptid=None): if user_can_read_concepts(user=request.user): allowed_formats = ['json', 'json-ld'] format = request.GET.get('format', 'json-ld') if format not in allowed_formats: return JSONResponse(status=406, reason='incorrect format specified, only %s formats allowed' % allowed_formats) include_subconcepts = request.GET.get('includesubconcepts', 'true') == 'true' include_parentconcepts = request.GET.get('includeparentconcepts', 'true') == 'true' include_relatedconcepts = request.GET.get('includerelatedconcepts', 'true') == 'true' depth_limit = request.GET.get('depthlimit', None) lang = request.GET.get('lang', settings.LANGUAGE_CODE) try: indent = int(request.GET.get('indent', None)) except Exception: indent = None if conceptid: try: 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) ret.append(concept_graph) except models.Concept.DoesNotExist: return JSONResponse(status=404) except Exception as e: return JSONResponse(status=500, reason=e) else: return JSONResponse(status=500) else: return JSONResponse(status=500) if format == 'json-ld': try: skos = SKOSWriter() value = skos.write(ret, format="nt") js = from_rdf(str(value), options={format: 'application/nquads'}) context = [{ "@context": { "skos": SKOS, "dcterms": DCTERMS, "rdf": str(RDF) } }, { "@context": settings.RDM_JSONLD_CONTEXT }] ret = compact(js, context) except Exception as e: return JSONResponse(status=500, reason=e) return JSONResponse(ret, indent=indent)
def export(request, conceptid): concept_graphs = [ Concept().get(id=conceptid, include_subconcepts=True, include_parentconcepts=False, include_relatedconcepts=True, depth_limit=None, up_depth_limit=None) ] skos = SKOSWriter() response = HttpResponse(skos.write(concept_graphs, format="pretty-xml"), content_type="application/xml") response['Content-Disposition'] = 'attachment; filename="%s.xml"' % ( conceptid) return response
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
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
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