diff --git a/app.py b/app.py index b3da398..0d28680 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,4 @@ -from flask import Flask, request, redirect, jsonify, render_template +from flask import Flask, request, redirect, jsonify, render_template, Response import json from pprint import pprint as pp from pathlib import Path @@ -19,22 +19,73 @@ def index(): set_list.append(info_file) return render_template('frontpage.html',set_list=set_list) -@app.route('/') +@app.route('/', methods=['GET', 'POST']) def sets(tmp): + with open('./static/sets/'+tmp+'/info.json') as info: info_file = json.loads(info.read()) with open('./static/sets/'+tmp+'/inventory.json') as inventory: inventory_file = json.loads(inventory.read()) with open('./info/'+tmp+'.json') as info: json_file = json.loads(info.read()) - return render_template('bootstrap_table.html', tmp=tmp,title=info_file['name'], - info_file=info_file,inventory_file=inventory_file,json_file=json_file) + + if request.method == 'POST': + part_num = request.form.get('brickpartpart_num') + color = request.form.get('brickcolorname') + index = request.form.get('index') + number = request.form.get('numberInput') + is_spare = request.form.get('is_spare') + + # print(part_num) + # print(color) + # print(index) + # print(number) + # print(is_spare) + + if number is not None: + + print(part_num) + print(color) + print(number) + print(is_spare) + + with open('./info/'+tmp+'.json') as info: + json_file = json.loads(info.read()) + print(json_file['count']) + + data = '{"brick" : {"ID":"' + part_num + '","is_spare": "' + is_spare + '","color_name": "' + color + '","amount":"' + number + '"}}' + + if len(json_file['unit'][int(index)]['bricks']['missing']) == 0: + json_file['unit'][int(index)]['bricks']['missing'].append(json.loads(data)) + print(json_file) + elif number == '': + for idx,i in enumerate(json_file['unit'][int(index)]['bricks']['missing']): + if i['brick']['ID'] == part_num and i['brick']['is_spare'] == is_spare and i['brick']['color_name'] == color: + json_file['unit'][int(index)]['bricks']['missing'].pop(idx) + else: + found = False + for idx,i in enumerate(json_file['unit'][int(index)]['bricks']['missing']): + if not found and i['brick']['ID'] == part_num and i['brick']['is_spare'] == is_spare and i['brick']['color_name'] == color: + json_file['unit'][int(index)]['bricks']['missing'][idx]['brick']['amount'] = number + found = True + if not found: + json_file['unit'][int(index)]['bricks']['missing'].append(json.loads(data)) + + + with open('./info/'+tmp+'.json', 'w') as dump_file: + json.dump(json_file,dump_file) + #return Response(status=200) + return ('', 204) + else: + return render_template('bootstrap_table.html', tmp=tmp,title=info_file['name'], + info_file=info_file,inventory_file=inventory_file,json_file=json_file) + @app.route('//saveNumber', methods=['POST']) def save_number(tmp): - part_num = request.form.get('brick.part.part_num') - color = request.form.get('brick.color.name') + part_num = request.form.get('brickpartpart_num') + color = request.form.get('brickcolorname') index = request.form.get('index') number = request.form.get('numberInput') is_spare = request.form.get('is_spare') @@ -48,14 +99,13 @@ def save_number(tmp): with open('./info/'+tmp+'.json') as info: json_file = json.loads(info.read()) - print(json_file['count']) data = '{"brick" : {"ID":"' + part_num + '","is_spare": "' + is_spare + '","color_name": "' + color + '","amount":"' + number + '"}}' if len(json_file['unit'][int(index)]['bricks']['missing']) == 0: json_file['unit'][int(index)]['bricks']['missing'].append(json.loads(data)) print(json_file) - elif number == '0' or number == '': + elif number == '': for idx,i in enumerate(json_file['unit'][int(index)]['bricks']['missing']): if i['brick']['ID'] == part_num and i['brick']['is_spare'] == is_spare and i['brick']['color_name'] == color: json_file['unit'][int(index)]['bricks']['missing'].pop(idx) @@ -72,7 +122,7 @@ def save_number(tmp): with open('./info/'+tmp+'.json', 'w') as dump_file: json.dump(json_file,dump_file) - return redirect('/{}'.format(tmp)) + return Response(status=204) if __name__ == '__main__': app.run(host='192.168.10.109', debug=True, port=3333) diff --git a/templates/base.html b/templates/base.html index 105b82f..410a945 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,16 +1,26 @@ - + + + {{ title }} - - + + + + + + + +