From dd876856b88bfb75abe0c0619255a30210e78a9d Mon Sep 17 00:00:00 2001 From: Chris Fulljames Date: Fri, 11 Apr 2025 17:00:17 -0400 Subject: [PATCH] Add Jam and JamEvent objects --- src/littlesongplace/auth.py | 2 +- src/littlesongplace/jams.py | 87 +++++++++++++++++++++----- src/littlesongplace/templates/jam.html | 23 +++---- 3 files changed, 81 insertions(+), 31 deletions(-) diff --git a/src/littlesongplace/auth.py b/src/littlesongplace/auth.py index c886179..174a5f2 100644 --- a/src/littlesongplace/auth.py +++ b/src/littlesongplace/auth.py @@ -1,5 +1,5 @@ +import functools from datetime import datetime, timezone -from functools import wraps import bcrypt from flask import Blueprint, render_template, redirect, flash, g, request, current_app, session diff --git a/src/littlesongplace/jams.py b/src/littlesongplace/jams.py index 2eef234..a3f0033 100644 --- a/src/littlesongplace/jams.py +++ b/src/littlesongplace/jams.py @@ -1,8 +1,9 @@ +from dataclasses import dataclass from datetime import datetime, timezone -from flask import Blueprint, g, render_template, url_for +from flask import Blueprint, g, redirect, render_template, url_for -from . import db +from . import auth, db bp = Blueprint("jams", __name__, url_prefix="/jams") @@ -12,7 +13,7 @@ def jams(): ... @bp.get("/create") -@requires_login +@auth.requires_login def create(): # Create a new jam and redirect to the edit form timestamp = datetime.now(timezone.utc).isoformat() @@ -26,26 +27,28 @@ def create(): one=True) db.commit() jamid = row["jamid"] - return redirect(url_for('jam', jamid=jamid)) + return redirect(url_for('jams.jam', jamid=jamid)) @bp.get("/") def jam(jamid): - row = db.query("SELECT * FROM jams WHERE jamid = ?", [jamid], one=True) + row = db.query( + """ + SELECT * FROM jams + INNER JOIN users ON jams.ownerid = users.userid + WHERE jamid = ? + """, [jamid], one=True) + jam = Jam.from_row(row) # Show the main jam page - return render_template( - "jam.html", - title=row["title"], - owner=row["userid"], - description=row["description"]) + return render_template("jam.html", jam=jam) @bp.post("//update") -@requires_login +@auth.requires_login def update(jamid): # Update a jam with the new form data, redirect to view page ... @bp.get("//delete") -@requires_login +@auth.requires_login def delete(jamid): # Delete a jam, redirect to the jams list ... @@ -56,7 +59,7 @@ def events(jamid): ... @bp.get("//events/create") -@requires_login +@auth.requires_login def events_create(): # Create a new event and redirect to the edit form ... @@ -67,14 +70,68 @@ def events_view(eventid): ... @bp.post("//events//update") -@requires_login +@auth.requires_login def events_update(jamid): # Update an event with the new form data ... @bp.get("//events//delete") -@requires_login +@auth.requires_login def events_delete(jamid): # Delete an event, redirect to list of all events ... +@dataclass +class Jam: + jamid: int + # TODO: User colors? + # TODO: User object? + ownerid: int + ownername: str + created: datetime + title: str + description: str + events: list + + @classmethod + def from_row(cls, row): + event_rows = db.query("SELECT * FROM jam_events WHERE jamid = ?", [row["jamid"]]) + events = [JamEvent.from_row(r) for r in event_rows] + return cls( + jamid=row["jamid"], + title=row["title"], + description=row["description"], # TODO: Sanitize + ownerid=row["userid"], + ownername=row["username"], + created=datetime.fromisoformat(row["created"]), + events=events, + ) + +@dataclass +class JamEvent: + eventid: int + jamid: int + threadid: int + created: datetime + title: str + startdate: datetime + enddate: datetime + description: str + # TODO: Comment object? + comments: list + + @classmethod + def from_row(cls, row): + comments = db.query("SELECT * FROM comments WHERE threadid = ?", [row["threadid"]]) + return cls( + eventid=row["eventid"], + jamid=row["jamid"], + threadid=row["threadid"], + created=datetime.fromisoformat(row["created"]), + title=row["title"], + startdate=datetime.fromisoformat(row["startdate"]), + enddate=datetime.fromisoformat(row["enddate"]), + description=row["description"], # TODO: Sanitize + # TODO: Comment object? + comments=comments, + ) diff --git a/src/littlesongplace/templates/jam.html b/src/littlesongplace/templates/jam.html index 9b1a0c8..f6f5fe3 100644 --- a/src/littlesongplace/templates/jam.html +++ b/src/littlesongplace/templates/jam.html @@ -1,30 +1,23 @@ {% extends "base.html" %} -{% block title %}Jam{% endblock %} +{% block title %}{{ jam.title }}{% endblock %} {% block body %} -

Jam Title: Event Title

+

{{ jam.title }}

-
- Deadline: 2000-00-00 -
- -

Description

-Event Description. - -Lorem ipsum dolor sit amet... + Posted By: {{ jam.userid }}
-
- + Deadline: {{ jam.enddate }}
+

Description

-

Submitted Songs

-

-(Shown only after jam deadline) +

+{{ jam.description }} +
{% endblock %} -- 2.39.5