From 3d2d8d21d361b7c8a0731b42010166a2441114e4 Mon Sep 17 00:00:00 2001 From: Chris Fulljames Date: Sat, 4 Jan 2025 19:10:09 -0500 Subject: [PATCH] Start work on tags and collaborators --- main.py | 32 ++++++++------------------------ schema.sql | 29 +++++++++++++++++++++++++++-- templates/profile.html | 10 +++++++++- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/main.py b/main.py index d22eadf..8952be5 100644 --- a/main.py +++ b/main.py @@ -10,25 +10,14 @@ from flask import Flask, render_template, request, redirect, g, session, abort, send_from_directory from werkzeug.utils import secure_filename -################################################################################ -# Check for Required Environment Variables -################################################################################ - -REQUIRED_VARS = ["SECRET_KEY", "DATA_DIR"] - -for var in REQUIRED_VARS: - if var not in os.environ: - print(f"{var} not set") - sys.exit(1) - -DATA_DIR = Path(os.environ["DATA_DIR"]) - ################################################################################ # Routes ################################################################################ +DATA_DIR = Path(".") + app = Flask(__name__) -app.secret_key = os.environ["SECRET_KEY"] +app.secret_key = "dev" @app.route("/") def index(): @@ -148,7 +137,9 @@ def upload_song(): @app.get("/song//") def song(userid, songid): try: - int(userid) # Make sure userid is a valid integer + # Make sure values are valid integers + int(userid) + int(songid) except ValueError: abort(404) @@ -177,6 +168,7 @@ def query_db(query, args=(), one=False): cur.close() return (rv[0] if rv else None) if one else rv +@app.cli.add_command @click.command("init-db") def init_db(): """Clear the existing data and create new tables""" @@ -191,18 +183,10 @@ def init_db(): # Generate Session Key ################################################################################ +@app.cli.add_command @click.command("gen-key") def gen_key(): """Generate a secret key for session cookie encryption""" import secrets print(secrets.token_hex()) - -################################################################################ -# App Configuration -################################################################################ - -app.teardown_appcontext(close_db) -app.cli.add_command(init_db) -app.cli.add_command(gen_key) - diff --git a/schema.sql b/schema.sql index 331c5be..abf6126 100644 --- a/schema.sql +++ b/schema.sql @@ -1,12 +1,11 @@ DROP TABLE IF EXISTS users; -DROP TABLE IF EXISTS songs; - CREATE TABLE users ( userid INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE NOT NULL, password TEXT NOT NULL ); +DROP TABLE IF EXISTS songs; CREATE TABLE songs ( songid INTEGER PRIMARY KEY AUTOINCREMENT, userid INTEGER NOT NULL, @@ -14,3 +13,29 @@ CREATE TABLE songs ( description TEXT, FOREIGN KEY(userid) REFERENCES users(userid) ); + +DROP TABLE IF EXISTS song_collaborators; +CREATE TABLE song_collaborators ( + collabid INTEGER NOT NULL, + songid INTEGER NOT NULL, + userid INTEGER, + name TEXT, + FOREIGN KEY(userid) REFERENCES users(userid), + PRIMARY KEY(collabid, songid), + 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, + songid INTEGER NOT NULL, + FOREIGN KEY(tagid) REFERENCES tags(tagid), + FOREIGN KEY(songid) REFERENCES songs(songid), + PRIMARY KEY(tagid, songid) +); diff --git a/templates/profile.html b/templates/profile.html index 77d0a05..92fc915 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -18,9 +18,17 @@
- +
+
+ + +
+
+ + +
-- 2.39.5