handle DB errors

This commit is contained in:
2020-12-31 11:31:15 +11:00
parent 6d9ec1336f
commit 0ae4019a1a

View File

@@ -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( "<b>Failed to add Author:</b>&nbsp;{}".format( e.orig) )
return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
################################################################################
# /author/<id> -> 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( "<b>Failed to modify Author:</b>&nbsp;{}".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