修复BUG

This commit is contained in:
ZaneYork 2024-09-14 08:29:16 +08:00
parent 5c3d0c1d3f
commit 6aa4fa2c50
2 changed files with 8 additions and 6 deletions

View File

@ -104,20 +104,22 @@ def dinner_update():
if not is_valid_user(nickname): if not is_valid_user(nickname):
return make_response(json.dumps(dict(code=-1, data="你不能投票(*^_^*)"))) return make_response(json.dumps(dict(code=-1, data="你不能投票(*^_^*)")))
if not user_menu: if not user_menu:
set_user_menu({}, nickname) set_user_menu({}, nickname, [])
return make_response(json.dumps(dict(code=0, data="OK"))) return make_response(json.dumps(dict(code=0, data="OK")))
user_menu = json.loads(user_menu) user_menu = json.loads(user_menu)
choice = user_menu['choice'] choice = user_menu['choice']
dislike = user_menu['dislike'] dislike = user_menu.get('dislike')
if dislike is None:
dislike = []
# 计算总投票数值 # 计算总投票数值
summary = sum(abs(int(value)) for value in choice.values()) summary = sum(abs(int(value)) for value in choice.values())
if summary <= 0: if summary <= 0:
set_user_menu({}, nickname) set_user_menu({}, nickname, dislike)
return make_response(json.dumps(dict(code=0, data="OK"))) return make_response(json.dumps(dict(code=0, data="OK")))
# 投票数归一化 # 投票数归一化
for key in choice: for key in choice:
choice[key] = abs(int(choice[key])) / summary choice[key] = abs(int(choice[key])) / summary
set_user_menu(choice, nickname) set_user_menu(choice, nickname, dislike)
return make_response(json.dumps(dict(code=0, data="OK"))) return make_response(json.dumps(dict(code=0, data="OK")))

View File

@ -17,7 +17,7 @@
<div class="mb-3"> <div class="mb-3">
<div class="input-group"> <div class="input-group">
<label class="input-group-text" for="inputName" id="basic-addon1">姓名</label> <label class="input-group-text" for="inputName" id="basic-addon1">姓名</label>
<input type="text" class="form-control" {{ 'disabled="disabled"' if user_menu.nickname else '' | safe }} <input type="text" class="form-control" {{ 'readonly="readonly"' if user_menu.nickname else '' | safe }}
id="inputName" name="nickname" value="{{ user_menu.nickname }}"> id="inputName" name="nickname" value="{{ user_menu.nickname }}">
</div> </div>
</div> </div>
@ -35,7 +35,7 @@
<span class="percentage">{{ '{:.2f}'.format((user_menu.menu.get(choice.name) or 0) * 100) }}</span>% <span class="percentage">{{ '{:.2f}'.format((user_menu.menu.get(choice.name) or 0) * 100) }}</span>%
</label> </label>
<input type="range" class="form-range" <input type="range" class="form-range"
id="range{{ loop.index }}" name="choice.{{ choice.name }}" id="range{{ loop.index }}" name="choice[{{ choice.name }}]"
min="0" max="10" step="1" min="0" max="10" step="1"
value="{{ (user_menu.menu.get(choice.name) or 0) * 10 }}"> value="{{ (user_menu.menu.get(choice.name) or 0) * 10 }}">
</div> </div>