Separate public and logged-in user settings

This commit is contained in:
Kiril Kovachev 2024-10-21 10:20:15 +01:00
parent 6c1e0887b6
commit 009cb6aade
3 changed files with 66 additions and 43 deletions

View File

@ -43,41 +43,56 @@ def create_app(test_config=None):
def update_settings(form, files):
db = get_database()
if "user_id" in session:
# 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 (user_id, lang, theme) VALUES (?, ?, ?)",
(session["user_id"], form["language"], form["theme"]))
if "pfp" in files and files["pfp"].filename != "":
stored_filename = str(uuid.uuid4())
pfp = files["pfp"]
pfp.save(os.path.join(app.config["PFP_STORE"], stored_filename))
db.execute("UPDATE user_settings SET pfp_filename = ?", (stored_filename,))
db.commit()
# 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 (user_id, lang, theme) VALUES (?, ?, ?)",
(session["user_id"], form["language"], form["theme"]))
if "pfp" in files and files["pfp"].filename != "":
stored_filename = str(uuid.uuid4())
pfp = files["pfp"]
pfp.save(os.path.join(app.config["PFP_STORE"], stored_filename))
db.execute("UPDATE user_settings SET pfp_filename = ?", (stored_filename,))
db.commit()
update_logged_out_settings(form)
return redirect("/options")
def update_logged_out_settings(form):
# Set values directly in the session
session["language"] = form["language"]
session["theme"] = form["theme"]
return redirect("/options")
return redirect("/public_options")
@app.route("/options", methods=["GET", "POST"])
def options():
if request.method == "GET":
return render_template("options.html")
if "user_id" in session:
return render_template("options.html")
else:
return redirect("/public_options")
else:
return update_settings(request.form, request.files)
@app.route("/public_options", methods=["GET", "POST"])
def public_options():
if request.method == "GET":
if "user_id" in session:
return redirect("/public_options")
else:
return render_template("logged_out_options.html")
else:
return update_logged_out_settings(request.form)
@app.get("/user/<int:user_id>")
def user_page(user_id: int):
db = get_database()

View File

@ -0,0 +1,25 @@
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}{{ localize("options") }}{% endblock %}</h1>
{% endblock %}
{% block content %}
<form method="post" enctype=multipart/form-data>
<label for="language-select">{{ localize("language") }}</label>
<select name="language" id="language-select">
<option value="ja">日本語</option>
<option value="en">English</option>
</select>
<label for="theme-select">{{ localize("theme") }}</label>
<select name="theme" id="theme-select">
<option value="dark">{{ localize("dark_theme") }}</option>
<option value="light">{{ localize("light_theme") }}</option>
</select>
{% block logged_in_options %}{% endblock %}
<hr>
<button type="submit">Okay</button>
</form>
{% endblock %}

View File

@ -1,27 +1,10 @@
{% extends 'base.html' %}
{% extends 'logged_out_options.html' %}
{% block header %}
<h1>{% block title %}{{ localize("options") }}{% endblock %}</h1>
{% endblock %}
{% block content %}
<form method="post" enctype=multipart/form-data>
<label for="language-select">{{ localize("language") }}</label>
<select name="language" id="language-select">
<option value="ja">日本語</option>
<option value="en">English</option>
</select>
<label for="theme-select">{{ localize("theme") }}</label>
<select name="theme" id="theme-select">
<option value="dark">{{ localize("dark_theme") }}</option>
<option value="light">{{ localize("light_theme") }}</option>
</select>
{% block logged_in_options %}
<label for="pfp">{{ localize("pfp") }}</label>
<input type="file" id="pfp" name="pfp">
<hr>
<button type="submit">Okay</button>
</form>
{% endblock %}