Record pages on existing books

Very very minimal and also very ugly
This commit is contained in:
Peter J. Holzer 2022-09-01 15:57:49 +02:00
commit 2fdcc2f6c1
4 changed files with 113 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

58
app.py Normal file
View File

@ -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/<id>")
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/<id>/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()

36
templates/book.html Normal file
View File

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title> Lesetagebuch: {{book.title}} </title>
<style>
table.bookmarks {
width: clamp(24ch, 90vw, 80ch)
}
td.pagenr {
text-align: right;
}
</style>
</head>
<body>
<h1>{{ book.title }}</h1>
<form method="post" action="{{ url_for('bookmark', id=book.id) }}">
<input name="page">
<input type="submit">
</form>
<table class="bookmarks">
{% for bookmark in bookmarks %}
<tr>
<td class="timestamp">
{{bookmark.ts.strftime("%Y-%m-%d %H:%M%z")}}
</td>
<td class="pagenr">
{{bookmark.page}}
</td>
</tr>
{% endfor %}
</table>
</body>
</html>

18
templates/home.html Normal file
View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title> Lesetagebuch </title>
</head>
<body>
<ul>
{% for book in books %}
<li>
<a href="/book/{{book.id}}">{{ book.title }}</a>
</li>
{% endfor %}
</ul>
</body>
</html>