From 0ae4019a1a2ba5f51dabe57e051d9568ecbd315f Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Thu, 31 Dec 2020 11:31:15 +1100 Subject: [PATCH] handle DB errors --- author.py | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/author.py b/author.py index f694109..85e7f2b 100644 --- a/author.py +++ b/author.py @@ -2,6 +2,8 @@ from wtforms import SubmitField, StringField, HiddenField, validators, Form from flask_wtf import FlaskForm from flask import request, render_template, redirect from __main__ import db, app, ma +from sqlalchemy import Sequence +from sqlalchemy.exc import SQLAlchemyError from status import st, Status ################################################################################ @@ -50,15 +52,20 @@ def authors(): @app.route("/author", methods=["GET", "POST"]) def new_author(): form = AuthorForm(request.form) + page_title='Create new Author' if 'surname' not in request.form: - return render_template("edit_id_name.html", form=form, page_title='Create new Author' ) + return render_template("edit_id_name.html", form=form, page_title=page_title ) else: author = Author( surname=request.form["surname"], firstnames=request.form["firstnames"] ) - db.session.add(author) - db.session.commit() - st.SetMessage( "Created new Author ({})".format(author) ) - return redirect( '/authors' ) - + try: + db.session.add(author) + db.session.commit() + st.SetMessage( "Created new Author ({})".format(author) ) + return redirect( '/authors' ) + except SQLAlchemyError as e: + st.SetAlert( "danger" ) + st.SetMessage( "Failed to add Author: {}".format( e.orig) ) + return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() ) ################################################################################ # /author/ -> GET/POST(save or delete) -> shows/edits/delets a single author @@ -67,22 +74,28 @@ def new_author(): def author(id): ### DDP: should this be request.form or request.values? form = AuthorForm(request.form) + page_title='Edit Author' if request.method == 'POST' and form.validate(): - author = Author.query.get(id) - if 'delete' in request.form: - st.SetMessage("Successfully deleted Author: ({})".format( author ) ) - author = Author.query.filter(Author.id==id).delete() - if 'submit' in request.form: - st.SetMessage("Successfully Updated Author: (From: {}".format(author) ) - author.surname = request.form['surname'] - author.firstnames = request.form['firstnames'] - st.AppendMessage(" To: {}".format(author) ) - db.session.commit() - return redirect( '/authors' ) + try: + author = Author.query.get(id) + if 'delete' in request.form: + st.SetMessage("Successfully deleted Author: ({})".format( author ) ) + author = Author.query.filter(Author.id==id).delete() + if 'submit' in request.form: + st.SetMessage("Successfully Updated Author: (From: {}".format(author) ) + author.surname = request.form['surname'] + author.firstnames = request.form['firstnames'] + st.AppendMessage(" To: {}".format(author) ) + db.session.commit() + return redirect( '/authors' ) + except SQLAlchemyError as e: + st.SetAlert( "danger" ) + st.SetMessage( "Failed to modify Author: {}".format(e.orig) ) + return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() ) else: author = Author.query.get(id) form = AuthorForm(request.values, obj=author) - return render_template("edit_id_name.html", object=author, form=form) + return render_template("edit_id_name.html", object=author, form=form, page_title = page_title, alert=st.GetAlert(), message=st.GetMessage() ) ################################################################################ # helper fund to GetAuthors -> author_list -> jinja2 for author drop-down in book.html