Commit 36202b83c858002ca675bb2b997af3d933e33bf9
Language and location auto-completion implemented.
| | | | 11 | from urllib import quote_plus | 11 | from urllib import quote_plus |
---|
12 | from urllib import unquote_plus | 12 | from urllib import unquote_plus |
---|
13 | import conf | 13 | import conf |
---|
| | 14 | import oursql |
---|
| | 15 | from flask import jsonify |
---|
14 | app = Flask(__name__) | 16 | app = Flask(__name__) |
---|
15 | @app.before_request | 17 | @app.before_request |
---|
16 | def first(): | 18 | def first(): |
---|
… | | … | |
---|
346 | query.reverse() | 346 | query.reverse() |
---|
347 | return render_template('directory.html', name=query, mymodule = quote_plus, myset=set, mylist= list) | 347 | return render_template('directory.html', name=query, mymodule = quote_plus, myset=set, mylist= list) |
---|
348 | | 348 | |
---|
349 | @app.route('/getLang') | | @app.route('/getLang') |
---|
| | 349 | @app.route('/getLoc', methods=['GET']) | | | 350 | def get_loc(): |
---|
| | 351 | term = request.args['term'] |
---|
| | 352 | connection = oursql.Connection(conf.DBHOST[0],conf.DBUSRNAME[0],conf.DBPASSWD[0],db=conf.DBNAME[0]) |
---|
| | 353 | cursor = connection.cursor(oursql.DictCursor) |
---|
| | 354 | cursor.execute('select l.name, c.country_name from `location` as l, `codes` as c where l.name like ? and l.code=c.code limit ?', (term+'%', 5)) |
---|
| | 355 | r = cursor.fetchall() |
---|
| | 356 | connection.close() |
---|
| | 357 | d = {} |
---|
| | 358 | d['return'] = r |
---|
| | 359 | return jsonify(d) |
---|
| | 360 | @app.route('/getLang', methods=['GET']) |
---|
350 | def get_lang(): | 361 | def get_lang(): |
---|
351 | collection = g.db['alipi_lang'] | | collection = g.db['alipi_lang'] |
---|
352 | term = '^{0}.*'.format(request.args['term'][0]) | | term = '^{0}.*'.format(request.args['term'][0]) |
---|
353 | query = collection.group( | | query = collection.group( |
---|
354 | key = Code('function(doc){return {"name" : doc.name}}'), | | key = Code('function(doc){return {"name" : doc.name}}'), |
---|
355 | condition={"name":{'$regex':term, '$options':'i'}}, | | condition={"name":{'$regex':term, '$options':'i'}}, |
---|
356 | initial={'na': []}, | | initial={'na': []}, |
---|
357 | reduce=Code('function(doc,out){out.na.push(doc);}') | | reduce=Code('function(doc,out){out.na.push(doc);}') |
---|
358 | ) | | ) |
---|
359 | string = {'name':[]} | | string = {'name':[]} |
---|
360 | if len(query) != 0: | | if len(query) != 0: |
---|
361 | for i in query: | | for i in query: |
---|
362 | for x in i['na']: | | for x in i['na']: |
---|
363 | if x != '_id': | | if x != '_id': |
---|
364 | string['name'].append((x['name'])) | | string['name'].append((x['name'])) |
---|
365 | return jsonify(string) | | return jsonify(string) |
---|
366 | | | |
---|
| | 362 | term = request.args['term'] | | | 363 | connection = oursql.Connection(conf.DBHOST[0],conf.DBUSRNAME[0],conf.DBPASSWD[0],db=conf.DBNAME[0]) |
---|
| | 364 | cursor = connection.cursor(oursql.DictCursor) |
---|
| | 365 | cursor.execute('select * from `languages` as l where l.name like ? limit ?', (term+'%',5)) |
---|
| | 366 | r = cursor.fetchall() |
---|
| | 367 | connection.close() |
---|
| | 368 | d = {} |
---|
| | 369 | d['return'] = r |
---|
| | 370 | return jsonify(d) |
---|
367 | import logging,os | 371 | import logging,os |
---|
368 | from logging import FileHandler | 372 | from logging import FileHandler |
---|
369 | | 373 | |
---|