diff --git a/choices.py b/choices.py index 818aab8..972e669 100644 --- a/choices.py +++ b/choices.py @@ -13,6 +13,15 @@ def season_choices(): return choices +def player_choice(): + """ + Query database for a list of available players to bind them to a select box + """ + sql, args = db.load_players() + players = db.query_db(sql, args, cls=models.Player) + return [(p.id, p.name) for p in players] + + class SeasonChoicesIterable: """ This is used to declare choices for WTForms SelectFields at class definition time. @@ -22,3 +31,14 @@ class SeasonChoicesIterable: def __iter__(self): for choice in season_choices(): yield choice + + +class PlayerChoiceIterable: + """ + This is used to declare choices for WTForms SelectFields at class definition time. + Be aware that this uses an undocumented WTForms feature and is not guaranteed to work. + """ + + def __iter__(self): + for choice in player_choice(): + yield choice diff --git a/db.py b/db.py index b5ee1cc..e1d1ba1 100644 --- a/db.py +++ b/db.py @@ -1,5 +1,7 @@ import sqlite3 import logging as log +from typing import Sequence + from flask import g import models @@ -187,6 +189,12 @@ def load_episodes(season_id: int = None): return sql, args +def save_episode_players(episode_id: int, player_ids: Sequence[int]): + sql = "insert into episode_player values (?, ?)" + args = [(episode_id, i) for i in player_ids] + return sql, args + + def save_episode(episode: models.Episode): if not episode.id: sql = "insert into episode values (?, ?, ?, ?, ?, ?, ?)" diff --git a/forms.py b/forms.py index 4748b54..d9c62e4 100644 --- a/forms.py +++ b/forms.py @@ -7,6 +7,7 @@ from wtforms import ( BooleanField, DecimalField, SelectField, + SelectMultipleField, ) from wtforms.validators import DataRequired, Optional @@ -28,10 +29,12 @@ class EpisodeForm(FlaskForm): episode_id = StringField("Episode ID", render_kw={"readonly": True}) code = StringField("Episode Code", validators=[DataRequired()]) title = StringField("Title", validators=[DataRequired()]) - description = StringField("Description", validators=[]) date = DateField("Episode Date", format="%Y-%m-%d", validators=[DataRequired()]) start = TimeField("Start Time", format="%H:%M", validators=[DataRequired()]) end = TimeField("End Time", format="%H:%M", validators=[DataRequired()]) + players = SelectMultipleField( + "Players", coerce=int, choices=choices.PlayerChoiceIterable() + ) submit_button = SubmitField("Submit") diff --git a/schema.sql b/schema.sql index 2840196..f671489 100644 --- a/schema.sql +++ b/schema.sql @@ -68,37 +68,21 @@ create table if not exists episode references season, title text not null, date text not null, - start text not null, - end text not null + start timestamp not null, + end timestamp not null, + code text not null default 'EXX' ); create unique index if not exists episode_id_uindex on episode (id); - -alter table episode - add code text not null default 'EXX'; - - -create table episode_dg_tmp +create table if not exists episode_player ( - id integer not null - constraint episode_pk - primary key autoincrement, - season_id integer not null - references season, - title text not null, - date text not null, - start timestamp not null, - end timestamp not null, - code text default 'EXX' not null + episode_id integer not null + constraint episode_player_episode_id_fk + references episode, + player_id integer not null + constraint episode_player_player_id_fk + references player ); -insert into episode_dg_tmp(id, season_id, title, date, start, end, code) select id, season_id, title, date, start, end, code from episode; - -drop table episode; - -alter table episode_dg_tmp rename to episode; - -create unique index episode_id_uindex - on episode (id);