Improved search query

master
Davide Bongiovanni 6 years ago
parent 48f8ef5ddc
commit 8e4002c25d

@ -9,6 +9,9 @@ from flask import Flask
from flask import render_template, send_from_directory, request, Response from flask import render_template, send_from_directory, request, Response
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
import pprint
pp = pprint.PrettyPrinter(indent=4)
app = Flask(__name__) app = Flask(__name__)
db_engine = {} db_engine = {}
@ -49,20 +52,45 @@ def get_part_info(partID):
r.close() r.close()
return json.dumps(l[0]) return json.dumps(l[0])
@app.route('/parts/query/<filter>/<query>') @app.route('/parts/query/<filter_dummy>/<query>')
def query(filter, query): def query(filter_dummy, query):
query = '%' + query + '%' filter = request.args.to_dict()
filter = int(filter) keywords = query.split() # Default splits with spaces
for i in range(len(keywords)):
keywords[i] = '%' + keywords[i] + '%'
kw_dict = {}
for i in range(len(keywords)):
kw_dict["kw" + str(i)] = keywords[i]
s = 'select p.id,partno,description,l.name as location_descriptor from parts as p inner join locations as l on p.location_id = l.id where ' s = 'select p.id,partno,description,l.name as location_descriptor from parts as p inner join locations as l on p.location_id = l.id where '
if filter & (1 << 1) != 0: if filter['l']:
s += 'LOWER(partno) like LOWER(:name) or ' s += '('
if filter & (1 << 4) != 0: for i in range(len(keywords)):
s += 'LOWER(description) like LOWER(:name) or ' s += 'LOWER(l.name) like LOWER(:kw'+ str(i) +') AND '
if filter & (1 << 5) != 0: s = s[:-5]
s += 'LOWER(notes) like LOWER(:name) or ' s += ') OR '
if filter['p']:
s += '('
for i in range(len(keywords)):
s += 'LOWER(partno) like LOWER(:kw'+ str(i) +') AND '
s = s[:-5]
s += ') OR '
if filter['d']:
s += '('
for i in range(len(keywords)):
s += 'LOWER(description) like LOWER(:kw'+ str(i) +') AND '
s = s[:-5]
s += ') OR '
if filter['n']:
s += '('
for i in range(len(keywords)):
s += 'LOWER(notes) like LOWER(:kw'+ str(i) +') AND '
s = s[:-5]
s += ') OR '
s = s[:-4] + ';' s = s[:-4] + ';'
print(s)
s = text(s) s = text(s)
r = db_engine.execute(s, name=query) r = db_engine.execute(s, kw_dict)
l = [] l = []
for row in r: for row in r:
l.append(dict(row)) l.append(dict(row))

Loading…
Cancel
Save