]> littlesong.place Git - littlesongplace.git/commitdiff
Add user bio
authorChris Fulljames <christianfulljames@gmail.com>
Fri, 10 Jan 2025 01:37:22 +0000 (20:37 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Fri, 10 Jan 2025 01:37:22 +0000 (20:37 -0500)
main.py
schema.sql
templates/profile.html
todo.txt

diff --git a/main.py b/main.py
index 77611ad37d0e88697f9e02889898ebecc945d693..e56bebd7c261b10f357c95a959cceaeb4e234357 100644 (file)
--- a/main.py
+++ b/main.py
@@ -113,14 +113,26 @@ def users_profile(profile_username):
 
     # Get songs for current profile
     profile_userid = profile_data["userid"]
+    profile_bio = profile_data["bio"]
     songs = Song.get_all_for_user(profile_userid)
 
     return render_template(
             "profile.html",
             name=profile_username,
             userid=profile_userid,
+            bio=profile_bio,
             song_list=render_template("song-list.html", songs=songs))
 
+@app.post("/update-bio")
+def update_bio():
+    query_db(
+            "update users set bio = ? where userid = ?",
+            [request.form["bio"], session["userid"]])
+    get_db().commit()
+    flash("Bio updated successfully")
+
+    return redirect(f"/users/{session['username']}")
+
 @app.get("/edit-song")
 def edit_song():
     if not "userid" in session:
index 2dcc41d2ef9fd697d28c79cd2c033501922103f9..006709ed94a13df65c7fd16835d182279c1e03de 100644 (file)
@@ -2,8 +2,10 @@ DROP TABLE IF EXISTS users;
 CREATE TABLE users (
     userid INTEGER PRIMARY KEY AUTOINCREMENT,
     username TEXT UNIQUE NOT NULL,
-    password TEXT NOT NULL
+    password TEXT NOT NULL,
+    bio TEXT
 );
+CREATE INDEX users_by_name ON users(username);
 
 DROP TABLE IF EXISTS songs;
 CREATE TABLE songs (
index f439d34c8e02863836432c2b62706848082e73fb..4cd2ff7fb9c4677fa5e0fb3184a39d7d17080aad 100644 (file)
@@ -6,6 +6,40 @@
 
 <h1 class="profile-name">{{ name }}</h1>
 
+<!-- Bio -->
+<div class="profile-bio" id="profile-bio">{{ bio }}</div>
+
+<!-- Bio edit form -->
+{% if session["userid"] == userid %}
+
+<a href="javascript:showEditForm();" id="profile-bio-edit-btn">Edit Bio</a>
+
+<form id="profile-edit-form" action="/update-bio" method="post" hidden>
+    <div class="profile-edit">
+        <textarea name="bio">{{ bio }}</textarea>
+    </div>
+    <div class="profile-edit">
+        <a href="javascript:hideEditForm();">Cancel</a>
+        <input type="submit" value="Save">
+    </div>
+</form>
+
+<!-- Show/hide bio edit form -->
+<script>
+    function showEditForm() {
+        document.getElementById("profile-bio").hidden = true;
+        document.getElementById("profile-bio-edit-btn").hidden = true;
+        document.getElementById("profile-edit-form").hidden = false;
+    }
+    function hideEditForm() {
+        document.getElementById("profile-bio").hidden = false;
+        document.getElementById("profile-bio-edit-btn").hidden = false;
+        document.getElementById("profile-edit-form").hidden = true;
+    }
+</script>
+
+{% endif %}
+
 <h2>Songs</h2>
 
 <!-- Upload New Song button -->
@@ -13,6 +47,7 @@
 <a href="/edit-song">Upload New Song</a>
 {% endif %}
 
+<!-- Song List -->
 {{ song_list|safe }}
 
 {% endblock %}
index 4bbcb80ba2d981592187b373e4d2eacbca33efc3..230c9a8f3cca33d6701f8419213e6526b2444954 100644 (file)
--- a/todo.txt
+++ b/todo.txt
@@ -1,5 +1,4 @@
-- user bio
-- edit user bio
+- markup in user bio
 - delete user account
 - sort songs by upload timestamp