handle DB errors
This commit is contained in:
19
author.py
19
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"] )
|
||||
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> {}".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,7 +74,9 @@ 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():
|
||||
try:
|
||||
author = Author.query.get(id)
|
||||
if 'delete' in request.form:
|
||||
st.SetMessage("Successfully deleted Author: ({})".format( author ) )
|
||||
@@ -79,10 +88,14 @@ def author(id):
|
||||
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> {}".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
|
||||
|
||||
Reference in New Issue
Block a user