74 lines
3.3 KiB
Python
74 lines
3.3 KiB
Python
from wtforms import SubmitField, StringField, HiddenField, validators, Form
|
|
from flask_wtf import FlaskForm
|
|
from flask import request, render_template
|
|
from __main__ import db, app, ma
|
|
|
|
################################################################################
|
|
# Class describing Author in the database, and via sqlalchemy, connected to the DB as well
|
|
################################################################################
|
|
class Author(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)
|
|
|
|
def __repr__(self):
|
|
return "<id: {}, firstnames: {}, surname: {}>".format(self.id,self.firstnames, self.surname)
|
|
|
|
################################################################################
|
|
# Helper class that inherits a .dump() method to turn class Author into json / useful in jinja2
|
|
################################################################################
|
|
class AuthorSchema(ma.SQLAlchemyAutoSchema):
|
|
class Meta:
|
|
model = Author
|
|
ordered = True
|
|
|
|
################################################################################
|
|
# Helper class that defines a form for author, used to make html <form>, with field validation (via wtforms)
|
|
################################################################################
|
|
class AuthorForm(FlaskForm):
|
|
id = HiddenField()
|
|
firstnames = StringField('FirstName(s):', [validators.DataRequired()])
|
|
surname = StringField('Surname:', [validators.DataRequired()])
|
|
submit = SubmitField('Save' )
|
|
delete = SubmitField('Delete' )
|
|
|
|
################################################################################
|
|
# Routes for author data
|
|
#
|
|
# /authors -> GET only -> prints out list of all authors
|
|
################################################################################
|
|
@app.route("/authors", methods=["GET"])
|
|
def authors():
|
|
authors = Author.query.all()
|
|
return render_template("authors.html", authors=authors)
|
|
|
|
################################################################################
|
|
# /author/<id> -> GET/POST(save or delete) -> shows/edits/delets a single author
|
|
################################################################################
|
|
@app.route("/author/<id>", methods=["GET", "POST"])
|
|
def author(id):
|
|
### DDP: should this be request.form or request.values?
|
|
alert="Success"
|
|
author_form = AuthorForm(request.form)
|
|
if request.method == 'POST' and author_form.validate_on_submit():
|
|
id = request.form['id']
|
|
author = Author.query.get(id)
|
|
try:
|
|
request.form['submit']
|
|
except:
|
|
message="Sorry, Deleting unsupported at present"
|
|
alert="Danger"
|
|
else:
|
|
author.firstnames = request.form['firstnames']
|
|
author.surname = request.form['surname']
|
|
db.session.commit()
|
|
message="Successfully Updated Author (id={})".format(id)
|
|
else:
|
|
author = Author.query.get(id)
|
|
author_form = AuthorForm(request.values, obj=author)
|
|
message=""
|
|
return render_template("author.html", author=author, alert=alert, message=message, author_form=author_form)
|
|
|
|
def GetAuthors():
|
|
return Author.query.order_by('surname','firstnames').all()
|