handle DB errors
This commit is contained in:
19
publisher.py
19
publisher.py
@@ -2,7 +2,8 @@ from wtforms import SubmitField, StringField, HiddenField, SelectField, validato
|
|||||||
from flask import request, render_template, redirect
|
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 Sequence
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from status import st, Status
|
from status import st, Status
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
@@ -46,14 +47,20 @@ def publishers():
|
|||||||
@app.route("/publisher", methods=["GET", "POST"])
|
@app.route("/publisher", methods=["GET", "POST"])
|
||||||
def new_publisher():
|
def new_publisher():
|
||||||
form = PublisherForm(request.form)
|
form = PublisherForm(request.form)
|
||||||
|
page_title='Create new Publisher'
|
||||||
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 Publisher' )
|
return render_template("edit_id_name.html", form=form, page_title=page_title )
|
||||||
else:
|
else:
|
||||||
publisher = Publisher( name=request.form["name"] )
|
publisher = Publisher( name=request.form["name"] )
|
||||||
|
try:
|
||||||
db.session.add(publisher)
|
db.session.add(publisher)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
st.SetMessage( "Created new Publisher (id={})".format(publisher.id) )
|
st.SetMessage( "Created new Publisher (id={})".format(publisher.id) )
|
||||||
return redirect( '/publishers' )
|
return redirect( '/publishers' )
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
st.SetAlert( "danger" )
|
||||||
|
st.SetMessage( "<b>Failed to add Publisher:</b> {}".format( e.orig) )
|
||||||
|
return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# /publisher/<id> -> GET/POST(save or delete) -> shows/edits/delets a single publisher
|
# /publisher/<id> -> GET/POST(save or delete) -> shows/edits/delets a single publisher
|
||||||
@@ -62,7 +69,9 @@ def new_publisher():
|
|||||||
def publisher(id):
|
def publisher(id):
|
||||||
### DDP: should this be request.form or request.values?
|
### DDP: should this be request.form or request.values?
|
||||||
form = PublisherForm(request.form)
|
form = PublisherForm(request.form)
|
||||||
|
page_title='Edit Publisher'
|
||||||
if request.method == 'POST' and form.validate():
|
if request.method == 'POST' and form.validate():
|
||||||
|
try:
|
||||||
publisher = Publisher.query.get(id)
|
publisher = Publisher.query.get(id)
|
||||||
if 'delete' in request.form:
|
if 'delete' in request.form:
|
||||||
st.SetMessage("Successfully deleted (id={}, name={})".format( publisher.id, publisher.name ) )
|
st.SetMessage("Successfully deleted (id={}, name={})".format( publisher.id, publisher.name ) )
|
||||||
@@ -72,10 +81,14 @@ def publisher(id):
|
|||||||
publisher.name = request.form['name']
|
publisher.name = request.form['name']
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect( '/publishers' )
|
return redirect( '/publishers' )
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
st.SetAlert( "danger" )
|
||||||
|
st.SetMessage( "<b>Failed to modify Publisher:</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:
|
||||||
publisher = Publisher.query.get(id)
|
publisher = Publisher.query.get(id)
|
||||||
form = PublisherForm(request.values, obj=publisher)
|
form = PublisherForm(request.values, obj=publisher)
|
||||||
return render_template("edit_id_name.html", form=form, page_title='Edit Publisher' )
|
return render_template("edit_id_name.html", form=form, page_title=page_title )
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# Gets the Publisher matching id from DB, helper func in jinja2 code to show books
|
# Gets the Publisher matching id from DB, helper func in jinja2 code to show books
|
||||||
|
|||||||
Reference in New Issue
Block a user