Implemented simple query with filter

pull/3/head
assar 8 years ago
parent 3139b527af
commit 2ab689e87c

@ -1,6 +1,7 @@
import json
import sqlalchemy
from sqlalchemy.sql import select
from sqlalchemy.sql import text
from flask import Flask
from flask import render_template
@ -14,9 +15,28 @@ parts = {}
def index():
return render_template('partsearch.html')
@app.route('/query/<query>')
def query(query):
pass
@app.route('/query/<filter>/<query>')
def query(filter, query):
query = '%' + query + '%'
s = 'select * from parts where '
if filter & (1 << 0) != 0:
s += 'type like :name or '
if filter & (1 << 1) != 0:
s += 'partno like :name or '
if filter & (1 << 2) != 0:
s += 'partnoalt like :name or '
if filter & (1 << 3) != 0:
s += 'partnoalt2 like :name or '
if filter & (1 << 4) != 0:
s += 'description like :name or '
s = s[:-4] + ';'
s = text(s)
r = db_engine.execute(s, name=query)
l = []
for row in r:
l.append(dict(row))
r.close()
return json.dumps(l)
def connect(user, password, db, host='localhost', port=5432):
'''Returns a connection and a metadata object'''

Loading…
Cancel
Save