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():
@app.get("/song/<userid>/<songid>")
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)
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"""
# 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)
-
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,
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)
+);
<input type="text" name="title" required></input>
</div>
<div class="upload-form">
- <label for="title">Description (optional)</label>
+ <label for="title">Description</label>
<textarea name="description"></textarea>
</div>
+ <div class="upload-form">
+ <label for="tags">Tags</label>
+ <input type="text" name="tags" placeholder="country, extratone, vocals, ..."></input>
+ </div>
+ <div class="upload-form">
+ <label for="collabs">Collaborators</label>
+ <input type="text" name="collabs" placeholder="@fren_user, John Doe, ..."></input>
+ </div>
<div class="upload-form">
<input type="submit" value="Upload"></input>
</div>