80 lines
3.7 KiB
Python
80 lines
3.7 KiB
Python
from wtforms import SubmitField, StringField, HiddenField, validators, TextAreaField
|
|
from flask_wtf import FlaskForm
|
|
from flask import request, render_template
|
|
from wtforms.fields.html5 import DateField
|
|
from __main__ import db, app, ma
|
|
|
|
################################################################################
|
|
# 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)
|
|
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)
|
|
date_lent = db.Column(db.Date, nullable=False )
|
|
|
|
def __repr__(self):
|
|
return "<id: {}, firstnames: {}, surname: {}>".format(self.id,self.firstnames, self.surname)
|
|
|
|
################################################################################
|
|
# Helper class that inherits a .dump() method to turn class Loan into json / useful in jinja2
|
|
################################################################################
|
|
class LoanSchema(ma.SQLAlchemyAutoSchema):
|
|
class Meta:
|
|
model = Loan
|
|
ordered = True
|
|
|
|
################################################################################
|
|
# Helper class that defines a form for loan, used to make html <form>, with field validation (via wtforms)
|
|
################################################################################
|
|
class LoanForm(FlaskForm):
|
|
id = HiddenField()
|
|
firstnames = StringField('FirstName(s):', [validators.DataRequired()])
|
|
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' )
|
|
submit = SubmitField('Save' )
|
|
delete = SubmitField('Delete' )
|
|
|
|
################################################################################
|
|
# Routes for loan data
|
|
#
|
|
# /loans -> GET only -> prints out list of all loans
|
|
################################################################################
|
|
@app.route("/loans", methods=["GET"])
|
|
def loans():
|
|
loans = Loan.query.all()
|
|
return render_template("loans.html", loans=loans)
|
|
|
|
################################################################################
|
|
# /loan/<id> -> GET/POST(save or delete) -> shows/edits/delets a single loan
|
|
################################################################################
|
|
@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']
|
|
loan = Loan.query.get(id)
|
|
try:
|
|
request.form['submit']
|
|
except:
|
|
message="Sorry, Deleting unsupported at present"
|
|
alert="Danger"
|
|
else:
|
|
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)
|
|
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)
|