diff --git a/app.py b/app.py index 0a9d0cb..a42eac6 100644 --- a/app.py +++ b/app.py @@ -2,7 +2,7 @@ import functools import logging import os -from flask import Flask, g, render_template, request, redirect, session +from flask import Flask, g, render_template, request, redirect, session, url_for from flask_bootstrap import Bootstrap import db @@ -104,34 +104,55 @@ def season_list(): ("end", "Ended At"), ], } - return render_template("seasons.html", model=model) + return render_template("season_list.html", model=model) @app.route("/seasons/new", methods=["GET"]) @authorize def season_new(): - return render_template("editseason.html", model={}) + form = forms.SeasonForm() + model = models.GenericFormModel( + page_title="New Season", + form_title="Create New Season", + post_url="/seasons/edit/null", + ) + return render_template("generic_form.html", model=model, form=form) -@app.route("/seasons//edit") +@app.route("/seasons/edit/", methods=["GET", "POST"]) @authorize -def season_edit(id: int): - sql, args = db.load_season(id) - loaded = db.query_db(sql, args, one=True, cls=models.Season) - return render_template("editseason.html", model=loaded) +def season_edit(season_id: int): + model = models.GenericFormModel( + page_title="Seasons", + form_title="Edit Season", + post_url=f"/seasons/edit/{season_id}", + ) + if request.method == "GET": + sql, args = db.load_season(season_id) + season: models.Season = db.query_db(sql, args, one=True, cls=models.Season) -@app.route("/seasons/save", methods=["POST"]) -@authorize -def season_save(): - try: - season = models.Season.from_form(request.form) - except AttributeError as err: - print(err) - return render_template("editseason.html", model={}) - sql, args = db.save_season_query(season) - res = db.update_db(sql, args) - return redirect("/seasons") + form = forms.SeasonForm() + form.season_id.data = season.id + form.code.data = season.code + form.game_name.data = season.game + form.description.data = season.description + form.start.data = season.start + form.end.data = season.end + + model.form_title = f"Edit Season '{season.code}: {season.game}'" + return render_template("generic_form.html", model=model, form=form) + else: + form = forms.SeasonForm() + + if not form.validate_on_submit(): + model.errors = form.errors + return render_template("generic_form.html", model=model, form=form) + + season = models.Season.from_form(form) + sql, args = db.save_season_query(season) + errors = db.update_db(sql, args) + return redirect(url_for("season_list")) @app.route("/seasons/", methods=["GET"]) @@ -341,9 +362,6 @@ def enemy_edit(enemy_id: int): ) if request.method == "GET": - sql, args = db.load_season() - seasons = db.query_db(sql, args, cls=models.Season) - sql, args = db.load_enemies(enemy_id) enemy = db.query_db(sql, args, one=True, cls=models.Enemy) diff --git a/forms.py b/forms.py index c1be2de..4748b54 100644 --- a/forms.py +++ b/forms.py @@ -8,11 +8,21 @@ from wtforms import ( DecimalField, SelectField, ) -from wtforms.validators import DataRequired +from wtforms.validators import DataRequired, Optional import choices +class SeasonForm(FlaskForm): + season_id = StringField("Season ID", render_kw={"readonly": True}) + code = StringField("Season Code", validators=[DataRequired()]) + game_name = StringField("Game Name", validators=[DataRequired()]) + description = StringField("Season Description") + start = DateField("Season Start", format="%Y-%m-%d", validators=[DataRequired()]) + end = DateField("Season End", format="%Y-%m-%d", validators=[Optional()]) + submit_button = SubmitField("Submit") + + class EpisodeForm(FlaskForm): season_id = StringField("Season ID", render_kw={"readonly": True}) episode_id = StringField("Episode ID", render_kw={"readonly": True}) diff --git a/models.py b/models.py index bb9aa10..7bdfc7d 100644 --- a/models.py +++ b/models.py @@ -1,4 +1,5 @@ import datetime +from typing import Dict, List from dataclasses import dataclass import forms @@ -11,6 +12,7 @@ class GenericFormModel: page_title: str form_title: str post_url: str + errors: Dict[str, List[str]] = None @dataclass @@ -78,34 +80,24 @@ class Season: end: datetime.date code: str - @classmethod - def from_form(cls, form): - id = form.get("id", None) - id = int(id) if id else None - - game = form.get("game", None) - if not game: - raise AttributeError(INVALID_STR.format("game")) - game = str(game) - - description = form.get("description", None) - - start = form.get("start", None) + def __post_init__(self): try: - start = datetime.date.fromisoformat(start) + self.start = datetime.datetime.strptime(self.start, "%Y-%m-%d").date() except Exception: - raise AttributeError(INVALID_STR.format("start")) + pass + try: + self.end = datetime.datetime.strptime(self.end, "%Y-%m-%d").date() + except Exception: + pass - end = form.get("end", None) - if end: - try: - end = datetime.date.fromisoformat(end) - except Exception: - raise AttributeError(INVALID_STR.format("end")) + @classmethod + def from_form(cls, form: forms.SeasonForm): + season_id = int(form.season_id.data) if form.season_id.data else None + code = str(form.code.data) + game = str(form.game_name.data) + description = str(form.description.data) if form.description.data else None + start = form.start.data + end = form.end.data - code = form.get("code", None) - if not code: - raise AttributeError(INVALID_STR.format("code")) - - self = cls(id, game, description, start, end, code) + self = cls(season_id, game, description, start, end, code) return self diff --git a/templates/generic_form.html b/templates/generic_form.html index 005f34b..4d2b981 100644 --- a/templates/generic_form.html +++ b/templates/generic_form.html @@ -6,6 +6,18 @@ {% block page %}

{{ model.form_title }}

+ + {% if model.errors %} +
+ {% for field, errors in model.errors.items() %} +
+ {{ field }}: + {{ errors|join(', ') }} +
+ {% endfor %} +
+ {% endif %} +
{{ wtf.quick_form(form, button_map={'submit_button': 'primary'}, form_type='horizontal', horizontal_columns=('lg', 2, 10)) }}
diff --git a/templates/seasons.html b/templates/season_list.html similarity index 81% rename from templates/seasons.html rename to templates/season_list.html index edcc0ce..06ee6c3 100644 --- a/templates/seasons.html +++ b/templates/season_list.html @@ -5,7 +5,7 @@ {% block page %} {% if g.is_editor %} {% endif %} {% if not model.seasons %} @@ -40,9 +40,11 @@ {% if g.is_editor %} - + + + - + Episode