improved DB error handling
This commit is contained in:
12
condition.py
12
condition.py
@@ -47,8 +47,9 @@ def conditions():
|
|||||||
@app.route("/condition", methods=["GET", "POST"])
|
@app.route("/condition", methods=["GET", "POST"])
|
||||||
def new_condition():
|
def new_condition():
|
||||||
form = ConditionForm(request.form)
|
form = ConditionForm(request.form)
|
||||||
|
page_title='Create new Condition'
|
||||||
if 'name' not in request.form:
|
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:
|
else:
|
||||||
condition = Condition( name=request.form["name"] )
|
condition = Condition( name=request.form["name"] )
|
||||||
try:
|
try:
|
||||||
@@ -59,7 +60,7 @@ def new_condition():
|
|||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
st.SetAlert( "danger" )
|
st.SetAlert( "danger" )
|
||||||
st.SetMessage( "<b>Failed to add condition:</b> {}".format( e.orig) )
|
st.SetMessage( "<b>Failed to add condition:</b> {}".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
|
# /condition/<id> -> GET/POST(save or delete) -> shows/edits/delets a single condition
|
||||||
@@ -68,6 +69,7 @@ def new_condition():
|
|||||||
def condition(id):
|
def condition(id):
|
||||||
### DDP: should this be request.form or request.values?
|
### DDP: should this be request.form or request.values?
|
||||||
form = ConditionForm(request.form)
|
form = ConditionForm(request.form)
|
||||||
|
page_title='Edit Condition'
|
||||||
if request.method == 'POST' and form.validate():
|
if request.method == 'POST' and form.validate():
|
||||||
try:
|
try:
|
||||||
condition = Condition.query.get(id)
|
condition = Condition.query.get(id)
|
||||||
@@ -81,12 +83,12 @@ def condition(id):
|
|||||||
return redirect( '/conditions' )
|
return redirect( '/conditions' )
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
st.SetAlert( "danger" )
|
st.SetAlert( "danger" )
|
||||||
st.SetMessage( "<b>Failed to modity condition:</b> {}".format(e.orig) )
|
st.SetMessage( "<b>Failed to modify Condition:</b> {}".format(e.orig) )
|
||||||
return render_template("edit_id_name.html", form=form, page_title='Edit 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() )
|
||||||
else:
|
else:
|
||||||
condition = Condition.query.get(id)
|
condition = Condition.query.get(id)
|
||||||
form = ConditionForm(request.values, obj=condition)
|
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
|
# Gets the Condition matching id from DB, helper func in jinja2 code to show books
|
||||||
|
|||||||
44
covertype.py
44
covertype.py
@@ -3,6 +3,7 @@ from flask import request, render_template, redirect
|
|||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from __main__ import db, app, ma
|
from __main__ import db, app, ma
|
||||||
from sqlalchemy import func, Sequence
|
from sqlalchemy import func, Sequence
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from status import st, Status
|
from status import st, Status
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
@@ -46,14 +47,21 @@ def covertypes():
|
|||||||
@app.route("/covertype", methods=["GET", "POST"])
|
@app.route("/covertype", methods=["GET", "POST"])
|
||||||
def new_covertype():
|
def new_covertype():
|
||||||
form = CovertypeForm(request.form)
|
form = CovertypeForm(request.form)
|
||||||
|
page_title='Create new Covertype'
|
||||||
if 'name' not in request.form:
|
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:
|
else:
|
||||||
covertype = Covertype( name=request.form["name"] )
|
covertype = Covertype( name=request.form["name"] )
|
||||||
db.session.add(covertype)
|
try:
|
||||||
db.session.commit()
|
db.session.add(covertype)
|
||||||
st.SetMessage( "Created new Covertype (id={})".format(covertype.id) )
|
db.session.commit()
|
||||||
return redirect( '/covertypes' )
|
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> {}".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
|
# /covertype/<id> -> GET/POST(save or delete) -> shows/edits/delets a single covertype
|
||||||
@@ -62,20 +70,26 @@ def new_covertype():
|
|||||||
def covertype(id):
|
def covertype(id):
|
||||||
### DDP: should this be request.form or request.values?
|
### DDP: should this be request.form or request.values?
|
||||||
form = CovertypeForm(request.form)
|
form = CovertypeForm(request.form)
|
||||||
|
page_title='Edit Covertype'
|
||||||
if request.method == 'POST' and form.validate():
|
if request.method == 'POST' and form.validate():
|
||||||
covertype = Covertype.query.get(id)
|
try:
|
||||||
if 'delete' in request.form:
|
covertype = Covertype.query.get(id)
|
||||||
st.SetMessage("Successfully deleted (id={}, name={})".format( covertype.id, covertype.name ) )
|
if 'delete' in request.form:
|
||||||
covertype = Covertype.query.filter(Covertype.id==id).delete()
|
st.SetMessage("Successfully deleted (id={}, name={})".format( covertype.id, covertype.name ) )
|
||||||
if 'submit' in request.form:
|
covertype = Covertype.query.filter(Covertype.id==id).delete()
|
||||||
st.SetMessage("Successfully Updated Covertype (id={})".format(id) )
|
if 'submit' in request.form:
|
||||||
covertype.name = request.form['name']
|
st.SetMessage("Successfully Updated Covertype (id={})".format(id) )
|
||||||
db.session.commit()
|
covertype.name = request.form['name']
|
||||||
return redirect( '/covertypes' )
|
db.session.commit()
|
||||||
|
return redirect( '/covertypes' )
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
st.SetAlert( "danger" )
|
||||||
|
st.SetMessage( "<b>Failed to modify Covertype:</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:
|
||||||
covertype = Covertype.query.get(id)
|
covertype = Covertype.query.get(id)
|
||||||
form = CovertypeForm(request.values, obj=covertype)
|
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
|
# Gets the Covertype matching id from DB, helper func in jinja2 code to show books
|
||||||
|
|||||||
12
genre.py
12
genre.py
@@ -47,8 +47,9 @@ def genres():
|
|||||||
@app.route("/genre", methods=["GET", "POST"])
|
@app.route("/genre", methods=["GET", "POST"])
|
||||||
def new_genre():
|
def new_genre():
|
||||||
form = GenreForm(request.form)
|
form = GenreForm(request.form)
|
||||||
|
page_title='Create new Genre'
|
||||||
if 'name' not in request.form:
|
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:
|
else:
|
||||||
genre = Genre( name=request.form["name"] )
|
genre = Genre( name=request.form["name"] )
|
||||||
try:
|
try:
|
||||||
@@ -59,7 +60,7 @@ def new_genre():
|
|||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
st.SetAlert( "danger" )
|
st.SetAlert( "danger" )
|
||||||
st.SetMessage( "<b>Failed to add Genre:</b> {}".format( e.orig) )
|
st.SetMessage( "<b>Failed to add Genre:</b> {}".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
|
# /genre/<id> -> GET/POST(save or delete) -> shows/edits/delets a single genre
|
||||||
@@ -68,6 +69,7 @@ def new_genre():
|
|||||||
def genre(id):
|
def genre(id):
|
||||||
### DDP: should this be request.form or request.values?
|
### DDP: should this be request.form or request.values?
|
||||||
form = GenreForm(request.form)
|
form = GenreForm(request.form)
|
||||||
|
page_title='Edit Genre'
|
||||||
if request.method == 'POST' and form.validate():
|
if request.method == 'POST' and form.validate():
|
||||||
try:
|
try:
|
||||||
genre = Genre.query.get(id)
|
genre = Genre.query.get(id)
|
||||||
@@ -81,12 +83,12 @@ def genre(id):
|
|||||||
return redirect( '/genres' )
|
return redirect( '/genres' )
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
st.SetAlert( "danger" )
|
st.SetAlert( "danger" )
|
||||||
st.SetMessage( "<b>Failed to modity Genre:</b> {}".format(e.orig) )
|
st.SetMessage( "<b>Failed to modify Genre:</b> {}".format(e.orig) )
|
||||||
return render_template("edit_id_name.html", form=form, page_title='Edit 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() )
|
||||||
else:
|
else:
|
||||||
genre = Genre.query.get(id)
|
genre = Genre.query.get(id)
|
||||||
form = GenreForm(request.values, obj=genre)
|
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
|
# Gets the Genre matching id from DB, helper func in jinja2 code to show books
|
||||||
|
|||||||
40
owned.py
40
owned.py
@@ -3,6 +3,7 @@ from flask import request, render_template, redirect
|
|||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from __main__ import db, app, ma
|
from __main__ import db, app, ma
|
||||||
from sqlalchemy import func, Sequence
|
from sqlalchemy import func, Sequence
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from status import st, Status
|
from status import st, Status
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
@@ -46,14 +47,20 @@ def owneds():
|
|||||||
@app.route("/owned", methods=["GET", "POST"])
|
@app.route("/owned", methods=["GET", "POST"])
|
||||||
def new_owned():
|
def new_owned():
|
||||||
form = OwnedForm(request.form)
|
form = OwnedForm(request.form)
|
||||||
|
page_title='Create new Ownership Type'
|
||||||
if 'name' not in request.form:
|
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:
|
else:
|
||||||
owned = Owned( name=request.form["name"] )
|
owned = Owned( name=request.form["name"] )
|
||||||
db.session.add(owned)
|
try:
|
||||||
db.session.commit()
|
db.session.add(owned)
|
||||||
st.SetMessage( "Created new Owned Type (id={})".format(owned.id) )
|
db.session.commit()
|
||||||
return redirect( '/owneds' )
|
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> {}".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
|
# /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?
|
### DDP: should this be request.form or request.values?
|
||||||
form = OwnedForm(request.form)
|
form = OwnedForm(request.form)
|
||||||
if request.method == 'POST' and form.validate():
|
if request.method == 'POST' and form.validate():
|
||||||
owned = Owned.query.get(id)
|
try:
|
||||||
if 'delete' in request.form:
|
owned = Owned.query.get(id)
|
||||||
st.SetMessage("Successfully deleted (id={}, name={})".format( owned.id, owned.name ) )
|
if 'delete' in request.form:
|
||||||
owned = Owned.query.filter(Owned.id==id).delete()
|
st.SetMessage("Successfully deleted (id={}, name={})".format( owned.id, owned.name ) )
|
||||||
if 'submit' in request.form:
|
owned = Owned.query.filter(Owned.id==id).delete()
|
||||||
owned.name = request.form['name']
|
if 'submit' in request.form:
|
||||||
st.SetMessage("Successfully Updated Owned (id={})".format(id) )
|
owned.name = request.form['name']
|
||||||
db.session.commit()
|
st.SetMessage("Successfully Updated Owned (id={})".format(id) )
|
||||||
return redirect( '/owneds' )
|
db.session.commit()
|
||||||
|
return redirect( '/owneds' )
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
st.SetAlert( "danger" )
|
||||||
|
st.SetMessage( "<b>Failed to modify Ownership Type:</b> {}".format(e.orig) )
|
||||||
|
return render_template("edit_id_name.html", form=form, page_title='Edit Ownership Type', alert=st.GetAlert(), message=st.GetMessage() )
|
||||||
else:
|
else:
|
||||||
obj = Owned.query.get(id)
|
obj = Owned.query.get(id)
|
||||||
form = OwnedForm(request.values, obj=obj)
|
form = OwnedForm(request.values, obj=obj)
|
||||||
|
|||||||
12
rating.py
12
rating.py
@@ -47,8 +47,9 @@ def ratings():
|
|||||||
@app.route("/rating", methods=["GET", "POST"])
|
@app.route("/rating", methods=["GET", "POST"])
|
||||||
def new_rating():
|
def new_rating():
|
||||||
form = RatingForm(request.form)
|
form = RatingForm(request.form)
|
||||||
|
page_title='Create new Rating'
|
||||||
if 'name' not in request.form:
|
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:
|
else:
|
||||||
rating = Rating( name=request.form["name"] )
|
rating = Rating( name=request.form["name"] )
|
||||||
try:
|
try:
|
||||||
@@ -59,7 +60,7 @@ def new_rating():
|
|||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
st.SetAlert( "danger" )
|
st.SetAlert( "danger" )
|
||||||
st.SetMessage( "<b>Failed to add rating:</b> {}".format( e.orig) )
|
st.SetMessage( "<b>Failed to add rating:</b> {}".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
|
# /rating/<id> -> GET/POST(save or delete) -> shows/edits/delets a single rating
|
||||||
@@ -68,6 +69,7 @@ def new_rating():
|
|||||||
def rating(id):
|
def rating(id):
|
||||||
### DDP: should this be request.form or request.values?
|
### DDP: should this be request.form or request.values?
|
||||||
form = RatingForm(request.form)
|
form = RatingForm(request.form)
|
||||||
|
page_title='Edit Rating'
|
||||||
if request.method == 'POST' and form.validate():
|
if request.method == 'POST' and form.validate():
|
||||||
try:
|
try:
|
||||||
rating = Rating.query.get(id)
|
rating = Rating.query.get(id)
|
||||||
@@ -81,12 +83,12 @@ def rating(id):
|
|||||||
return redirect( '/ratings' )
|
return redirect( '/ratings' )
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
st.SetAlert( "danger" )
|
st.SetAlert( "danger" )
|
||||||
st.SetMessage( "<b>Failed to modity rating:</b> {}".format(e.orig) )
|
st.SetMessage( "<b>Failed to modify Rating:</b> {}".format(e.orig) )
|
||||||
return render_template("edit_id_name.html", form=form, page_title='Edit 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() )
|
||||||
else:
|
else:
|
||||||
rating = Rating.query.get(id)
|
rating = Rating.query.get(id)
|
||||||
form = RatingForm(request.values, obj=rating)
|
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
|
# Gets the Rating matching id from DB, helper func in jinja2 code to show books
|
||||||
|
|||||||
Reference in New Issue
Block a user