aboutsummaryrefslogtreecommitdiff
path: root/api/user/preferences.py
blob: 6c8a6f32a6db7b2d68bc3e84f5b35a2320c6fd8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from flask import Blueprint, request
from db import cursor, connection
from auth.login_token import token_login
import json

def format_preferences(preferences): #TODO: remove excess properties (create preferences class?)
    return json.dumps(preferences) or ""

preferences = Blueprint('preferences', __name__)

@preferences.route('/preferences', methods = ["GET"])
def get_preferences():
    data = request.get_json()

    token = request.cookies.get("token") or ""

    if not token: return "", 401
    login = token_login(token) or ""

    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

@preferences.route('/preferences', methods = ["POST"])
def index():
    data = request.get_json()

    new_preferences = data.get("newPreferences") or ""
    token = request.cookies.get("token") or ""

    if not token: return "", 401
    login = token_login(token) or ""

    if not login: return "", 403

    formatted_json = format_preferences(new_preferences)
    if not formatted_json: return "", 400

    cursor.execute("update users set preferences = ? where user_id = ?", [formatted_json, login])
    connection.commit()

    return "", 200

dynamic_route = ["/user", preferences]