From 2fdcc2f6c1bc32638e848b62255d39c3e45ccd0e Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Thu, 1 Sep 2022 15:57:49 +0200 Subject: [PATCH] Record pages on existing books Very very minimal and also very ugly --- .gitignore | 1 + app.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ templates/book.html | 36 ++++++++++++++++++++++++++++ templates/home.html | 18 ++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 templates/book.html create mode 100644 templates/home.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/app.py b/app.py new file mode 100644 index 0000000..23acef8 --- /dev/null +++ b/app.py @@ -0,0 +1,58 @@ +import logging +import psycopg2 +import psycopg2.extras + +from flask import Flask, render_template, g, redirect, request, url_for + +logging.basicConfig(format="%(asctime)s %(levelname)s %(name)s %(lineno)d | %(message)s", level=logging.DEBUG) + +app = Flask(__name__) + +log = logging.getLogger(__name__) + +@app.route("/") +def home(): + csr = get_cursor() + csr.execute("select * from books where current") + books = csr.fetchall() + return render_template("home.html", books=books) + +@app.route("/book/") +def book(id): + csr = get_cursor() + csr.execute("select * from books where id = %s", (id,)) + book = csr.fetchone() + csr.execute("select * from bookmarks where book = %s order by ts desc", (id,)) + bookmarks = csr.fetchall() + return render_template("book.html", book=book, bookmarks=bookmarks) + +@app.route("/book//mark", methods=["POST"]) +def bookmark(id): + page = request.form["page"] + csr = get_cursor() + csr.execute("insert into bookmarks(book, page) values(%s, %s)", (id, page,)) + return redirect(url_for("book", id=id), code=303) + +def get_cursor(): + db = get_db() + csr = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) + return csr + +def get_db(): + if "db" not in g: + g.db = psycopg2.connect(dbname="lesetagebuch") + return g.db + +@app.teardown_appcontext +def teardown_db(exception): + log.info("in teardown_db: exception = %s", exception) + db = g.pop('db', None) + if db is not None: + if not exception: + try: + db.commit() + except: + pass + db.rollback() + db.close() + diff --git a/templates/book.html b/templates/book.html new file mode 100644 index 0000000..7cad313 --- /dev/null +++ b/templates/book.html @@ -0,0 +1,36 @@ + + + + + + Lesetagebuch: {{book.title}} + + + +

{{ book.title }}

+
+ + +
+ + {% for bookmark in bookmarks %} + + + + + {% endfor %} +
+ {{bookmark.ts.strftime("%Y-%m-%d %H:%M%z")}} + + {{bookmark.page}} +
+ + + diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..335f5ae --- /dev/null +++ b/templates/home.html @@ -0,0 +1,18 @@ + + + + + + Lesetagebuch + + + + +