Skip to content

Instantly share code, notes, and snippets.

@cjauvin
Created December 11, 2012 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjauvin/4262845 to your computer and use it in GitHub Desktop.
Save cjauvin/4262845 to your computer and use it in GitHub Desktop.
import psycopg2, psycopg2.extras
import little_pger as db
from flask import *
application = Flask('autocomplete-tribute')
@application.route('/autocomplete', methods=['GET'])
def autocomplete():
conn = psycopg2.connect("dbname=autocomplete-tribute user=christian",
connection_factory=psycopg2.extras.RealDictConnection)
cursor = conn.cursor()
where = {}
if request.args['query']:
query_tokens = request.args['query'].split()
fields = ['adjective', 'animal', 'version']
# MUST be a set in this context!
where[('||'.join(fields), 'ilike')] = {'%%%s%%' % v for v in query_tokens}
# else: we want everything!
return jsonify(success=True,
data=db.select(cursor, 'ubuntu', where=where, order_by='id'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment