forked from LinDA-tools/LindaWorkbench
/
views.py
371 lines (270 loc) · 12.3 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
from datetime import datetime
import json
import urllib
from django.http import Http404, HttpResponse, JsonResponse
from django.shortcuts import render, get_object_or_404
from django.utils.http import urlquote
import requests
from linda_app.models import DatasourceDescription, VocabularyProperty, Query, get_configuration
from linda_app.settings import LINDA_HOME
def designer_defaults():
params = {
'datasources': list(DatasourceDescription.objects.all()),
'RDF2ANY_SERVER': get_configuration().rdf2any_server
}
params['datasources'].insert(0,
DatasourceDescription(title="All private data dources", name="all", is_public=False
, uri=LINDA_HOME + "sparql/all/", createdOn=datetime.today(),
updatedOn=datetime.today()))
return params
# Home page
def index(request):
params = designer_defaults()
endpoint = request.GET.get('endpoint')
dt_id = request.GET.get('dt_id')
if endpoint:
params['datasource_default'] = endpoint
elif dt_id:
params['datasource_default'] = DatasourceDescription.objects.get(name=request.GET.get('dt_id'))
if not params['datasource_default']:
return Http404
return render(request, "builder_advanced/index.html", params)
# Load an existing design
def load_design(request, pk):
params = designer_defaults()
params['query'] = Query.objects.get(pk=pk)
if not params['query']:
raise Http404
return render(request, "builder_advanced/index.html", params)
# API calls
# get endpoint by data source name
def get_endpoint_from_dt_name(dt_name):
'''
if dt_name != "all": # search in all private data source
datasources = DatasourceDescription.objects.filter(name=dt_name)
if not datasources: # data source not found by name
raise Http404
return datasources[0].get_endpoint()
else:
return get_configuration().private_sparql_endpoint
'''
return dt_name
# Execute a SparQL query on an endpoint and return json response
def sparql_query_json(endpoint, query, timeout=None, append_slash=False, http_response=True):
# encode the query
query_enc = urlquote(query, safe='')
# ClioPatria bugfix
if append_slash and endpoint[-1] != '/':
endpoint += '/'
# get query results and turn them into json
# with &output=json we support non-standard endpoints like IMDB & World Factbook
response = requests.get(
endpoint + "?Accept=" + urlquote(
"application/sparql-results+json") + "&query=" + query_enc + "&format=json&output=json", timeout=timeout)
# ClioPatria bugfix
if not append_slash:
try:
j_obj = json.loads(response.text)
except:
return sparql_query_json(endpoint, query, timeout, append_slash=True)
if response.status_code != 200:
return HttpResponse(response.text, status=response.status_code)
# return the response
if http_response:
return HttpResponse(response.text, "application/json")
else:
return j_obj
# Get active classes in a data source
def active_classes(request, dt_name):
# get the endpoint of the query
endpoint = get_endpoint_from_dt_name(dt_name)
# editor classes
if request.GET.get('q'):
q = request.GET.get('q')
if request.GET.get('prefix'):
regex = request.GET.get('prefix') + '(.)*' + q + '(.)*'
else:
regex = '^http://(/)*(.)*' + q + '(.)*'
query = 'select distinct ?Concept where {[] a ?Concept. FILTER regex(str(?Concept), "' + regex + '" , "i")} LIMIT 20'
else:
# get page
p = request.GET.get('p', '1')
# check if searching distinct
if request.GET.get('distinct'):
distinct = "DISTINCT"
else:
distinct = ""
# query to get all classes with at least one instance
classes_query_paginate_by = 10000
query = "SELECT " + distinct + " ?Concept WHERE { ?s a ?Concept } LIMIT " + str(classes_query_paginate_by) + " OFFSET " + str(
(int(p) - 1) * classes_query_paginate_by)
return sparql_query_json(endpoint, query)
# Get active classes in a data source
def active_root_classes(request, dt_name):
# get the endpoint of the query
endpoint = get_endpoint_from_dt_name(dt_name)
# query to get all classes with at least one instance
query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\nSELECT DISTINCT ?class ((count(?x)) AS ?cnt) WHERE {?x a ?class. FILTER NOT EXISTS {?class rdfs:subClassOf ?parentClass.} } GROUP BY ?class ORDER BY DESC (?cnt)"
return sparql_query_json(endpoint, query)
# Get active subclasses in a data source
def active_subclasses(request, dt_name):
# get parent class
if not request.GET.get('parent_class'):
raise Http404
parent_class = urllib.parse.unquote(request.GET.get('parent_class'))
# get the endpoint of the query
endpoint = get_endpoint_from_dt_name(dt_name)
# query to get all classes with at least one instance
query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\nSELECT DISTINCT ?class (count(?x) AS ?cnt) WHERE {?x a ?class. ?class rdfs:subClassOf <" + parent_class + ">. } GROUP BY ?class ORDER BY DESC (?cnt)"
return sparql_query_json(endpoint, query)
# Get active object properties in a data source
def object_properties(request, dt_name):
# get the endpoint of the query
endpoint = get_endpoint_from_dt_name(dt_name)
# query to get all classes with at least one instance
query = "PREFIX owl: <http://www.w3.org/2002/07/owl#>\nPREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\nSELECT DISTINCT ?property ?domain ?range WHERE {?property a owl:ObjectProperty. ?property rdfs:domain ?domain. ?property rdfs:range ?range}"
return sparql_query_json(endpoint, query)
# Get all properties of class instances in a data source
def active_class_properties(request, dt_name):
# get class searched
if not request.GET.get('class_uri'):
raise Http404
class_uri = urllib.parse.unquote(request.GET.get('class_uri'))
# get the endpoint of the query
endpoint = get_endpoint_from_dt_name(dt_name)
# get if pagination was set
if request.GET.get('page'):
p = int(request.GET['page'])
offset = (p - 1) * 200
page_str = " OFFSET " + str(offset) + " LIMIT 200"
else:
page_str = ""
# query to get all properties of a class with at least one instance
if request.GET.get('order') == "true":
query = "SELECT DISTINCT ?property (count(?x) AS ?cnt) WHERE {?x a <" + class_uri + ">. ?x ?property ?o } GROUP BY ?property ORDER BY DESC(?cnt)" + page_str
else:
query = "SELECT ?property WHERE {?x a <" + class_uri + ">. ?x ?property ?o }" + page_str
return sparql_query_json(endpoint, query)
# Get all properties in a data source
def active_properties(request, dt_name):
# get the query string
q = request.GET.get('q', '')
# get the endpoint of the query
endpoint = get_endpoint_from_dt_name(dt_name)
if request.GET.get('prefix'):
regex = request.GET.get('prefix') + '(.)*' + q + '(.)*'
else:
regex = '^http://(/)*(.)*' + q + '(.)*'
query = 'SELECT DISTINCT ?property WHERE {?x ?property ?o FILTER regex(str(?property), "' + regex + '" , "i")} LIMIT 20'
return sparql_query_json(endpoint, query)
def uri_to_label(uri):
label = uri.split('/')[-1].split('#')[-1].replace('_', ' ')
return urllib.parse.unquote(label)
# Suggest entities of a type
# e.g search for countries in World FactBook typing "fra"
# will return <http://wifo5-04.informatik.uni-mannheim.de/factbook/resource/France>
def get_entity_suggestions(request, dt_name):
# get query
q = request.GET.get('term', '')
q = q.replace(' ', '_')
# get instance & property type
class_uri = request.GET.get('class_uri')
property_uri = request.GET.get('property_uri')
# get the endpoint of the query
endpoint = get_endpoint_from_dt_name(dt_name)
regex = '^http://(/)*(.)*' + q + '(.)*'
if property_uri:
if q:
query = 'SELECT DISTINCT ?instance WHERE {?x a <' + class_uri + \
'>. ?x <' + property_uri + '> ?instance FILTER regex(str(?instance), "' + regex + '" , "i")} LIMIT 20'
else:
query = 'SELECT DISTINCT ?instance WHERE {?x a <' + class_uri + '>. ?x <' +\
property_uri + '> ?instance} LIMIT 20'
else:
if q:
query = 'SELECT DISTINCT ?instance WHERE {?instance a <' + class_uri + \
'> FILTER regex(str(?instance), "' + regex + '" , "i")} LIMIT 20'
else:
query = 'SELECT DISTINCT ?instance WHERE {?instance a <' + class_uri + '>} LIMIT 20'
# get json result
result = sparql_query_json(endpoint, query)
# make array of results
results = []
res = json.loads(result.content.decode('utf8'))
for b in res['results']['bindings']:
results.append({"value": b['instance']['value'], "label": uri_to_label(b['instance']['value'])})
return HttpResponse(json.dumps(results), "application/json")
# Get the return type of a property
def get_property_type(request, dt_name):
# get property uri
if not request.GET.get('property_uri'):
raise Http404
property_uri = urllib.parse.unquote(request.GET.get('property_uri'))
# find type and create json response
props = VocabularyProperty.objects.filter(uri=property_uri)
if not props: # could not detect the property in the vocabulary repository
tp = ""
else: # get the return type (range)
tp = props[0].range_uri()
data = json.dumps({'type': tp})
# return the response
return HttpResponse(data, "application/json")
# Get the domain of a property
def get_properties_with_domain(request, dt_name):
# get class uri
if not request.GET.get('class_uri'):
raise Http404
class_uri = urllib.parse.unquote(request.GET.get('class_uri'))
# find properties and create json response
# resembles a SparQL response json to ease the client's job
r = {"results": {
"bindings": []}
}
for p in VocabularyProperty.objects.filter(domain=class_uri):
r["results"]["bindings"].append({"property": {"value": p.uri}})
data = json.dumps(r)
# return the response
return HttpResponse(data, "application/json")
# Get number of class instances
def class_info(request, dt_name):
# get class searched
if not request.GET.get('class_uri'):
raise Http404
class_uri = urllib.parse.unquote(request.GET.get('class_uri'))
# get the endpoint of the query
endpoint = get_endpoint_from_dt_name(dt_name)
# query to get all classes with at least one instance
query = "SELECT (count(?x) AS ?cnt) WHERE {?x a <" + class_uri + ">}"
return sparql_query_json(endpoint, query)
API_QUERY_LIMIT = 100
def auto_paginate(q, page):
"""
If no limit/offset is specified inside the query, adds limit & offset to query based on current page and sets flag
to True.
Otherwise, returns query untouched and sets flag to False.
"""
if 'limit ' not in q.lower() and 'offset ' not in q.lower(): # no pagination parameters
return '%s LIMIT %d OFFSET %d' % (q, API_QUERY_LIMIT, (page - 1)*API_QUERY_LIMIT), True
else:
return q, False
# API call to execute a specific query, auto-paginate & get results
def execute_query_api(request, q_id):
# get query & current page
query = get_object_or_404(Query, pk=q_id)
page = int(request.GET.get('page', '1'))
# auto-paginate
sparql, include_links = auto_paginate(query.sparql, page)
# execute the query
result = sparql_query_json(query.endpoint, sparql, http_response=False)
if type(result) == HttpResponse: # error case
return result
else:
# include pagination links
if include_links:
links = {}
if page > 1:
links['prev'] = '/api/query/%d/execute/?page=%d' % (query.pk, page - 1)
if len(result['results']['bindings']) == API_QUERY_LIMIT:
links['next'] = '/api/query/%d/execute/?page=%d' % (query.pk, page + 1)
result['links'] = links
return HttpResponse(json.dumps(result), content_type='application/json')