From 003e1ae635c194f9ea86a781f73e19e519fbf8f3 Mon Sep 17 00:00:00 2001 From: Kiril Kovachev Date: Sat, 19 Oct 2024 17:13:31 +0100 Subject: [PATCH] Add profile pictures --- kanken_online/__init__.py | 29 +++++++++++++++++++------- kanken_online/schema.sql | 3 ++- kanken_online/static/lang/en.json | 3 ++- kanken_online/static/lang/ja.json | 3 ++- kanken_online/templates/options.html | 6 +++++- kanken_online/templates/user_page.html | 3 ++- 6 files changed, 35 insertions(+), 12 deletions(-) diff --git a/kanken_online/__init__.py b/kanken_online/__init__.py index 9abd87a..f84a71f 100644 --- a/kanken_online/__init__.py +++ b/kanken_online/__init__.py @@ -1,6 +1,7 @@ import json import os -from flask import Flask, redirect, render_template, g, request, session, url_for +import uuid +from flask import Flask, redirect, render_template, g, request, send_file, session, url_for from pathlib import Path from kanken_online.api import get_kanji_by_character @@ -10,12 +11,14 @@ from .auth import login_required DATABASE_NAME = "kanken_online.sqlite" JITEN_DB_NAME = "kanken.db" +PFP_DIRECTORY_NAME = "pfp" def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY="dev", DATABASE=str(Path(app.instance_path) / DATABASE_NAME), - JITEN_DB=str(Path(app.instance_path) / JITEN_DB_NAME) + JITEN_DB=str(Path(app.instance_path) / JITEN_DB_NAME), + PFP_STORE=str(Path(app.instance_path) / PFP_DIRECTORY_NAME) ) if test_config is None: @@ -38,7 +41,7 @@ def create_app(test_config=None): def about_page(): return render_template("about.html") - def update_settings(form): + def update_settings(form, files): db = get_database() if "user_id" in session: @@ -50,8 +53,16 @@ def create_app(test_config=None): 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 (?, ?, ?)", + db.execute("INSERT INTO user_settings (user_id, lang, theme) VALUES (?, ?, ?)", (session["user_id"], form["language"], form["theme"])) + + if "pfp" in files: + 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 directly in the session @@ -65,13 +76,13 @@ def create_app(test_config=None): if request.method == "GET": return render_template("options.html") else: - return update_settings(request.form) + return update_settings(request.form, request.files) @app.get("/user/") def user_page(user_id: int): db = get_database() - (username,) = db.execute("SELECT username FROM user WHERE id = ?", (user_id,)).fetchone() - return render_template("user_page.html", username=username) + (username,pfp_filename) = db.execute("SELECT username, pfp_filename FROM user, user_settings WHERE user.id = ? AND user_settings.user_id = ?", (user_id,user_id)).fetchone() + return render_template("user_page.html", username=username, pfp_filename=os.path.join(app.config["PFP_STORE"], pfp_filename)) @app.get("/me") @login_required @@ -156,6 +167,10 @@ def create_app(test_config=None): lang.update_languages() return redirect("/translations") + @app.get(app.config["PFP_STORE"] + "/") + def get_pfp(img: str): + return send_file(os.path.join(app.config["PFP_STORE"], img)) + @app.route("/data") def data_page(): return render_template("data.html") diff --git a/kanken_online/schema.sql b/kanken_online/schema.sql index fe17c90..929e5b5 100644 --- a/kanken_online/schema.sql +++ b/kanken_online/schema.sql @@ -20,5 +20,6 @@ CREATE TABLE post ( 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" + theme VARCHAR(5) NOT NULL, -- "light" or "dark" + pfp_filename CHAR(36) -- 36-char UUID string ); \ No newline at end of file diff --git a/kanken_online/static/lang/en.json b/kanken_online/static/lang/en.json index 5f9a3b5..593b2cd 100644 --- a/kanken_online/static/lang/en.json +++ b/kanken_online/static/lang/en.json @@ -42,5 +42,6 @@ "title": "Title", "post_body": "Post body", "success_new_post": "Successfully created new post", -"user_introduction": "User" +"user_introduction": "User", +"pfp": "Profile picture" } \ No newline at end of file diff --git a/kanken_online/static/lang/ja.json b/kanken_online/static/lang/ja.json index b504da3..b359405 100644 --- a/kanken_online/static/lang/ja.json +++ b/kanken_online/static/lang/ja.json @@ -42,5 +42,6 @@ "title": "話題", "post_body": "投稿内容", "success_new_post": "無事に新しい投稿を作りました", -"user_introduction": "利用者の" +"user_introduction": "利用者の", +"pfp": "利用者アイコン" } \ No newline at end of file diff --git a/kanken_online/templates/options.html b/kanken_online/templates/options.html index 028b4e6..b6a5d8c 100644 --- a/kanken_online/templates/options.html +++ b/kanken_online/templates/options.html @@ -5,7 +5,7 @@ {% endblock %} {% block content %} -
+ + + + +
diff --git a/kanken_online/templates/user_page.html b/kanken_online/templates/user_page.html index edc2e35..95140b2 100644 --- a/kanken_online/templates/user_page.html +++ b/kanken_online/templates/user_page.html @@ -5,5 +5,6 @@ {% endblock %} {% block content %} - (profile picture) + + {{ username }} {% endblock %} \ No newline at end of file