loan now has working create/update/delete, still no add/rem books though

This commit is contained in:
2020-12-21 22:06:35 +11:00
parent c4d7fdaa04
commit 7bd81d3119
3 changed files with 52 additions and 38 deletions

51
loan.py
View File

@@ -1,14 +1,16 @@
from wtforms import SubmitField, StringField, HiddenField, validators, TextAreaField
from flask_wtf import FlaskForm
from flask import request, render_template
from flask import request, render_template, redirect
from wtforms.fields.html5 import DateField
from __main__ import db, app, ma
from datetime import date
from status import st, Status
################################################################################
# Class describing Loan in the database, and via sqlalchemy, connected to the DB as well
################################################################################
class Loan(db.Model):
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
id = db.Column(db.Integer, db.Sequence('loan_id_seq'), primary_key=True )
surname = db.Column(db.String(30), unique=False, nullable=False)
firstnames = db.Column(db.String(50), unique=False, nullable=False)
contact_details = db.Column(db.String(256), unique=False, nullable=False)
@@ -34,7 +36,7 @@ class LoanForm(FlaskForm):
surname = StringField('Surname:', [validators.DataRequired()])
contact_details = TextAreaField('Contact Details:', [validators.DataRequired()])
surname = StringField('Surname:', [validators.DataRequired()])
date_lent = DateField('Date Lent:', format='%Y-%m-%d' )
date_lent = DateField('Date Lent:', format='%Y-%m-%d', default=date.today(), validators=[validators.DataRequired()] )
submit = SubmitField('Save' )
delete = SubmitField('Delete' )
@@ -46,7 +48,22 @@ class LoanForm(FlaskForm):
@app.route("/loans", methods=["GET"])
def loans():
loans = Loan.query.all()
return render_template("loans.html", loans=loans)
return render_template("loans.html", loans=loans, alert=st.GetAlert(), message=st.GetMessage())
################################################################################
# /loan -> GET/POST -> creates a new loan type and when created, takes you back to /loans
################################################################################
@app.route("/loan", methods=["GET", "POST"])
def new_loan():
form = LoanForm(request.form)
if 'firstnames' not in request.form:
return render_template("loan.html", form=form, page_title='Create new Loan' )
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' )
################################################################################
# /loan/<id> -> GET/POST(save or delete) -> shows/edits/delets a single loan
@@ -54,26 +71,22 @@ def loans():
@app.route("/loan/<id>", methods=["GET", "POST"])
def loan(id):
### DDP: should this be request.form or request.values?
alert="Success"
loan_form = LoanForm(request.form)
if request.method == 'POST' and loan_form.validate_on_submit():
id = request.form['id']
form = LoanForm(request.form)
if request.method == 'POST' and form.validate():
loan = Loan.query.get(id)
try:
request.form['submit']
except:
message="Sorry, Deleting unsupported at present"
alert="Danger"
else:
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()
message="Successfully Updated Loan (id={})".format(id)
db.session.commit()
return redirect( '/loans' )
else:
loan = Loan.query.get(id)
loan_form = LoanForm(request.values, obj=loan)
message=""
return render_template("loan.html", loan=loan, alert=alert, message=message, loan_form=loan_form)
form = LoanForm(request.values, obj=loan)
return render_template("loan.html", loan=loan, form=form, page_title='Edit Loan' )