handle DB errors
This commit is contained in:
49
author.py
49
author.py
@@ -2,6 +2,8 @@ from wtforms import SubmitField, StringField, HiddenField, validators, Form
|
|||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from flask import request, render_template, redirect
|
from flask import request, render_template, redirect
|
||||||
from __main__ import db, app, ma
|
from __main__ import db, app, ma
|
||||||
|
from sqlalchemy import Sequence
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from status import st, Status
|
from status import st, Status
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
@@ -50,15 +52,20 @@ def authors():
|
|||||||
@app.route("/author", methods=["GET", "POST"])
|
@app.route("/author", methods=["GET", "POST"])
|
||||||
def new_author():
|
def new_author():
|
||||||
form = AuthorForm(request.form)
|
form = AuthorForm(request.form)
|
||||||
|
page_title='Create new Author'
|
||||||
if 'surname' not in request.form:
|
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:
|
else:
|
||||||
author = Author( surname=request.form["surname"], firstnames=request.form["firstnames"] )
|
author = Author( surname=request.form["surname"], firstnames=request.form["firstnames"] )
|
||||||
db.session.add(author)
|
try:
|
||||||
db.session.commit()
|
db.session.add(author)
|
||||||
st.SetMessage( "Created new Author ({})".format(author) )
|
db.session.commit()
|
||||||
return redirect( '/authors' )
|
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
|
# /author/<id> -> GET/POST(save or delete) -> shows/edits/delets a single author
|
||||||
@@ -67,22 +74,28 @@ def new_author():
|
|||||||
def author(id):
|
def author(id):
|
||||||
### DDP: should this be request.form or request.values?
|
### DDP: should this be request.form or request.values?
|
||||||
form = AuthorForm(request.form)
|
form = AuthorForm(request.form)
|
||||||
|
page_title='Edit Author'
|
||||||
if request.method == 'POST' and form.validate():
|
if request.method == 'POST' and form.validate():
|
||||||
author = Author.query.get(id)
|
try:
|
||||||
if 'delete' in request.form:
|
author = Author.query.get(id)
|
||||||
st.SetMessage("Successfully deleted Author: ({})".format( author ) )
|
if 'delete' in request.form:
|
||||||
author = Author.query.filter(Author.id==id).delete()
|
st.SetMessage("Successfully deleted Author: ({})".format( author ) )
|
||||||
if 'submit' in request.form:
|
author = Author.query.filter(Author.id==id).delete()
|
||||||
st.SetMessage("Successfully Updated Author: (From: {}".format(author) )
|
if 'submit' in request.form:
|
||||||
author.surname = request.form['surname']
|
st.SetMessage("Successfully Updated Author: (From: {}".format(author) )
|
||||||
author.firstnames = request.form['firstnames']
|
author.surname = request.form['surname']
|
||||||
st.AppendMessage(" To: {}".format(author) )
|
author.firstnames = request.form['firstnames']
|
||||||
db.session.commit()
|
st.AppendMessage(" To: {}".format(author) )
|
||||||
return redirect( '/authors' )
|
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:
|
else:
|
||||||
author = Author.query.get(id)
|
author = Author.query.get(id)
|
||||||
form = AuthorForm(request.values, obj=author)
|
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
|
# helper fund to GetAuthors -> author_list -> jinja2 for author drop-down in book.html
|
||||||
|
|||||||
Reference in New Issue
Block a user