]> littlesong.place Git - littlesongplace.git/commitdiff
Work on tags/collabs
authorChris Fulljames <christianfulljames@gmail.com>
Sun, 5 Jan 2025 02:16:41 +0000 (21:16 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Sun, 5 Jan 2025 02:16:41 +0000 (21:16 -0500)
main.py
schema.sql

diff --git a/main.py b/main.py
index 8952be5719786043814f89fcc720e98eb5bc9eb9..edbce88a32c2cd9cb245c3cf8e123a98866cdf58 100644 (file)
--- a/main.py
+++ b/main.py
@@ -36,7 +36,7 @@ def signup_post():
     password_confirm = request.form["password_confirm"]
 
     error = None
-    if not username.isalnum():
+    if not username.isidentifier():
         error = "Username cannot contain special characters"
     elif len(username) < 3:
         error ="Username must be at least 3 characters"
@@ -122,11 +122,56 @@ def upload_song():
     title = request.form["title"]
     description = request.form["description"]
 
+    error = None
+
+    # Check if tags are valid
+    tags = request.form["tags"]
+    tags = [t.strip() for t in tags.split(",")]
+    for tag in tags:
+        if not tag.isidentifier():
+            error = f"'{tag}' is not a valid tag name"
+            break
+
+    # Check if collaborators are valid
+    collaborators = request.form["collabs"]
+    collaborators = [c.strip() for c in collaborators.split(",")]
+    collab_ids = {}
+    for collab in collaborators:
+        # Check if @user exists
+        if collab.startswith("@"):
+            collab_user_data = query_db("select * from users where username = ?", [collab[1:]], one=True)
+            if collab_user_data is None:
+                error = f"Invalid collaborator username: {collab}"
+                break
+            else:
+                collab_ids[collab] = collab_user_data["userid"]
+
+        # Check if valid name
+        elif not collab.isprintable():
+            error = f"Invalid collaborator name: {collab}"
+            break
+
+    # TODO: Handle errors above
     # TODO: Validate song file
 
+    # Create song
     song_data = query_db(
             "insert into songs (userid, title, description) values (?, ?, ?) returning (songid)",
             [userid, title, description], one=True)
+    songid = song_data["songid"]
+
+    # Assign tags
+    for tag in tags:
+        query_db("insert into song_tags (tag, songid) values (?, ?)", [tag, songid])
+
+    # List collaborators
+    for collab in collaborators:
+        if collab.startswith("@"):
+            collab_id = collab_ids[collab]
+            query_db("insert into song_collaborators (songid, userid) values (?, ?)", [songid, collab_id])
+        else:
+            query_db("insert into song_collaborators (songid, name) values (?, ?)", [songid, collab])
+
     get_db().commit()
 
     filepath = userpath / (str(song_data["songid"]) + ".mp3")
@@ -138,8 +183,8 @@ def upload_song():
 def song(userid, songid):
     try:
         # Make sure values are valid integers
-        int(userid) 
-        int(songid) 
+        int(userid)
+        int(songid)
     except ValueError:
         abort(404)
 
index abf612640fb2e929d024cdcf245d918166a4320a..e7b8f22d7c345c6478873c3d4f831bfc87422ee5 100644 (file)
@@ -25,17 +25,10 @@ CREATE TABLE song_collaborators (
     CONSTRAINT userid_or_name CHECK ((userid IS NULL and name IS NOT NULL) OR (userid IS NOT NULL and name IS NULL))
 );
 
-DROP TABLE IF EXISTS tags;
-CREATE TABLE tags (
-    tagid INTEGER PRIMARY KEY,
-    name TEXT NOT NULL
-);
-
 DROP TABLE IF EXISTS song_tags;
 CREATE TABLE song_tags (
-    tagid INTEGER NOT NULL,
+    tag TEXT NOT NULL,
     songid INTEGER NOT NULL,
-    FOREIGN KEY(tagid) REFERENCES tags(tagid),
     FOREIGN KEY(songid) REFERENCES songs(songid),
-    PRIMARY KEY(tagid, songid)
+    PRIMARY KEY(tag, songid)
 );