Add profile pictures
This commit is contained in:
parent
8148976913
commit
003e1ae635
@ -1,6 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
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 pathlib import Path
|
||||||
|
|
||||||
from kanken_online.api import get_kanji_by_character
|
from kanken_online.api import get_kanji_by_character
|
||||||
@ -10,12 +11,14 @@ from .auth import login_required
|
|||||||
|
|
||||||
DATABASE_NAME = "kanken_online.sqlite"
|
DATABASE_NAME = "kanken_online.sqlite"
|
||||||
JITEN_DB_NAME = "kanken.db"
|
JITEN_DB_NAME = "kanken.db"
|
||||||
|
PFP_DIRECTORY_NAME = "pfp"
|
||||||
def create_app(test_config=None):
|
def create_app(test_config=None):
|
||||||
app = Flask(__name__, instance_relative_config=True)
|
app = Flask(__name__, instance_relative_config=True)
|
||||||
app.config.from_mapping(
|
app.config.from_mapping(
|
||||||
SECRET_KEY="dev",
|
SECRET_KEY="dev",
|
||||||
DATABASE=str(Path(app.instance_path) / DATABASE_NAME),
|
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:
|
if test_config is None:
|
||||||
@ -38,7 +41,7 @@ def create_app(test_config=None):
|
|||||||
def about_page():
|
def about_page():
|
||||||
return render_template("about.html")
|
return render_template("about.html")
|
||||||
|
|
||||||
def update_settings(form):
|
def update_settings(form, files):
|
||||||
db = get_database()
|
db = get_database()
|
||||||
|
|
||||||
if "user_id" in session:
|
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 = ?",
|
db.execute("UPDATE user_settings SET lang = ?, theme = ? WHERE user_id = ?",
|
||||||
(form["language"], form["theme"], session["user_id"]))
|
(form["language"], form["theme"], session["user_id"]))
|
||||||
else:
|
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"]))
|
(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()
|
db.commit()
|
||||||
|
|
||||||
# Set values directly in the session
|
# Set values directly in the session
|
||||||
@ -65,13 +76,13 @@ def create_app(test_config=None):
|
|||||||
if request.method == "GET":
|
if request.method == "GET":
|
||||||
return render_template("options.html")
|
return render_template("options.html")
|
||||||
else:
|
else:
|
||||||
return update_settings(request.form)
|
return update_settings(request.form, request.files)
|
||||||
|
|
||||||
@app.get("/user/<int:user_id>")
|
@app.get("/user/<int:user_id>")
|
||||||
def user_page(user_id: int):
|
def user_page(user_id: int):
|
||||||
db = get_database()
|
db = get_database()
|
||||||
(username,) = db.execute("SELECT username FROM user WHERE id = ?", (user_id,)).fetchone()
|
(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)
|
return render_template("user_page.html", username=username, pfp_filename=os.path.join(app.config["PFP_STORE"], pfp_filename))
|
||||||
|
|
||||||
@app.get("/me")
|
@app.get("/me")
|
||||||
@login_required
|
@login_required
|
||||||
@ -156,6 +167,10 @@ def create_app(test_config=None):
|
|||||||
lang.update_languages()
|
lang.update_languages()
|
||||||
return redirect("/translations")
|
return redirect("/translations")
|
||||||
|
|
||||||
|
@app.get(app.config["PFP_STORE"] + "/<img>")
|
||||||
|
def get_pfp(img: str):
|
||||||
|
return send_file(os.path.join(app.config["PFP_STORE"], img))
|
||||||
|
|
||||||
@app.route("/data")
|
@app.route("/data")
|
||||||
def data_page():
|
def data_page():
|
||||||
return render_template("data.html")
|
return render_template("data.html")
|
||||||
|
@ -20,5 +20,6 @@ CREATE TABLE post (
|
|||||||
CREATE TABLE user_settings (
|
CREATE TABLE user_settings (
|
||||||
user_id INTEGER PRIMARY KEY AUTOINCREMENT REFERENCES user (id),
|
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
|
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
|
||||||
);
|
);
|
@ -42,5 +42,6 @@
|
|||||||
"title": "Title",
|
"title": "Title",
|
||||||
"post_body": "Post body",
|
"post_body": "Post body",
|
||||||
"success_new_post": "Successfully created new post",
|
"success_new_post": "Successfully created new post",
|
||||||
"user_introduction": "User"
|
"user_introduction": "User",
|
||||||
|
"pfp": "Profile picture"
|
||||||
}
|
}
|
@ -42,5 +42,6 @@
|
|||||||
"title": "話題",
|
"title": "話題",
|
||||||
"post_body": "投稿内容",
|
"post_body": "投稿内容",
|
||||||
"success_new_post": "無事に新しい投稿を作りました",
|
"success_new_post": "無事に新しい投稿を作りました",
|
||||||
"user_introduction": "利用者の"
|
"user_introduction": "利用者の",
|
||||||
|
"pfp": "利用者アイコン"
|
||||||
}
|
}
|
@ -5,7 +5,7 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<form method="post">
|
<form method="post" enctype=multipart/form-data>
|
||||||
<label for="language-select">{{ localize("language") }}</label>
|
<label for="language-select">{{ localize("language") }}</label>
|
||||||
<select name="language" id="language-select">
|
<select name="language" id="language-select">
|
||||||
<option value="ja">日本語</option>
|
<option value="ja">日本語</option>
|
||||||
@ -17,6 +17,10 @@
|
|||||||
<option value="dark">{{ localize("dark_theme") }}</option>
|
<option value="dark">{{ localize("dark_theme") }}</option>
|
||||||
<option value="light">{{ localize("light_theme") }}</option>
|
<option value="light">{{ localize("light_theme") }}</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<label for="pfp">{{ localize("pfp") }}</label>
|
||||||
|
<input type="file" id="pfp" name="pfp">
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
<button type="submit">Okay</button>
|
<button type="submit">Okay</button>
|
||||||
</form>
|
</form>
|
||||||
|
@ -5,5 +5,6 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
(profile picture)
|
<img id="pfp" src="{{ pfp_filename }}" width=128 height=128>
|
||||||
|
<span id="username">{{ username }}</span>
|
||||||
{% endblock %}
|
{% endblock %}
|
Loading…
Reference in New Issue
Block a user