aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-03-15 11:39:58 +0100
committerlonkaars <l.leblansch@gmail.com>2021-03-15 11:39:58 +0100
commit1c5ee061f745b6ea1ea584a0047c99e88a5d3fcb (patch)
treea335aaea9438395a17b2d56401cf5d04b8812fc2
parent87328fa0fc773de63ed718f3e0a40940de1be7d8 (diff)
always send full preferences object and trim excess properties
-rw-r--r--api/user/preferences.py26
1 files changed, 21 insertions, 5 deletions
diff --git a/api/user/preferences.py b/api/user/preferences.py
index 6c8a6f3..cb3bf5e 100644
--- a/api/user/preferences.py
+++ b/api/user/preferences.py
@@ -1,10 +1,21 @@
from flask import Blueprint, request
from db import cursor, connection
from auth.login_token import token_login
+from ruleset import resolve_ruleset
import json
-def format_preferences(preferences): #TODO: remove excess properties (create preferences class?)
- return json.dumps(preferences) or ""
+class preferences_class:
+ def __init__(self, preferences):
+ self.darkMode = preferences.get("darkMode") or False
+ self.ruleset = resolve_ruleset(json.dumps(preferences.get("ruleset") or {}) or "default")
+ self.userColors = {
+ "diskA": preferences.get("userColors", {}).get("diskA") or "",
+ "diskB": preferences.get("userColors", {}).get("diskB") or "",
+ "background": preferences.get("userColors", {}).get("background") or ""
+ }
+
+def format_preferences(prefs):
+ return json.dumps((preferences_class(prefs)).__dict__)
preferences = Blueprint('preferences', __name__)
@@ -20,7 +31,7 @@ def get_preferences():
if not login: return "", 403
user_prefs = cursor.execute("select preferences from users where user_id = ?", [login]).fetchone()
- return { "preferences": json.loads(user_prefs[0]) }, 200
+ return { "preferences": format_preferences(json.loads(user_prefs[0])) }, 200
@preferences.route('/preferences', methods = ["POST"])
def index():
@@ -34,8 +45,13 @@ def index():
if not login: return "", 403
- formatted_json = format_preferences(new_preferences)
- if not formatted_json: return "", 400
+ preferences_json = {}
+ try:
+ preferences_json = json.loads(new_preferences)
+ except ValueError as e:
+ return "", 400
+
+ formatted_json = format_preferences(preferences_json)
cursor.execute("update users set preferences = ? where user_id = ?", [formatted_json, login])
connection.commit()