diff --git a/src/dinner.py b/src/dinner.py index 170df98..1544ab8 100644 --- a/src/dinner.py +++ b/src/dinner.py @@ -1,5 +1,6 @@ import json from re import Pattern +from itertools import chain from session import app from flask import render_template, make_response, abort @@ -32,16 +33,13 @@ def fetch_user_menu_summary() -> dict[str, float]: all_menu = list(fetch_all_user_today_menu()) if len(all_menu) > 0: menus = list(map(lambda x: json.loads(x[1]), all_menu)) - menu_keys = set() - for x in menus: - for k in x: - menu_keys.add(k) + menu_keys = set(chain.from_iterable(menus)) result = {} for k in menu_keys: result[k] = 0 - for x in menus: - for k in x: - result[k] += x[k] + for user_menu in menus: + for k in user_menu: + result[k] += user_menu[k] return result return {} @@ -106,20 +104,18 @@ def dinner_update(): abort(403) if check_roll() != 0: return make_response(json.dumps(dict(code=-1, data="来晚了,提交失败"))) - value = request.args.get('value').strip() - if not value: + user_menu = request.args.get('value').strip() + if not user_menu: set_user_menu('') return make_response(json.dumps(dict(code=0, data="OK"))) - value = json.loads(value) - summary = 0 - for key in value: - summary += int(value[key]) + user_menu = json.loads(user_menu) + summary = sum(abs(int(value)) for value in user_menu.values()) if summary <= 0: set_user_menu('') return make_response(json.dumps(dict(code=0, data="OK"))) - for key in value: - value[key] = int(value[key]) / summary - set_user_menu(json.dumps(value, ensure_ascii=False)) + for key in user_menu: + user_menu[key] = abs(int(user_menu[key])) / summary + set_user_menu(json.dumps(user_menu, ensure_ascii=False)) return make_response(json.dumps(dict(code=0, data="OK"))) diff --git a/src/start.py b/src/start.py index cf3f3d6..7f335ae 100644 --- a/src/start.py +++ b/src/start.py @@ -1,11 +1,9 @@ import os from flask import redirect, send_from_directory -import threading +# noinspection PyUnresolvedReferences from dinner import * from session import app -lock = threading.Lock() - @app.route('/') def index():