loan now has working create/update/delete, still no add/rem books though
This commit is contained in:
49
loan.py
49
loan.py
@@ -1,14 +1,16 @@
|
|||||||
from wtforms import SubmitField, StringField, HiddenField, validators, TextAreaField
|
from wtforms import SubmitField, StringField, HiddenField, validators, TextAreaField
|
||||||
from flask_wtf import FlaskForm
|
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 wtforms.fields.html5 import DateField
|
||||||
from __main__ import db, app, ma
|
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 describing Loan in the database, and via sqlalchemy, connected to the DB as well
|
||||||
################################################################################
|
################################################################################
|
||||||
class Loan(db.Model):
|
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)
|
surname = db.Column(db.String(30), unique=False, nullable=False)
|
||||||
firstnames = db.Column(db.String(50), 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)
|
contact_details = db.Column(db.String(256), unique=False, nullable=False)
|
||||||
@@ -34,7 +36,7 @@ class LoanForm(FlaskForm):
|
|||||||
surname = StringField('Surname:', [validators.DataRequired()])
|
surname = StringField('Surname:', [validators.DataRequired()])
|
||||||
contact_details = TextAreaField('Contact Details:', [validators.DataRequired()])
|
contact_details = TextAreaField('Contact Details:', [validators.DataRequired()])
|
||||||
surname = StringField('Surname:', [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' )
|
submit = SubmitField('Save' )
|
||||||
delete = SubmitField('Delete' )
|
delete = SubmitField('Delete' )
|
||||||
|
|
||||||
@@ -46,7 +48,22 @@ class LoanForm(FlaskForm):
|
|||||||
@app.route("/loans", methods=["GET"])
|
@app.route("/loans", methods=["GET"])
|
||||||
def loans():
|
def loans():
|
||||||
loans = Loan.query.all()
|
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
|
# /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"])
|
@app.route("/loan/<id>", methods=["GET", "POST"])
|
||||||
def loan(id):
|
def loan(id):
|
||||||
### DDP: should this be request.form or request.values?
|
### DDP: should this be request.form or request.values?
|
||||||
alert="Success"
|
form = LoanForm(request.form)
|
||||||
loan_form = LoanForm(request.form)
|
if request.method == 'POST' and form.validate():
|
||||||
if request.method == 'POST' and loan_form.validate_on_submit():
|
|
||||||
id = request.form['id']
|
|
||||||
loan = Loan.query.get(id)
|
loan = Loan.query.get(id)
|
||||||
try:
|
if 'delete' in request.form:
|
||||||
request.form['submit']
|
st.SetMessage("Successfully deleted (id={}, who={} {})".format( loan.id, loan.firstnames, loan.surname ) )
|
||||||
except:
|
loan = Loan.query.filter(Loan.id==id).delete()
|
||||||
message="Sorry, Deleting unsupported at present"
|
if 'submit' in request.form:
|
||||||
alert="Danger"
|
st.SetMessage("Successfully Updated Loan (id={})".format(id) )
|
||||||
else:
|
|
||||||
loan.firstnames = request.form['firstnames']
|
loan.firstnames = request.form['firstnames']
|
||||||
loan.surname = request.form['surname']
|
loan.surname = request.form['surname']
|
||||||
loan.surname = request.form['surname']
|
loan.surname = request.form['surname']
|
||||||
loan.contact_details = request.form['contact_details']
|
loan.contact_details = request.form['contact_details']
|
||||||
loan.date_lent = request.form['date_lent']
|
loan.date_lent = request.form['date_lent']
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
message="Successfully Updated Loan (id={})".format(id)
|
return redirect( '/loans' )
|
||||||
else:
|
else:
|
||||||
loan = Loan.query.get(id)
|
loan = Loan.query.get(id)
|
||||||
loan_form = LoanForm(request.values, obj=loan)
|
form = LoanForm(request.values, obj=loan)
|
||||||
message=""
|
return render_template("loan.html", loan=loan, form=form, page_title='Edit Loan' )
|
||||||
return render_template("loan.html", loan=loan, alert=alert, message=message, loan_form=loan_form)
|
|
||||||
|
|||||||
@@ -1,39 +1,34 @@
|
|||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
{% block main_content %}
|
{% block main_content %}
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h3 class="col-lg-12"><center>Loan</center></h3>
|
<h3 class="col-lg-12"><center>{{page_title}}</center></h3>
|
||||||
{% if message|length %}
|
|
||||||
<div class="row alert alert-{{alert}}">
|
|
||||||
{{message}}
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-8">
|
<div class="col-lg-8">
|
||||||
<form class="form form-inline col-lg-12" action="" method="POST">
|
<form class="form form-inline col-lg-12" action="" method="POST">
|
||||||
{{ loan_form.csrf_token }}
|
{{ form.csrf_token }}
|
||||||
{{ loan_form.id }}
|
{{ form.id }}
|
||||||
<div class="form-row col-lg-12">
|
<div class="form-row col-lg-12">
|
||||||
{{ loan_form.firstnames.label( class="col-lg-2" ) }}
|
{{ form.firstnames.label( class="col-lg-2" ) }}
|
||||||
{{ loan_form.firstnames( class="form-control col-lg-10" ) }}
|
{{ form.firstnames( class="form-control col-lg-10" ) }}
|
||||||
</div class="form-row">
|
</div class="form-row">
|
||||||
<div class="form-row col-lg-12">
|
<div class="form-row col-lg-12">
|
||||||
{{ loan_form.surname.label( class="col-lg-2" ) }}
|
{{ form.surname.label( class="col-lg-2" ) }}
|
||||||
{{ loan_form.surname( class="form-control col-lg-10" ) }}
|
{{ form.surname( class="form-control col-lg-10" ) }}
|
||||||
</div class="form-row">
|
</div class="form-row">
|
||||||
<div class="form-row col-lg-12">
|
<div class="form-row col-lg-12">
|
||||||
{{ loan_form.date_lent.label( class="col-lg-2" ) }}
|
{{ form.date_lent.label( class="col-lg-2" ) }}
|
||||||
{{ loan_form.date_lent( class="form-control col-lg-10" ) }}
|
{{ form.date_lent( class="form-control col-lg-10" ) }}
|
||||||
</div class="form-row">
|
</div class="form-row">
|
||||||
<div class="form-row col-lg-12">
|
<div class="form-row col-lg-12">
|
||||||
{{ loan_form.contact_details.label( class="col-lg-2" ) }}
|
{{ form.contact_details.label( class="col-lg-2" ) }}
|
||||||
{{ loan_form.contact_details( class="form-control col-lg-10", rows="5" ) }}
|
{{ form.contact_details( class="form-control col-lg-10", rows="5" ) }}
|
||||||
</div class="form-row">
|
</div class="form-row">
|
||||||
<div class="row col-lg-12">
|
<div class="row col-lg-12">
|
||||||
<br>
|
<br>
|
||||||
</div class="row">
|
</div class="row">
|
||||||
<div class="form-row col-lg-12">
|
<div class="form-row col-lg-12">
|
||||||
{{ loan_form.delete( class="btn btn-outline-danger col-lg-2" )}}
|
{{ form.delete( class="btn btn-outline-danger col-lg-2" )}}
|
||||||
{{ loan_form.submit( class="btn btn-primary col-lg-2" )}}
|
{{ form.submit( class="btn btn-primary col-lg-2" )}}
|
||||||
</div class="form-row">
|
</div class="form-row">
|
||||||
</form>
|
</form>
|
||||||
</div class="col-lg-8">
|
</div class="col-lg-8">
|
||||||
@@ -47,7 +42,7 @@
|
|||||||
{% block script_content %}
|
{% block script_content %}
|
||||||
<script>
|
<script>
|
||||||
$(document).ready( function() {
|
$(document).ready( function() {
|
||||||
$("#books_for_loan_bit").load("/books_for_loan/" + {{loan_form.data.id}})
|
$("#books_for_loan_bit").load("/books_for_loan/" + {{form.data.id}})
|
||||||
} )
|
} )
|
||||||
</script>
|
</script>
|
||||||
{% endblock script_content %}
|
{% endblock script_content %}
|
||||||
|
|||||||
@@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
{% block main_content %}
|
{% block main_content %}
|
||||||
<h3>Loans</h3>
|
<h3>Loans</h3>
|
||||||
|
{% if message|length %}
|
||||||
|
<div class="row alert alert-{{alert}}">
|
||||||
|
{{message}}
|
||||||
|
{{ ClearStatus() }}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
<table id="book_table" class="table table-striped table-sm" data-toolbar="#toolbar" data-search="true">
|
<table id="book_table" class="table table-striped table-sm" data-toolbar="#toolbar" data-search="true">
|
||||||
<thead>
|
<thead>
|
||||||
<tr class="thead-light"><th>Surname</th><th>Firstname(s)</th><th>Date Lent</th><th>Contact Details</th></tr>
|
<tr class="thead-light"><th>Surname</th><th>Firstname(s)</th><th>Date Lent</th><th>Contact Details</th></tr>
|
||||||
|
|||||||
Reference in New Issue
Block a user