Beispiel #1
0
def active_properties(request, dt_name):
    dt_name = urllib.parse.unquote(dt_name)

    # get class
    q = request.GET.get('q', None)
    if not q:
        raise Http404
    q = urllib.parse.unquote(q)

    # try to locate in the repository first
    p_from_db = VocabularyProperty.objects.filter(uri=q)
    if p_from_db:
        return render(request, "builder_advanced/docs/property.html", {"property": p_from_db[0]})

    # get endpoint
    endpoint = get_endpoint_from_dt_name(dt_name)
    if not endpoint:
        raise Http404

    query = 'SELECT ?domain ?range WHERE { ?a <' + q + '> ?b . ?a a ?domain . optional{?b a ?range .} } limit 1'

    # get json result
    result = sparql_query_json(endpoint, query)

    # check for errors
    if result.status_code >= 400:
        return HttpResponse(result.content, status=result.status_code)

    # create the class object
    res = json.loads(result.content.decode('utf-8'))

    if len(res['results']['bindings']) == 0:
        return HttpResponse('<p>Docs not found.</p>')

    b = res['results']['bindings'][0]
    if 'range' in b:
        p_range = b['range']['value']
    else:
        p_range = 'http://www.w3.org/2000/01/rdf-schema#Literal'
    property_object = {"uri": q, "domain": b['domain']['value'], "range": p_range}

    # return class template
    return render(request, "builder_advanced/docs/property.html", {"property": property_object, "from_json": True})
Beispiel #2
0
def active_classes(request, dt_name):
    dt_name = urllib.parse.unquote(dt_name)

    # get class
    q = request.GET.get('q', None)
    if not q:
        raise Http404
    q = urllib.parse.unquote(q)

    # try to locate in the repository first
    c_from_db = VocabularyClass.objects.filter(uri=q)
    if c_from_db:
        return render(request, "builder_advanced/docs/class.html", {"class": c_from_db[0]})

    # get endpoint
    endpoint = get_endpoint_from_dt_name(dt_name)
    if not endpoint:
        raise Http404

    property_limit = 10
    query = 'SELECT DISTINCT ?property WHERE { ?x a <' + q + '>. ?x ?property ?y .} limit ' + str(property_limit)

    # get json result
    result = sparql_query_json(endpoint, query)

    # check for errors
    if result.status_code >= 400:
        return HttpResponse(result.content, status=result.status_code)

    # create the class object
    res = json.loads(result.content.decode('utf-8'))

    properties = []
    bindings = res['results']['bindings']
    for b in bindings:
        properties.append(b['property']['value'])
    class_object = {"uri": q, "properties": properties}

    # return class template
    options = {"class": class_object, "from_json": True, "property_limit": property_limit}
    return render(request, "builder_advanced/docs/class.html", options)