Save user settings in database for cross-browser fetching
This commit is contained in:
parent
1ffbc1b6f0
commit
2f8ab9ac96
@ -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"])
|
||||
|
@ -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)
|
||||
|
@ -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"
|
||||
);
|
Loading…
Reference in New Issue
Block a user