can now create/update/delete authors (base.html, using edit*.html, and authors.html)
This commit is contained in:
14
README
14
README
@@ -24,10 +24,12 @@ pip3 install --user flask sqlalchemy flask-sqlalchemy flask-marshmallow SQLAlche
|
|||||||
python3 main.py
|
python3 main.py
|
||||||
|
|
||||||
|
|
||||||
|
### MAYBE:
|
||||||
|
when moving a book in a series (which is part of 2 series), do we pop-up
|
||||||
|
offer to move parent/child series orders as well to match (think Thomas Covenant)
|
||||||
|
|
||||||
### TODO:
|
### TODO:
|
||||||
-- Without redirects (after creation) the potential for reload's trying to re-add objects may suck...
|
- need to create series, loan, book classes (and make green + button work)
|
||||||
-> see if I can see how to pass alert/message through to /owneds somehow ... if I can even pass a global or some shit then pick it up in the python code?
|
|
||||||
- need to create all classes (green + button)
|
|
||||||
- need to delete all classes (and watch for referential integrity)
|
- need to delete all classes (and watch for referential integrity)
|
||||||
- need to add 1 author to book, book to sub_book, series
|
- need to add 1 author to book, book to sub_book, series
|
||||||
- need to delete 1 author from book
|
- need to delete 1 author from book
|
||||||
@@ -37,9 +39,3 @@ python3 main.py
|
|||||||
- show books on shelf list
|
- show books on shelf list
|
||||||
- show books to buy view / printable
|
- show books to buy view / printable
|
||||||
- with ORM: do I to lazy load all books (ajax the 2nd->last pages in, or not use ORM, and do a quick db.execute()....)
|
- with ORM: do I to lazy load all books (ajax the 2nd->last pages in, or not use ORM, and do a quick db.execute()....)
|
||||||
|
|
||||||
|
|
||||||
### MAYBE:
|
|
||||||
when moving a book in a series (which is part of 2 series), do we pop-up
|
|
||||||
offer to move parent/child series orders as well to match (think Thomas Covenant)
|
|
||||||
|
|
||||||
|
|||||||
56
author.py
56
author.py
@@ -1,13 +1,14 @@
|
|||||||
from wtforms import SubmitField, StringField, HiddenField, validators, Form
|
from wtforms import SubmitField, StringField, HiddenField, validators, Form
|
||||||
from flask_wtf import FlaskForm
|
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 __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 describing Author in the database, and via sqlalchemy, connected to the DB as well
|
||||||
################################################################################
|
################################################################################
|
||||||
class Author(db.Model):
|
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)
|
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)
|
||||||
|
|
||||||
@@ -40,7 +41,24 @@ class AuthorForm(FlaskForm):
|
|||||||
@app.route("/authors", methods=["GET"])
|
@app.route("/authors", methods=["GET"])
|
||||||
def authors():
|
def authors():
|
||||||
authors = Author.query.all()
|
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
|
# /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"])
|
@app.route("/author/<id>", methods=["GET", "POST"])
|
||||||
def author(id):
|
def author(id):
|
||||||
### DDP: should this be request.form or request.values?
|
### DDP: should this be request.form or request.values?
|
||||||
alert="Success"
|
form = AuthorForm(request.form)
|
||||||
author_form = AuthorForm(request.form)
|
if request.method == 'POST' and form.validate():
|
||||||
if request.method == 'POST' and author_form.validate_on_submit():
|
|
||||||
id = request.form['id']
|
|
||||||
author = Author.query.get(id)
|
author = Author.query.get(id)
|
||||||
try:
|
if 'delete' in request.form:
|
||||||
request.form['submit']
|
st.SetMessage("Successfully deleted Author: ({})".format( author ) )
|
||||||
except:
|
author = Author.query.filter(Author.id==id).delete()
|
||||||
message="Sorry, Deleting unsupported at present"
|
if 'submit' in request.form:
|
||||||
alert="Danger"
|
st.SetMessage("Successfully Updated Author: (From: {}".format(author) )
|
||||||
else:
|
|
||||||
author.firstnames = request.form['firstnames']
|
|
||||||
author.surname = request.form['surname']
|
author.surname = request.form['surname']
|
||||||
db.session.commit()
|
author.firstnames = request.form['firstnames']
|
||||||
message="Successfully Updated Author (id={})".format(id)
|
st.AppendMessage(" To: {}".format(author) )
|
||||||
|
db.session.commit()
|
||||||
|
return redirect( '/authors' )
|
||||||
else:
|
else:
|
||||||
author = Author.query.get(id)
|
author = Author.query.get(id)
|
||||||
author_form = AuthorForm(request.values, obj=author)
|
form = AuthorForm(request.values, obj=author)
|
||||||
message=""
|
return render_template("edit_id_name.html", object=author, form=form)
|
||||||
return render_template("author.html", author=author, alert=alert, message=message, author_form=author_form)
|
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
# helper fund to GetAuthors -> author_list -> jinja2 for author drop-down in book.html
|
||||||
|
################################################################################
|
||||||
def GetAuthors():
|
def GetAuthors():
|
||||||
return Author.query.order_by('surname','firstnames').all()
|
return Author.query.order_by('surname','firstnames').all()
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
{% extends "base.html" %} {% block main_content %}
|
|
||||||
<div class="container">
|
|
||||||
<h3 class="col-lg-12"><center>Author</center></h3>
|
|
||||||
<div class="row">
|
|
||||||
{% if message|length %}
|
|
||||||
<div class="row alert alert-{{alert}}">
|
|
||||||
{{message}}
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
<form class="form form-inline col-lg-12" action="" method="POST">
|
|
||||||
{{ author_form.csrf_token }}
|
|
||||||
{{ author_form.id }}
|
|
||||||
<div class="form-row col-lg-12">
|
|
||||||
{{ author_form.firstnames.label( class="col-lg-2" ) }}
|
|
||||||
{{ author_form.firstnames( class="form-control col-lg-2" ) }}
|
|
||||||
</div class="form-row">
|
|
||||||
<div class="form-row col-lg-12">
|
|
||||||
{{ author_form.surname.label( class="col-lg-2" ) }}
|
|
||||||
{{ author_form.surname( class="form-control col-lg-2" ) }}
|
|
||||||
</div class="form-row">
|
|
||||||
<div class="row col-lg-12">
|
|
||||||
<br>
|
|
||||||
</div class="row">
|
|
||||||
<div class="form-row col-lg-12">
|
|
||||||
{{ author_form.delete( class="btn btn-outline-danger col-lg-2" )}}
|
|
||||||
{{ author_form.submit( class="btn btn-primary col-lg-2" )}}
|
|
||||||
</div class="form-row">
|
|
||||||
</form>
|
|
||||||
</div class="row">
|
|
||||||
</div class="container">
|
|
||||||
{% endblock main_content %}
|
|
||||||
Reference in New Issue
Block a user