query.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import logging
  2. from flask import (
  3. Blueprint, current_app, jsonify, request, make_response,
  4. render_template, send_file)
  5. from rdflib import URIRef
  6. from rdflib.plugin import PluginException
  7. from lakesuperior import env
  8. from lakesuperior.api import query as query_api
  9. from lakesuperior.dictionaries.namespaces import ns_collection as nsc
  10. from lakesuperior.dictionaries.namespaces import ns_mgr as nsm
  11. # Query endpoint. raw SPARQL queries exposing the underlying layout can be made
  12. # available. Also convenience methods that allow simple lookups based on simple
  13. # binary comparisons should be added. Binary lookups—maybe?
  14. # N.B All data sources are read-only for this endpoint.
  15. logger = logging.getLogger(__name__)
  16. rdfly = env.app_globals.rdfly
  17. query = Blueprint('query', __name__)
  18. @query.route('/term_search', methods=['GET', 'POST'])
  19. def term_search():
  20. """
  21. Search by entering a search term and optional property and comparison term.
  22. """
  23. operands = (
  24. ('_id', 'Matches Term'),
  25. ('=', 'Is Equal To'),
  26. ('!=', 'Is Not Equal To'),
  27. ('<', 'Is Less Than'),
  28. ('>', 'Is Greater Than'),
  29. ('<=', 'Is Less Than Or Equal To'),
  30. ('>=', 'Is Greater Than Or Equal To'),
  31. )
  32. qres = term_list = []
  33. if request.method == 'POST':
  34. terms = request.json.get('terms', {})
  35. or_logic = request.json.get('logic', 'and') == 'or'
  36. logger.info('Form: {}'.format(request.json))
  37. logger.info('Terms: {}'.format(terms))
  38. logger.info('Logic: {}'.format(or_logic))
  39. qres = query_api.term_query(terms, or_logic)
  40. rsp = [
  41. uri.replace(nsc['fcres'], request.host_url.rstrip('/') + '/ldp')
  42. for uri in qres]
  43. return jsonify(rsp), 200
  44. else:
  45. return render_template(
  46. 'term_search.html', operands=operands, qres=qres, nsm=nsm)
  47. @query.route('/sparql', methods=['GET', 'POST'])
  48. def sparql():
  49. """
  50. Perform a direct SPARQL query on the underlying triplestore.
  51. :param str qry: SPARQL query string.
  52. """
  53. accept_mimetypes = {
  54. 'text/csv': 'csv',
  55. 'application/sparql-results+json': 'json',
  56. 'application/sparql-results+xml': 'xml',
  57. }
  58. if request.method == 'GET':
  59. return render_template('sparql_query.html', nsm=nsm)
  60. else:
  61. if request.mimetype == 'application/sparql-query':
  62. qstr = request.stream.read()
  63. else:
  64. qstr = request.form['query']
  65. logger.debug('Query: {}'.format(qstr))
  66. match = request.accept_mimetypes.best_match(accept_mimetypes.keys())
  67. fmt = (
  68. accept_mimetypes[match] if match
  69. else request.accept_mimetypes.best)
  70. try:
  71. out_stream = query_api.sparql_query(qstr, fmt)
  72. except PluginException:
  73. return (
  74. 'Unable to serialize results into format {}'.format(fmt), 406)
  75. return send_file(out_stream, mimetype=fmt), 200