improved DB error handling

This commit is contained in:
2020-12-30 23:12:36 +11:00
parent 60b7484afb
commit af6c76d136
5 changed files with 76 additions and 44 deletions

View File

@@ -47,8 +47,9 @@ def conditions():
@app.route("/condition", methods=["GET", "POST"])
def new_condition():
form = ConditionForm(request.form)
page_title='Create new Condition'
if 'name' not in request.form:
return render_template("edit_id_name.html", form=form, page_title='Create new Condition' )
return render_template("edit_id_name.html", form=form, page_title=page_title )
else:
condition = Condition( name=request.form["name"] )
try:
@@ -59,7 +60,7 @@ def new_condition():
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to add condition:</b>&nbsp;{}".format( e.orig) )
return render_template("edit_id_name.html", form=form, page_title='Create new Condition', alert=st.GetAlert(), message=st.GetMessage() )
return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
################################################################################
# /condition/<id> -> GET/POST(save or delete) -> shows/edits/delets a single condition
@@ -68,6 +69,7 @@ def new_condition():
def condition(id):
### DDP: should this be request.form or request.values?
form = ConditionForm(request.form)
page_title='Edit Condition'
if request.method == 'POST' and form.validate():
try:
condition = Condition.query.get(id)
@@ -81,12 +83,12 @@ def condition(id):
return redirect( '/conditions' )
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to modity condition:</b>&nbsp;{}".format(e.orig) )
return render_template("edit_id_name.html", form=form, page_title='Edit Condition', alert=st.GetAlert(), message=st.GetMessage() )
st.SetMessage( "<b>Failed to modify Condition:</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:
condition = Condition.query.get(id)
form = ConditionForm(request.values, obj=condition)
return render_template("edit_id_name.html", form=form, page_title='Edit Condition' )
return render_template("edit_id_name.html", form=form, page_title=page_title )
################################################################################
# Gets the Condition matching id from DB, helper func in jinja2 code to show books

View File

@@ -3,6 +3,7 @@ from flask import request, render_template, redirect
from flask_wtf import FlaskForm
from __main__ import db, app, ma
from sqlalchemy import func, Sequence
from sqlalchemy.exc import SQLAlchemyError
from status import st, Status
################################################################################
@@ -46,14 +47,21 @@ def covertypes():
@app.route("/covertype", methods=["GET", "POST"])
def new_covertype():
form = CovertypeForm(request.form)
page_title='Create new Covertype'
if 'name' not in request.form:
return render_template("edit_id_name.html", form=form, page_title='Create new Covertype' )
return render_template("edit_id_name.html", form=form, page_title=page_title )
else:
covertype = Covertype( name=request.form["name"] )
db.session.add(covertype)
db.session.commit()
st.SetMessage( "Created new Covertype (id={})".format(covertype.id) )
return redirect( '/covertypes' )
try:
db.session.add(covertype)
db.session.commit()
st.SetMessage( "Created new Covertype (id={})".format(covertype.id) )
return redirect( '/covertypes' )
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to add covertype:</b>&nbsp;{}".format( e.orig) )
return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
################################################################################
# /covertype/<id> -> GET/POST(save or delete) -> shows/edits/delets a single covertype
@@ -62,20 +70,26 @@ def new_covertype():
def covertype(id):
### DDP: should this be request.form or request.values?
form = CovertypeForm(request.form)
page_title='Edit Covertype'
if request.method == 'POST' and form.validate():
covertype = Covertype.query.get(id)
if 'delete' in request.form:
st.SetMessage("Successfully deleted (id={}, name={})".format( covertype.id, covertype.name ) )
covertype = Covertype.query.filter(Covertype.id==id).delete()
if 'submit' in request.form:
st.SetMessage("Successfully Updated Covertype (id={})".format(id) )
covertype.name = request.form['name']
db.session.commit()
return redirect( '/covertypes' )
try:
covertype = Covertype.query.get(id)
if 'delete' in request.form:
st.SetMessage("Successfully deleted (id={}, name={})".format( covertype.id, covertype.name ) )
covertype = Covertype.query.filter(Covertype.id==id).delete()
if 'submit' in request.form:
st.SetMessage("Successfully Updated Covertype (id={})".format(id) )
covertype.name = request.form['name']
db.session.commit()
return redirect( '/covertypes' )
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to modify Covertype:</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:
covertype = Covertype.query.get(id)
form = CovertypeForm(request.values, obj=covertype)
return render_template("edit_id_name.html", form=form, page_title='Edit Covertype' )
return render_template("edit_id_name.html", form=form, page_title=page_title )
################################################################################
# Gets the Covertype matching id from DB, helper func in jinja2 code to show books

View File

@@ -47,8 +47,9 @@ def genres():
@app.route("/genre", methods=["GET", "POST"])
def new_genre():
form = GenreForm(request.form)
page_title='Create new Genre'
if 'name' not in request.form:
return render_template("edit_id_name.html", form=form, page_title='Create new Genre' )
return render_template("edit_id_name.html", form=form, page_title=page_title )
else:
genre = Genre( name=request.form["name"] )
try:
@@ -59,7 +60,7 @@ def new_genre():
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to add Genre:</b>&nbsp;{}".format( e.orig) )
return render_template("edit_id_name.html", form=form, page_title='Create new Genre', alert=st.GetAlert(), message=st.GetMessage() )
return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
################################################################################
# /genre/<id> -> GET/POST(save or delete) -> shows/edits/delets a single genre
@@ -68,6 +69,7 @@ def new_genre():
def genre(id):
### DDP: should this be request.form or request.values?
form = GenreForm(request.form)
page_title='Edit Genre'
if request.method == 'POST' and form.validate():
try:
genre = Genre.query.get(id)
@@ -81,12 +83,12 @@ def genre(id):
return redirect( '/genres' )
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to modity Genre:</b>&nbsp;{}".format(e.orig) )
return render_template("edit_id_name.html", form=form, page_title='Edit Genre', alert=st.GetAlert(), message=st.GetMessage() )
st.SetMessage( "<b>Failed to modify Genre:</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:
genre = Genre.query.get(id)
form = GenreForm(request.values, obj=genre)
return render_template("edit_id_name.html", form=form, page_title='Edit Genre' )
return render_template("edit_id_name.html", form=form, page_title=page_title )
################################################################################
# Gets the Genre matching id from DB, helper func in jinja2 code to show books

View File

@@ -3,6 +3,7 @@ from flask import request, render_template, redirect
from flask_wtf import FlaskForm
from __main__ import db, app, ma
from sqlalchemy import func, Sequence
from sqlalchemy.exc import SQLAlchemyError
from status import st, Status
################################################################################
@@ -46,14 +47,20 @@ def owneds():
@app.route("/owned", methods=["GET", "POST"])
def new_owned():
form = OwnedForm(request.form)
page_title='Create new Ownership Type'
if 'name' not in request.form:
return render_template("edit_id_name.html", form=form, page_title='Create new Owned Type' )
return render_template("edit_id_name.html", form=form, page_title=page_title )
else:
owned = Owned( name=request.form["name"] )
db.session.add(owned)
db.session.commit()
st.SetMessage( "Created new Owned Type (id={})".format(owned.id) )
return redirect( '/owneds' )
try:
db.session.add(owned)
db.session.commit()
st.SetMessage( "Created new Owned Type (id={})".format(owned.id) )
return redirect( '/owneds' )
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to add Ownership type:</b>&nbsp;{}".format( e.orig) )
return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
################################################################################
# /owned/<id> -> GET/POST(save or delete) -> shows/edits/delets a single owned
@@ -63,15 +70,20 @@ def owned(id):
### DDP: should this be request.form or request.values?
form = OwnedForm(request.form)
if request.method == 'POST' and form.validate():
owned = Owned.query.get(id)
if 'delete' in request.form:
st.SetMessage("Successfully deleted (id={}, name={})".format( owned.id, owned.name ) )
owned = Owned.query.filter(Owned.id==id).delete()
if 'submit' in request.form:
owned.name = request.form['name']
st.SetMessage("Successfully Updated Owned (id={})".format(id) )
db.session.commit()
return redirect( '/owneds' )
try:
owned = Owned.query.get(id)
if 'delete' in request.form:
st.SetMessage("Successfully deleted (id={}, name={})".format( owned.id, owned.name ) )
owned = Owned.query.filter(Owned.id==id).delete()
if 'submit' in request.form:
owned.name = request.form['name']
st.SetMessage("Successfully Updated Owned (id={})".format(id) )
db.session.commit()
return redirect( '/owneds' )
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to modify Ownership Type:</b>&nbsp;{}".format(e.orig) )
return render_template("edit_id_name.html", form=form, page_title='Edit Ownership Type', alert=st.GetAlert(), message=st.GetMessage() )
else:
obj = Owned.query.get(id)
form = OwnedForm(request.values, obj=obj)

View File

@@ -47,8 +47,9 @@ def ratings():
@app.route("/rating", methods=["GET", "POST"])
def new_rating():
form = RatingForm(request.form)
page_title='Create new Rating'
if 'name' not in request.form:
return render_template("edit_id_name.html", form=form, page_title='Create new Rating' )
return render_template("edit_id_name.html", form=form, page_title=page_title )
else:
rating = Rating( name=request.form["name"] )
try:
@@ -59,7 +60,7 @@ def new_rating():
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to add rating:</b>&nbsp;{}".format( e.orig) )
return render_template("edit_id_name.html", form=form, page_title='Create new Rating', alert=st.GetAlert(), message=st.GetMessage() )
return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
################################################################################
# /rating/<id> -> GET/POST(save or delete) -> shows/edits/delets a single rating
@@ -68,6 +69,7 @@ def new_rating():
def rating(id):
### DDP: should this be request.form or request.values?
form = RatingForm(request.form)
page_title='Edit Rating'
if request.method == 'POST' and form.validate():
try:
rating = Rating.query.get(id)
@@ -81,12 +83,12 @@ def rating(id):
return redirect( '/ratings' )
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to modity rating:</b>&nbsp;{}".format(e.orig) )
return render_template("edit_id_name.html", form=form, page_title='Edit Rating', alert=st.GetAlert(), message=st.GetMessage() )
st.SetMessage( "<b>Failed to modify Rating:</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:
rating = Rating.query.get(id)
form = RatingForm(request.values, obj=rating)
return render_template("edit_id_name.html", form=form, page_title='Edit Rating' )
return render_template("edit_id_name.html", form=form, page_title=page_title )
################################################################################
# Gets the Rating matching id from DB, helper func in jinja2 code to show books