]> littlesong.place Git - littlesongplace.git/commitdiff
More work on jams
authorChris Fulljames <christianfulljames@gmail.com>
Thu, 10 Apr 2025 11:32:00 +0000 (07:32 -0400)
committerChris Fulljames <christianfulljames@gmail.com>
Thu, 10 Apr 2025 11:32:00 +0000 (07:32 -0400)
src/littlesongplace/auth.py
src/littlesongplace/db.py
src/littlesongplace/jams.py
src/littlesongplace/sql/schema_revert.sql
src/littlesongplace/sql/schema_update.sql
test/test_jams.py [new file with mode: 0644]

index c4c790c31f2410c191d84213930df59afe57db99..c886179bfa2bddc7ebaf2934de84cc8d6e78b7c8 100644 (file)
@@ -1,7 +1,8 @@
 from datetime import datetime, timezone
+from functools import wraps
 
 import bcrypt
-from flask import Blueprint, render_template, redirect, flash, request, current_app, session
+from flask import Blueprint, render_template, redirect, flash, g, request, current_app, session
 
 from . import comments, db
 from .logutils import flash_and_log
@@ -101,3 +102,16 @@ def logout():
 
     return redirect("/")
 
+def requires_login(f):
+    @functools.wraps(f)
+    def _wrapper(*args, **kwargs):
+        if not "userid" in session:
+            return redirect("/login")
+
+        g.userid = session["userid"]
+        g.username = session["username"]
+
+        return f(*args, **kwargs)
+
+    return _wrapper
+
index 8c10ded0bdd77c33f7358fda08070c217298753b..d4e1fb164455c14d6375c95841cf4639ad294a1c 100644 (file)
@@ -6,7 +6,7 @@ from flask import g, current_app
 
 from . import datadir
 
-DB_VERSION = 4
+DB_VERSION = 5
 
 def get():
     db = getattr(g, '_database', None)
@@ -49,6 +49,15 @@ def init_cmd():
             db.cursor().executescript(f.read())
         db.commit()
 
+@click.command("revert-db")
+def init_cmd():
+    """Revert the database to the previous schema"""
+    with current_app.app_context():
+        db = sqlite3.connect(datadir.get_db_path())
+        with current_app.open_resource('sql/schema_revert.sql', mode='r') as f:
+            db.cursor().executescript(f.read())
+        db.commit()
+
 def init_app(app):
     app.cli.add_command(init_cmd)
     app.teardown_appcontext(close)
index 1e729a18571bba998474b987c36789b8037ed6af..2eef2349524b33b0a50590a529bd595f6bfd5434 100644 (file)
@@ -1,39 +1,62 @@
-from flask import Blueprint, render_template
+from datetime import datetime, timezone
+
+from flask import Blueprint, g, render_template, url_for
+
+from . import db
 
 bp = Blueprint("jams", __name__, url_prefix="/jams")
 
 @bp.get("/")
 def jams():
-    # Show a list of all jams: current, upcoming, previous
+    # Show a list of all jams (or events?): current, upcoming, previous
     ...
 
 @bp.get("/create")
+@requires_login
 def create():
     # Create a new jam and redirect to the edit form
-    ...
+    timestamp = datetime.now(timezone.utc).isoformat()
+    row = db.query(
+            """
+            INSERT INTO jams (ownerid, created, title)
+            VALUES (?, ?, ?)
+            RETURNING jamid
+            """,
+            args=[g.userid, timestamp, f"New Jam"],
+            one=True)
+    db.commit()
+    jamid = row["jamid"]
+    return redirect(url_for('jam', jamid=jamid))
 
 @bp.get("/<jamid>")
 def jam(jamid):
-    # Show the current/most recent event
-    # TODO: Redirect to current event page
-    return render_template("jam.html")
+    row = db.query("SELECT * FROM jams WHERE jamid = ?", [jamid], one=True)
+    # Show the main jam page
+    return render_template(
+            "jam.html",
+            title=row["title"],
+            owner=row["userid"],
+            description=row["description"])
 
 @bp.post("/<jamid>/update")
+@requires_login
 def update(jamid):
     # Update a jam with the new form data, redirect to view page
     ...
 
 @bp.get("/<jamid>/delete")
+@requires_login
 def delete(jamid):
     # Delete a jam, redirect to the jams list
     ...
 
 @bp.get("/<jamid>/events")
 def events(jamid):
-    # Show a list of all events for the jam
+    # Show a list of all events for the jam (current, upcoming, previous)
     ...
 
 @bp.get("/<jamid>/events/create")
+@requires_login
 def events_create():
     # Create a new event and redirect to the edit form
     ...
@@ -44,11 +67,13 @@ def events_view(eventid):
     ...
 
 @bp.post("/<jamid>/events/<int:eventid>/update")
+@requires_login
 def events_update(jamid):
     # Update an event with the new form data
     ...
 
 @bp.get("/<jamid>/events/<int:eventid>/delete")
+@requires_login
 def events_delete(jamid):
     # Delete an event, redirect to list of all events
     ...
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..2fdb19274a91ccd1839a18ec175f5ec5367a0779 100644 (file)
@@ -0,0 +1,5 @@
+DROP TABLE IF EXISTS jams;
+DROP TABLE IF EXISTS jam_events;
+
+PRAGMA user_version = 4;
+
index fb4cc15648d1acf9ca180850fc6bcb649095f514..527381c73b675eb5c14b39a2680b5bef0ae61022 100644 (file)
@@ -1,23 +1,26 @@
 --DROP TABLE IF EXISTS jams;
 CREATE TABLE jams (
     jamid INTEGER PRIMARY KEY,
-    owner INTEGER NOT NULL,
+    ownerid INTEGER NOT NULL,
     created TEXT NOT NULL,
     title TEXT NOT NULL,
     description TEXT,
-    FOREIGN KEY(owner) REFERENCES users(userid)
+    FOREIGN KEY(ownerid) REFERENCES users(userid)
 );
 
 --DROP TABLE IF EXISTS jam_events;
 CREATE TABLE jam_events(
     eventid INTEGER PRIMARY KEY,
     jamid INTEGER NOT NULL,
-    created TEXT NOT NULL,
-    title TEXT NOT NULL,
-    startdate TEXT NOT NULL,
-    enddate TEXT NOT NULL,
     threadid INTEGER NOT NULL,
-    description TEXT,
+    created TEXT NOT NULL,
+    title TEXT NOT NULL, -- Hidden until startdate
+    startdate TEXT,
+    enddate TEXT,
+    description TEXT, -- Hidden until startdate
     FOREIGN KEY(jamid) REFERENCES jams(jamid),
     FOREIGN KEY(threadid) REFERENCES comment_threads(threadid)
 );
+
+PRAGMA user_version = 5;
+
diff --git a/test/test_jams.py b/test/test_jams.py
new file mode 100644 (file)
index 0000000..eacb428
--- /dev/null
@@ -0,0 +1,14 @@
+import pytest
+
+from .utils import create_user
+
+@pytest.fixture
+def user(client):
+    create_user(client, "user", login=True)
+    yield "user"
+
+def test_create_jam(client, user):
+    response = client.get("/jams/create", follow_redirects=True)
+    assert response.status_code == 200
+    assert b"New Jam" in response.data
+