Add player choice to episode editor.
This commit is contained in:
20
choices.py
20
choices.py
@@ -13,6 +13,15 @@ def season_choices():
|
|||||||
return 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:
|
class SeasonChoicesIterable:
|
||||||
"""
|
"""
|
||||||
This is used to declare choices for WTForms SelectFields at class definition time.
|
This is used to declare choices for WTForms SelectFields at class definition time.
|
||||||
@@ -22,3 +31,14 @@ class SeasonChoicesIterable:
|
|||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for choice in season_choices():
|
for choice in season_choices():
|
||||||
yield choice
|
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
|
||||||
|
|||||||
8
db.py
8
db.py
@@ -1,5 +1,7 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
import logging as log
|
import logging as log
|
||||||
|
from typing import Sequence
|
||||||
|
|
||||||
from flask import g
|
from flask import g
|
||||||
|
|
||||||
import models
|
import models
|
||||||
@@ -187,6 +189,12 @@ def load_episodes(season_id: int = None):
|
|||||||
return sql, args
|
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):
|
def save_episode(episode: models.Episode):
|
||||||
if not episode.id:
|
if not episode.id:
|
||||||
sql = "insert into episode values (?, ?, ?, ?, ?, ?, ?)"
|
sql = "insert into episode values (?, ?, ?, ?, ?, ?, ?)"
|
||||||
|
|||||||
5
forms.py
5
forms.py
@@ -7,6 +7,7 @@ from wtforms import (
|
|||||||
BooleanField,
|
BooleanField,
|
||||||
DecimalField,
|
DecimalField,
|
||||||
SelectField,
|
SelectField,
|
||||||
|
SelectMultipleField,
|
||||||
)
|
)
|
||||||
from wtforms.validators import DataRequired, Optional
|
from wtforms.validators import DataRequired, Optional
|
||||||
|
|
||||||
@@ -28,10 +29,12 @@ class EpisodeForm(FlaskForm):
|
|||||||
episode_id = StringField("Episode ID", render_kw={"readonly": True})
|
episode_id = StringField("Episode ID", render_kw={"readonly": True})
|
||||||
code = StringField("Episode Code", validators=[DataRequired()])
|
code = StringField("Episode Code", validators=[DataRequired()])
|
||||||
title = StringField("Title", validators=[DataRequired()])
|
title = StringField("Title", validators=[DataRequired()])
|
||||||
description = StringField("Description", validators=[])
|
|
||||||
date = DateField("Episode Date", format="%Y-%m-%d", validators=[DataRequired()])
|
date = DateField("Episode Date", format="%Y-%m-%d", validators=[DataRequired()])
|
||||||
start = TimeField("Start Time", format="%H:%M", validators=[DataRequired()])
|
start = TimeField("Start Time", format="%H:%M", validators=[DataRequired()])
|
||||||
end = TimeField("End 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")
|
submit_button = SubmitField("Submit")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
36
schema.sql
36
schema.sql
@@ -68,37 +68,21 @@ create table if not exists episode
|
|||||||
references season,
|
references season,
|
||||||
title text not null,
|
title text not null,
|
||||||
date text not null,
|
date text not null,
|
||||||
start text not null,
|
start timestamp not null,
|
||||||
end text not null
|
end timestamp not null,
|
||||||
|
code text not null default 'EXX'
|
||||||
);
|
);
|
||||||
|
|
||||||
create unique index if not exists episode_id_uindex
|
create unique index if not exists episode_id_uindex
|
||||||
on episode (id);
|
on episode (id);
|
||||||
|
|
||||||
|
create table if not exists episode_player
|
||||||
alter table episode
|
|
||||||
add code text not null default 'EXX';
|
|
||||||
|
|
||||||
|
|
||||||
create table episode_dg_tmp
|
|
||||||
(
|
(
|
||||||
id integer not null
|
episode_id integer not null
|
||||||
constraint episode_pk
|
constraint episode_player_episode_id_fk
|
||||||
primary key autoincrement,
|
references episode,
|
||||||
season_id integer not null
|
player_id integer not null
|
||||||
references season,
|
constraint episode_player_player_id_fk
|
||||||
title text not null,
|
references player
|
||||||
date text not null,
|
|
||||||
start timestamp not null,
|
|
||||||
end timestamp not null,
|
|
||||||
code text default 'EXX' not null
|
|
||||||
);
|
);
|
||||||
|
|
||||||
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);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user