handle DB errors

This commit is contained in:
2020-12-31 11:51:06 +11:00
parent 0ae4019a1a
commit 7e080f65c5

51
loan.py
View File

@@ -4,6 +4,8 @@ from flask import request, render_template, redirect
from wtforms.fields.html5 import DateField
from __main__ import db, app, ma
from datetime import date
from sqlalchemy import Sequence
from sqlalchemy.exc import SQLAlchemyError
from status import st, Status
################################################################################
@@ -56,14 +58,21 @@ def loans():
@app.route("/loan", methods=["GET", "POST"])
def new_loan():
form = LoanForm(request.form)
page_title='Create new Loan'
if 'firstnames' not in request.form:
return render_template("loan.html", form=form, page_title='Create new Loan' )
return render_template("loan.html", form=form, page_title=page_title )
else:
loan = Loan( firstnames=request.form["firstnames"], surname=request.form["surname"], date_lent=request.form["date_lent"], contact_details=request.form["contact_details"] )
db.session.add(loan)
db.session.commit()
st.SetMessage( "Created new Loan (id={})".format(loan.id) )
return redirect( '/loans' )
try:
db.session.add(loan)
db.session.commit()
st.SetMessage( "Created new Loan (id={})".format(loan.id) )
return redirect( '/loans' )
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to add Loan:</b>&nbsp;{}".format( e.orig) )
return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
################################################################################
# /loan/<id> -> GET/POST(save or delete) -> shows/edits/delets a single loan
@@ -72,21 +81,27 @@ def new_loan():
def loan(id):
### DDP: should this be request.form or request.values?
form = LoanForm(request.form)
page_title='Edit Loan'
if request.method == 'POST' and form.validate():
loan = Loan.query.get(id)
if 'delete' in request.form:
st.SetMessage("Successfully deleted (id={}, who={} {})".format( loan.id, loan.firstnames, loan.surname ) )
loan = Loan.query.filter(Loan.id==id).delete()
if 'submit' in request.form:
st.SetMessage("Successfully Updated Loan (id={})".format(id) )
loan.firstnames = request.form['firstnames']
loan.surname = request.form['surname']
loan.surname = request.form['surname']
loan.contact_details = request.form['contact_details']
loan.date_lent = request.form['date_lent']
db.session.commit()
return redirect( '/loans' )
try:
if 'delete' in request.form:
st.SetMessage("Successfully deleted (id={}, who={} {})".format( loan.id, loan.firstnames, loan.surname ) )
loan = Loan.query.filter(Loan.id==id).delete()
if 'submit' in request.form:
st.SetMessage("Successfully Updated Loan (id={})".format(id) )
loan.firstnames = request.form['firstnames']
loan.surname = request.form['surname']
loan.surname = request.form['surname']
loan.contact_details = request.form['contact_details']
loan.date_lent = request.form['date_lent']
db.session.commit()
return redirect( '/loans' )
except SQLAlchemyError as e:
st.SetAlert( "danger" )
st.SetMessage( "<b>Failed to modify Loan:</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:
loan = Loan.query.get(id)
form = LoanForm(request.values, obj=loan)
return render_template("loan.html", loan=loan, form=form, page_title='Edit Loan' )
return render_template("loan.html", loan=loan, form=form, page_title=page_title)