diff --git a/parts/server.py b/parts/server.py index a7937f8..ac33284 100644 --- a/parts/server.py +++ b/parts/server.py @@ -9,6 +9,9 @@ from flask import Flask from flask import render_template, send_from_directory, request, Response from werkzeug.utils import secure_filename +import pprint +pp = pprint.PrettyPrinter(indent=4) + app = Flask(__name__) db_engine = {} @@ -49,20 +52,45 @@ def get_part_info(partID): r.close() return json.dumps(l[0]) -@app.route('/parts/query//') -def query(filter, query): - query = '%' + query + '%' - filter = int(filter) +@app.route('/parts/query//') +def query(filter_dummy, query): + filter = request.args.to_dict() + 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 ' - if filter & (1 << 1) != 0: - s += 'LOWER(partno) like LOWER(:name) or ' - if filter & (1 << 4) != 0: - s += 'LOWER(description) like LOWER(:name) or ' - if filter & (1 << 5) != 0: - s += 'LOWER(notes) like LOWER(:name) or ' + if filter['l']: + s += '(' + for i in range(len(keywords)): + s += 'LOWER(l.name) like LOWER(:kw'+ str(i) +') AND ' + s = s[:-5] + 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] + ';' + print(s) s = text(s) - r = db_engine.execute(s, name=query) + r = db_engine.execute(s, kw_dict) l = [] for row in r: l.append(dict(row))