diff --git a/src/app.py b/src/app.py index ab229d1..a9a20f0 100644 --- a/src/app.py +++ b/src/app.py @@ -36,17 +36,31 @@ def update_hist(): fill_db(settings["db"], check_items(settings["item"])) return 'Hello, World!' -@app.route('/add',methods = ['POST']) +@app.route('/add', methods=['POST']) def add_item(): print("adding item") - itemid = request.form['itemid'] - if len(request.form['attributes'])>0: - attributes = request.form['attributes'].split(',') - else: - attributes = [] + + data = request.get_json() + + itemid = data.get('itemid') + attributes = data.get('attributes', '').split(',') if data.get('attributes') else [] + new_item = [(itemid, attributes)] extr = check_items(new_item) - return str(extr) + + if len(extr) > 0: + settings = get_conf() + skuid = list(extr.values())[0]["skuid"] + if check_exist(settings["db"], itemid, skuid): + # item already exists + return jsonify({'status': 3}), 400 + else: + # item is valid + fill_db(settings["db"], extr) + return jsonify({'status': 0}), 200 + else: + # item not valid or can't be parsed + return jsonify({'status': 1}), 400 @app.route('/datahistory',methods = ['POST', 'GET']) def data_history_request(): diff --git a/src/db.py b/src/db.py index 61bc0c8..f24e345 100644 --- a/src/db.py +++ b/src/db.py @@ -31,14 +31,7 @@ def add_history_entry(db_settings, itemid, skuid, choice, attributes, image, pri connection = connect_db(db_settings) cursor = connection.cursor() - cursor.execute(""" - SELECT uuid - FROM item - WHERE itemid = %s - AND skuid = %s - """, (itemid, skuid)) - - if cursor.rowcount == 0: + if not check_exist(db_settings, itemid, skuid): add_item(db_settings, itemid, skuid, choice, attributes, image) cursor.execute(""" SELECT uuid @@ -82,6 +75,27 @@ def get_item(db_settings): connection.close() return results +def check_exist(db_settings, itemid, skuid): + '''check if an item is already in the database''' + connection = connect_db(db_settings) + cursor = connection.cursor() + + cursor.execute(""" + SELECT uuid + FROM item + WHERE itemid = %s + AND skuid = %s + """, (itemid, skuid)) + + result = cursor.rowcount + cursor.close() + connection.close() + + if result == 0: + return False + else: + return True + def export_csv(db_settings): '''join item and history data from database and export it in ./output.csv''' connection = connect_db(db_settings) diff --git a/web/app.html b/web/app.html index 3cffd63..49dd6a6 100644 --- a/web/app.html +++ b/web/app.html @@ -1,16 +1,25 @@ - + +
+ + + +