From a4dca1dbf506dc931ac6c8b891f76b0612fa2846 Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Wed, 11 Nov 2020 19:23:48 +1100 Subject: [PATCH] updated README to reflect next steps (will use flask-wtf and flask-boostrap), but proof-of-concept author/ works to save to database with a form / POST --- README | 6 +++--- main.py | 19 +++++++++++++++-- templates/author.html | 47 +++++++++++++++++++++++++++++++++++++------ templates/books.html | 2 +- 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/README b/README index cc25667..db56426 100644 --- a/README +++ b/README @@ -16,7 +16,7 @@ python3 main.py ### TODO: -- book subbook link will be next real challenge (with raw sql for now) -- then next challenge will be to make single book page an edit / save -- then its just finish this off :) - fix up lame book linkages to tabels that are so not 3nf, *_LST tables, etc. +- use flask-wtf and flask-bootstrap, see this: https://medium.com/better-programming/how-to-use-flask-wtforms-faab71d5a034 for author/ +- consider getting rest of Book class connected, e.g. series, loan, etc. +- see if we can break this out of main.py -- can't recall what was circular about this, but at least the wtf bits seem to be able to be moved out, so why not do that diff --git a/main.py b/main.py index d0b2863..2c0a89c 100644 --- a/main.py +++ b/main.py @@ -167,9 +167,24 @@ def book(id): return render_template("books.html", books=book_s, subs=sub_book ) @app.route("/authors", methods=["GET"]) -def author(): +def authors(): authors = Author.query.all() - return render_template("author.html", authors=authors) + return render_template("authors.html", authors=authors) + +@app.route("/author/", methods=["GET", "POST"]) +def author(id): + if request.method == 'POST': + id = request.form['author.id'] + author = Author.query.get(id) + author.firstnames = request.form['author.firstnames'] + author.surname = request.form['author.surname'] + db.session.commit() + message="Successfully Updated Author (id={})".format(id) + else: + author = Author.query.get(id) + message="" + return render_template("author.html", author=author, message=message) + if __name__ == "__main__": app.run(host="0.0.0.0", debug=True) diff --git a/templates/author.html b/templates/author.html index 0dbdf16..518702c 100644 --- a/templates/author.html +++ b/templates/author.html @@ -1,9 +1,44 @@ - + + + + -

authors

- {% for author in authors %} -

{{author.surname}}, {{author.firstnames}}

- {% endfor %} - + + + + + + +

Author

+
+ {% if message|length %} +
+ {{message}} +
+ {% endif %} +
+
+ +
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+
+
+
+ diff --git a/templates/books.html b/templates/books.html index e8b27c3..ac10790 100644 --- a/templates/books.html +++ b/templates/books.html @@ -80,7 +80,7 @@ - {% if books.sub_book is defined %} + {% if books.sub_book|length %}

sub_book is defined: {{books.sub_book}}

{% endif %} {{ books.subs }}