can now create/update/delete authors (base.html, using edit*.html, and authors.html)
This commit is contained in:
56
author.py
56
author.py
@@ -1,13 +1,14 @@
|
||||
from wtforms import SubmitField, StringField, HiddenField, validators, Form
|
||||
from flask_wtf import FlaskForm
|
||||
from flask import request, render_template
|
||||
from flask import request, render_template, redirect
|
||||
from __main__ import db, app, ma
|
||||
from status import st, Status
|
||||
|
||||
################################################################################
|
||||
# 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)
|
||||
id = db.Column(db.Integer, db.Sequence('author_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)
|
||||
|
||||
@@ -40,7 +41,24 @@ class AuthorForm(FlaskForm):
|
||||
@app.route("/authors", methods=["GET"])
|
||||
def authors():
|
||||
authors = Author.query.all()
|
||||
return render_template("authors.html", authors=authors)
|
||||
return render_template("authors.html", authors=authors, alert=st.GetAlert(), message=st.GetMessage() )
|
||||
|
||||
|
||||
################################################################################
|
||||
# /author -> GET/POST -> creates a new author type and when created, takes you back to /authors
|
||||
################################################################################
|
||||
@app.route("/author", methods=["GET", "POST"])
|
||||
def new_author():
|
||||
form = AuthorForm(request.form)
|
||||
if 'surname' not in request.form:
|
||||
return render_template("edit_id_name.html", form=form, page_title='Create new Author' )
|
||||
else:
|
||||
author = Author( surname=request.form["surname"], firstnames=request.form["firstnames"] )
|
||||
db.session.add(author)
|
||||
db.session.commit()
|
||||
st.SetMessage( "Created new Author ({})".format(author) )
|
||||
return redirect( '/authors' )
|
||||
|
||||
|
||||
################################################################################
|
||||
# /author/<id> -> GET/POST(save or delete) -> shows/edits/delets a single author
|
||||
@@ -48,26 +66,26 @@ def authors():
|
||||
@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']
|
||||
form = AuthorForm(request.form)
|
||||
if request.method == 'POST' and form.validate():
|
||||
author = Author.query.get(id)
|
||||
try:
|
||||
request.form['submit']
|
||||
except:
|
||||
message="Sorry, Deleting unsupported at present"
|
||||
alert="Danger"
|
||||
else:
|
||||
author.firstnames = request.form['firstnames']
|
||||
if 'delete' in request.form:
|
||||
st.SetMessage("Successfully deleted Author: ({})".format( author ) )
|
||||
author = Author.query.filter(Author.id==id).delete()
|
||||
if 'submit' in request.form:
|
||||
st.SetMessage("Successfully Updated Author: (From: {}".format(author) )
|
||||
author.surname = request.form['surname']
|
||||
db.session.commit()
|
||||
message="Successfully Updated Author (id={})".format(id)
|
||||
author.firstnames = request.form['firstnames']
|
||||
st.AppendMessage(" To: {}".format(author) )
|
||||
db.session.commit()
|
||||
return redirect( '/authors' )
|
||||
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)
|
||||
form = AuthorForm(request.values, obj=author)
|
||||
return render_template("edit_id_name.html", object=author, form=form)
|
||||
|
||||
################################################################################
|
||||
# helper fund to GetAuthors -> author_list -> jinja2 for author drop-down in book.html
|
||||
################################################################################
|
||||
def GetAuthors():
|
||||
return Author.query.order_by('surname','firstnames').all()
|
||||
|
||||
Reference in New Issue
Block a user