-
Notifications
You must be signed in to change notification settings - Fork 0
/
books.py
99 lines (87 loc) · 2.77 KB
/
books.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import json
import pymysql
import operator
from bottle import *
from datetime import date
try:
os.chdir(os.path.dirname(__file__))
except FileNotFoundError:
pass
with open('credentials.json') as f:
CREDS = json.load(f)
def db_connection():
return pymysql.connect(
host='books.ivoah.net',
db='books',
cursorclass=pymysql.cursors.DictCursor,
**CREDS
)
@get('/')
def main():
db = db_connection()
with db.cursor() as cursor:
cursor.execute('''
SELECT
books.isbn,
title,
subtitle,
author,
started,
finished,
count(quotes.isbn) AS quotes
FROM books LEFT JOIN quotes USING (isbn)
GROUP BY books.isbn
ORDER BY started
''')
books = cursor.fetchall()
db.close()
return template('root.tpl', books=books)
@get('/<isbn>')
def book(isbn):
db = db_connection()
with db.cursor() as cursor:
cursor.execute('SELECT * FROM books WHERE isbn=%s', isbn)
book = cursor.fetchone()
cursor.execute('SELECT * FROM quotes WHERE isbn=%s ORDER BY id', isbn)
quotes = cursor.fetchall()
cursor.execute('SELECT * FROM bookmarks WHERE isbn=%s ORDER BY date', isbn)
bookmarks = cursor.fetchall()
db.close()
return template('book.tpl', book=book, quotes=quotes, bookmarks=bookmarks)
@post('/<isbn>/add_quote')
@auth_basic(lambda u, p: {'user': u, 'password': p} == CREDS)
def add_quote(isbn):
quote = request.forms.quote
location = request.forms.location
date = request.forms.date
print(quote)
db = db_connection()
with db.cursor() as cursor:
cursor.execute('SELECT IFNULL(max(id), 0) FROM quotes WHERE isbn=%s', isbn)
last_id, = map(int, cursor.fetchone().values())
cursor.execute('''
INSERT INTO quotes (isbn, id, quote, location, date)
VALUES (%s, %s, %s, %s, %s);
''', (isbn, last_id + 1, quote, location, date))
db.commit()
db.close()
redirect(f'/{isbn}#last')
@post('/<isbn>/add_bookmark')
@auth_basic(lambda u, p: {'user': u, 'password': p} == CREDS)
def add_bookmark(isbn):
date = request.forms.date
location = request.forms.location
db = db_connection()
with db.cursor() as cursor:
cursor.execute('''
INSERT INTO bookmarks (isbn, date, location)
VALUES (%s, %s, %s);
''', (isbn, date, location))
db.commit()
db.close()
redirect(f'/{isbn}')
application = default_app()
if __name__ == '__main__':
get('/static/<filename>')(lambda filename: static_file(filename, root='static/'))
run(app=application, host='0.0.0.0', port=8080, debug=True, reloader=True)