Created
December 11, 2012 22:19
-
-
Save cjauvin/4262845 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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