diff options
Diffstat (limited to 'api/user')
-rw-r--r-- | api/user/avatar.py | 4 | ||||
-rw-r--r-- | api/user/games.py | 56 | ||||
-rw-r--r-- | api/user/info.py | 46 | ||||
-rw-r--r-- | api/user/password.py | 2 | ||||
-rw-r--r-- | api/user/preferences.py | 24 | ||||
-rw-r--r-- | api/user/status.py | 4 |
6 files changed, 68 insertions, 68 deletions
diff --git a/api/user/avatar.py b/api/user/avatar.py index 1b5500a..b034c3e 100644 --- a/api/user/avatar.py +++ b/api/user/avatar.py @@ -28,14 +28,14 @@ def get_avatar(): @avatar.route( - '/avatar', methods=["POST"] + '/avatar', methods=["POST"] ) #TODO: pillow image size validation (client side resize) @auth_required("user") def update_avatar(login): if not request.data: return "", 400 open(f"database/avatars/{login}.png", - "wb").write(decode(request.data, "base64")) + "wb").write(decode(request.data, "base64")) return "", 200 diff --git a/api/user/games.py b/api/user/games.py index 83d721a..92799bd 100644 --- a/api/user/games.py +++ b/api/user/games.py @@ -12,26 +12,26 @@ import json # get total game outcome amount for user def sum_games(user_id): #! SANITIZE USER_ID FIRST wld_querys = [ - ' '.join( - [ - "select count(game_id)", - "from games", - "where", - f"player_{x[0]}_id = \"{user_id}\" and", - f"outcome = \"{x[1]}\"", - ] - ) for x in [(1, "w"), (1, "l"), (2, "w"), (2, "l")] + ' '.join( + [ + "select count(game_id)", + "from games", + "where", + f"player_{x[0]}_id = \"{user_id}\" and", + f"outcome = \"{x[1]}\"", + ] + ) for x in [(1, "w"), (1, "l"), (2, "w"), (2, "l")] ] wld_querys.insert( - 0, ' '.join( - [ - "select count(game_id)", - "from games", - "where", - f"(player_1_id = \"{user_id}\" or player_2_id = \"{user_id}\") and", - "outcome = \"d\"", - ] - ) + 0, ' '.join( + [ + "select count(game_id)", + "from games", + "where", + f"(player_1_id = \"{user_id}\" or player_2_id = \"{user_id}\") and", + "outcome = \"d\"", + ] + ) ) big_query = "select " + ", ".join([f"({query})" for query in wld_querys]) @@ -40,18 +40,18 @@ def sum_games(user_id): #! SANITIZE USER_ID FIRST # win and lose are calculated from user_id's perspective (player_1_id, player_2_id in db) return { - "draw": results[0], - "win": results[1] + results[4], - "lose": results[2] + results[3], - "games": reduce(lambda a, b: a + b, results) + "draw": results[0], + "win": results[1] + results[4], + "lose": results[2] + results[3], + "games": reduce(lambda a, b: a + b, results) } # get `count` games that `user_id` participated in, sorted by newest game def fetch_games(user_id, count): game_ids = cursor.execute( - "select game_id from games where player_1_id = ? or player_2_id = ? order by created desc", - [user_id, user_id] + "select game_id from games where player_1_id = ? or player_2_id = ? order by created desc", + [user_id, user_id] ).fetchmany(count) export = [] @@ -73,21 +73,21 @@ def index(): token = request.cookies.get("token") or "" if not user_id and \ - not token: + not token: return "", 400 if token and not user_id: user_id = token_login(token) if not cursor.execute( - "select user_id from users where user_id = ?", [user_id] + "select user_id from users where user_id = ?", [user_id] ).fetchone(): return "", 403 export = {} merge( - export, {"totals": sum_games(user_id)}, - {"games": fetch_games(user_id, 20)} + export, {"totals": sum_games(user_id)}, + {"games": fetch_games(user_id, 20)} ) return export, 200 diff --git a/api/user/info.py b/api/user/info.py index de0d2e8..ee20814 100644 --- a/api/user/info.py +++ b/api/user/info.py @@ -8,7 +8,7 @@ import json # check if user_id exists in database def valid_user_id(user_id): query = cursor.execute( - "select user_id from users where user_id = ?", [user_id] + "select user_id from users where user_id = ?", [user_id] ).fetchone() return bool(query) @@ -16,8 +16,8 @@ def valid_user_id(user_id): # get relation to user_2_id from user_1_id's perspective def get_relation_to(user_1_id, user_2_id): relation = cursor.execute("select * from social where " + \ - "(user_1_id = ? and user_2_id = ?) or " + \ - "(user_1_id = ? and user_2_id = ?)", [user_1_id, user_2_id, user_2_id, user_1_id]).fetchone() + "(user_1_id = ? and user_2_id = ?) or " + \ + "(user_1_id = ? and user_2_id = ?)", [user_1_id, user_2_id, user_2_id, user_1_id]).fetchone() if not relation: return "none" if relation[2] == "friendship": return "friends" if relation[2] == "outgoing" and relation[0] == user_1_id: @@ -31,8 +31,8 @@ def get_relation_to(user_1_id, user_2_id): # get users friend count def count_friends(user_id): query = cursor.execute( - "select type from social where (user_1_id = ? or user_2_id = ?) and type = \"friendship\"", - [user_id, user_id] + "select type from social where (user_1_id = ? or user_2_id = ?) and type = \"friendship\"", + [user_id, user_id] ).fetchall() return len(query) #FIXME: use SQL count() instead of python's len() @@ -40,25 +40,25 @@ def count_friends(user_id): # get user/info of `user_id` as `viewer` (id) def format_user(user_id, viewer=''): user = cursor.execute( - "select " + ", ".join( - [ - "username", - "user_id", - "country", - "registered", - "status", - ] - ) + " from users where user_id = ?", [user_id] + "select " + ", ".join( + [ + "username", + "user_id", + "country", + "registered", + "status", + ] + ) + " from users where user_id = ?", [user_id] ).fetchone() formatted_user = { - "username": user[0], - "id": user[1], - "country": user[2], - "registered": user[3], - "status": user[4], - "friends": count_friends(user_id), - "rating": - get_rating(user_id), #TODO: calculate rating based on game analysis + "username": user[0], + "id": user[1], + "country": user[2], + "registered": user[3], + "status": user[4], + "friends": count_friends(user_id), + "rating": + get_rating(user_id), #TODO: calculate rating based on game analysis } if viewer: #FIXME: validate viewer id? @@ -86,7 +86,7 @@ def index(): if username: temp_user_id = cursor.execute( - "select user_id from users where username = ?", [username] + "select user_id from users where username = ?", [username] ).fetchone() if len(temp_user_id) > 0: user_id = temp_user_id diff --git a/api/user/password.py b/api/user/password.py index ff52ba4..b77067f 100644 --- a/api/user/password.py +++ b/api/user/password.py @@ -10,7 +10,7 @@ def index(): data = request.get_json() if not data["password"] or \ - not data["newPassword"]: + not data["newPassword"]: return "", 400 return {}, 200 diff --git a/api/user/preferences.py b/api/user/preferences.py index 2feaade..2ca4a05 100644 --- a/api/user/preferences.py +++ b/api/user/preferences.py @@ -8,15 +8,15 @@ import json # fill missing dict keys in preferences object def format_preferences(prefs): return { - "darkMode": - prefs.get("darkMode") or False, - "ruleset": - resolve_ruleset(json.dumps(prefs.get("ruleset") or {}) or "default"), - "userColors": { - "diskA": prefs.get("userColors", {}).get("diskA") or "", - "diskB": prefs.get("userColors", {}).get("diskB") or "", - "background": prefs.get("userColors", {}).get("background") or "" - } + "darkMode": + prefs.get("darkMode") or False, + "ruleset": + resolve_ruleset(json.dumps(prefs.get("ruleset") or {}) or "default"), + "userColors": { + "diskA": prefs.get("userColors", {}).get("diskA") or "", + "diskB": prefs.get("userColors", {}).get("diskB") or "", + "background": prefs.get("userColors", {}).get("background") or "" + } } @@ -27,7 +27,7 @@ preferences = Blueprint('preferences', __name__) @auth_required("user") def get_preferences(login): user_prefs = cursor.execute( - "select preferences from users where user_id = ?", [login] + "select preferences from users where user_id = ?", [login] ).fetchone() return {"preferences": format_preferences(json.loads(user_prefs[0]))}, 200 @@ -41,8 +41,8 @@ def index(login): formatted_json = format_preferences(new_preferences) cursor.execute( - "update users set preferences = ? where user_id = ?", - [json.dumps(formatted_json), login] + "update users set preferences = ? where user_id = ?", + [json.dumps(formatted_json), login] ) connection.commit() diff --git a/api/user/status.py b/api/user/status.py index e762d12..219481a 100644 --- a/api/user/status.py +++ b/api/user/status.py @@ -14,8 +14,8 @@ def index(user_id): if not status: return "", 400 cursor.execute( - "update users set status = ? where user_id = ?", - [status[0:200], user_id] + "update users set status = ? where user_id = ?", + [status[0:200], user_id] ) connection.commit() |