diff --git a/kanken_online/__init__.py b/kanken_online/__init__.py index 034df68..19d34be 100644 --- a/kanken_online/__init__.py +++ b/kanken_online/__init__.py @@ -2,6 +2,8 @@ import json import os from flask import Flask, redirect, render_template, g, request, session, url_for from pathlib import Path + +from kanken_online.database import get_database from .auth import login_required @@ -34,8 +36,25 @@ def create_app(test_config=None): return render_template("about.html") def update_settings(form): + db = get_database() + + if session["user_id"]: + # Set values in the database + settings = db.execute("SELECT * FROM user_settings WHERE user_id = ?", + (session["user_id"],) + ).fetchone() + if settings: + db.execute("UPDATE user_settings SET lang = ?, theme = ? WHERE user_id = ?", + (form["language"], form["theme"], session["user_id"])) + else: + db.execute("INSERT INTO user_settings VALUES (?, ?, ?)", + (session["user_id"], form["language"], form["theme"])) + db.commit() + + # Set values directly in the session session["language"] = form["language"] session["theme"] = form["theme"] + return redirect("/options") @app.route("/options", methods=["GET", "POST"]) diff --git a/kanken_online/auth.py b/kanken_online/auth.py index 50069b0..5015493 100644 --- a/kanken_online/auth.py +++ b/kanken_online/auth.py @@ -53,6 +53,14 @@ def login(): if error is None: session.clear() session["user_id"] = user["id"] + settings = db.execute("SELECT * FROM user_settings WHERE user_id = ?", + (session["user_id"],) + ).fetchone() + if settings: + session["language"] = settings["lang"] + session["theme"] = settings["theme"] + + return redirect(url_for("index")) flash(error) diff --git a/kanken_online/schema.sql b/kanken_online/schema.sql index 7903187..fe17c90 100644 --- a/kanken_online/schema.sql +++ b/kanken_online/schema.sql @@ -1,5 +1,6 @@ DROP TABLE IF EXISTS user; --- DROP TABLE IF EXISTS post; +DROP TABLE IF EXISTS post; +DROP TABLE IF EXISTS user_settings; CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -15,3 +16,9 @@ CREATE TABLE post ( body TEXT NOT NULL, FOREIGN KEY (author_id) REFERENCES user (id) ); + +CREATE TABLE user_settings ( + user_id INTEGER PRIMARY KEY AUTOINCREMENT REFERENCES user (id), + lang VARCHAR(3) NOT NULL, -- e.g. en/jp; may add support for other (possibly 3-long) codes later + theme VARCHAR(5) NOT NULL -- "light" or "dark" +); \ No newline at end of file