|
@@ -1,12 +1,11 @@
|
|
|
import logging
|
|
|
|
|
|
-from flask import Blueprint, current_app, request, render_template
|
|
|
+from flask import Blueprint, current_app, request, render_template, send_file
|
|
|
from rdflib.plugin import PluginException
|
|
|
|
|
|
from lakesuperior.env import env
|
|
|
from lakesuperior.dictionaries.namespaces import ns_mgr as nsm
|
|
|
from lakesuperior.api import query as query_api
|
|
|
-from lakesuperior.store.ldp_rs.lmdb_store import LmdbStore, TxnManager
|
|
|
|
|
|
|
|
|
|
|
@@ -14,7 +13,6 @@ from lakesuperior.store.ldp_rs.lmdb_store import LmdbStore, TxnManager
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
-rdf_store = env.app_globals.rdf_store
|
|
|
rdfly = env.app_globals.rdfly
|
|
|
|
|
|
query = Blueprint('query', __name__)
|
|
@@ -55,25 +53,21 @@ def sparql():
|
|
|
if request.method == 'GET':
|
|
|
return render_template('sparql_query.html', nsm=nsm)
|
|
|
else:
|
|
|
- if request.mimetype == 'multipart/form-data':
|
|
|
- qstr = request.form['query']
|
|
|
+ if request.mimetype == 'application/sparql-query':
|
|
|
+ qstr = request.stream.read()
|
|
|
else:
|
|
|
- qstr = stream.read()
|
|
|
+ qstr = request.form['query']
|
|
|
logger.debug('Query: {}'.format(qstr))
|
|
|
- with TxnManager(rdf_store) as txn:
|
|
|
- qres = query_api.sparql_query(qstr)
|
|
|
|
|
|
- match = request.accept_mimetypes.best_match(accept_mimetypes.keys())
|
|
|
- if match:
|
|
|
- enc = accept_mimetypes[match]
|
|
|
- else:
|
|
|
- enc = request.accept_mimetypes.best
|
|
|
+ match = request.accept_mimetypes.best_match(accept_mimetypes.keys())
|
|
|
+ fmt = (
|
|
|
+ accept_mimetypes[match] if match
|
|
|
+ else request.accept_mimetypes.best)
|
|
|
|
|
|
- try:
|
|
|
- out = qres.serialize(format=enc)
|
|
|
- except PluginException:
|
|
|
- return (
|
|
|
- 'Unable to serialize results into format {}'.format(enc),
|
|
|
- 406)
|
|
|
+ try:
|
|
|
+ out_stream = query_api.sparql_query(qstr, fmt)
|
|
|
+ except PluginException:
|
|
|
+ return (
|
|
|
+ 'Unable to serialize results into format {}'.format(fmt), 406)
|
|
|
|
|
|
- return out, 200
|
|
|
+ return send_file(out_stream, mimetype=fmt), 200
|