def login(): db, cursor = db_op.db_connect() if db is None or cursor is None: return json.dumps({"success": False, "msg": "資料庫錯誤"}) uid, help = select_user_with_account(request.values['username'], request.values['password'], db, cursor) if uid is None or help is None: db_op.db_close(db) return json.dumps({"success": False, "msg": "帳號或密碼錯誤"}) view_type, view_status, view_range = select_user_setting( str(uid), db, cursor) if view_type is None or view_status is None or view_range is None: db_op.db_close(db) return json.dumps({"success": False, "msg": "無法載入使用者偏好設定"}) token = str(uuid.uuid1()) update_token(str(uid), token, db, cursor) db_op.db_close(db) return json.dumps({ "success": True, "msg": { "uid": uid, "help": help, "token": token, "view_type": view_type, "view_status": view_status, "view_range": view_range } })
def logout(): db, cursor = db_op.db_connect() if db is None or cursor is None: return json.dumps({"success": False, "msg": "資料庫錯誤"}) if auth_user(str(request.values['uid']), request.values['token'], db, cursor): clear_token(str(request.values['uid']), db, cursor) db_op.db_close(db) return json.dumps({"success": True, "msg": ""})
def report(): db, cursor = db_op.db_connect() if db is None or cursor is None: return json.dumps({"success": False, "msg": "資料庫錯誤"}) if not auth_user(str(request.values['uid']), request.values['token'], db, cursor): db_op.db_close(db) return json.dumps({"success": False, "msg": "無法確認使用者身份"}) report_data(json.loads(request.values['data']), str(request.values['uid']), db, cursor) db_op.db_close(db) return json.dumps({"success": True, "msg": "飲水機資料已加入,感謝您幫助完善臺北找水喝"})
def register(): db, cursor = db_op.db_connect() if db is None or cursor is None: return json.dumps({"success": False, "msg": "資料庫錯誤"}) if not check_account(request.values['username'], db, cursor): db_op.db_close(db) return json.dumps({"success": False, "msg": "使用者已存在"}) insert_user(request.values['username'], request.values['password'], request.values['helper'], db, cursor) db_op.db_close(db) return json.dumps({"success": True, "msg": "註冊成功,請進行登入"})
def setting(): db, cursor = db_op.db_connect() if db is None or cursor is None: return json.dumps({"success": False, "msg": "資料庫錯誤"}) if not auth_user(str(request.values['uid']), request.values['token'], db, cursor): db_op.db_close(db) return json.dumps({"success": False, "msg": "無法更新使用者偏好設定"}) update_setting(str(request.values['uid']), request.values['view_type'], request.values['view_status'], str(request.values['view_range']), db, cursor) db_op.db_close(db) return json.dumps({"success": True, "msg": "使用者偏好設定已更新"})
def data(): db, cursor = db_op.db_connect() if db is None or cursor is None: return json.dumps({"success": False, "msg": "資料庫錯誤"}) weekday = (datetime.datetime.today().weekday() + 1) % 7 time = str(datetime.datetime.today().hour).zfill(2) + str(datetime.datetime.today().minute).zfill(2) open_hour = get_open_hour(str(weekday), db, cursor) available_open_hour = [] for key in open_hour.keys(): tmp = False for pair in open_hour[key]: if pair[0] <= time <= pair[1]: tmp = True if tmp: available_open_hour.append(key) result = get_data(available_open_hour, open_hour, db, cursor) return json.dumps({"success": True, "msg": result})
def confirm(): db, cursor = db_op.db_connect() if db is None or cursor is None: return json.dumps({"success": False, "msg": "資料庫錯誤"}) if not auth_user(str(request.values['uid']), request.values['token'], db, cursor): db_op.db_close(db) return json.dumps({"success": False, "msg": "無法確認使用者身份"}) status, uid = get_confirm_data(request.values['index'], db, cursor) if status is None or uid is None: db_op.db_close(db) return json.dumps({"success": False, "msg": "無指定飲水機資料"}) elif str(uid) == str(request.values['uid']): db_op.db_close(db) return json.dumps({"success": False, "msg": "無法球員兼裁判"}) elif status != 0: db_op.db_close(db) return json.dumps({"success": False, "msg": "指定飲水機非待確認"}) elif not (request.values['available'] == '1' or request.values['available'] == '-1'): db_op.db_close(db) return json.dumps({"success": False, "msg": "需指定為可使用/不可使用"}) else: confirm_data(request.values['index'], str(request.values['available']), db, cursor) db_op.db_close(db) return json.dumps({"success": True, "msg": "飲水機狀態已更新,感謝您幫助完善臺北找水喝"})