pulled apart main to make a self-contained author.py... Weird use of importing from __main__ rather than main.py, and ordering the import to after the db object was created seems to be the secret sauce

This commit is contained in:
2020-11-14 15:03:37 +11:00
parent f8d31dd325
commit 3831c0a03b
2 changed files with 64 additions and 46 deletions

View File

@@ -1,8 +1,68 @@
from wtforms import SubmitField, StringField, HiddenField, validators, Form
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)
firstnames = db.Column(db.String(50), unique=False, nullable=False)
surname = db.Column(db.String(30), 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
################################################################################
# Helper class that defines a form for author, used to make html <form>, with field validation (via wtforms)
################################################################################
class AuthorForm(Form):
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():
id = request.form['id']
author = Author.query.get(id)
try:
request.form['submit']
except:
message="Sorry, Deleting unsupported at present"
alert="Danger"
else:
print( "seems to be saving" )
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)