|
| 1 | +import sqlite3 |
| 2 | +from contextlib import closing |
| 3 | + |
| 4 | +from bottle import Bottle |
| 5 | +from bottle import default_app |
| 6 | +from bottle import error |
| 7 | +from bottle import get |
| 8 | +from bottle import install |
| 9 | +from bottle import post |
| 10 | +from bottle import redirect |
| 11 | +from bottle import request |
| 12 | +from bottle import static_file |
| 13 | +from bottle import TEMPLATE_PATH |
| 14 | +from bottle import url |
| 15 | +from bottle import jinja2_template as template |
| 16 | + |
| 17 | +from bottle_sqlite import SQLitePlugin |
| 18 | +from bottle_flash import FlashPlugin |
| 19 | + |
| 20 | +# configuration goes here |
| 21 | +DATABASE = '/tmp/bottler.db' |
| 22 | +SECRET_KEY = 'development key' |
| 23 | +USERNAME = 'admin' |
| 24 | +PASSWORD = 'default' |
| 25 | + |
| 26 | +app = Bottle(__name__) |
| 27 | + |
| 28 | +# Resource configuration (relative to current file) |
| 29 | +app.resources.add_path('./', base=__file__) |
| 30 | +TEMPLATE_PATH.append("./templates") |
| 31 | + |
| 32 | +app.install(SQLitePlugin(dbfile='/tmp/bottler.db')) |
| 33 | +app.install(FlashPlugin(secret='COOKIE_SECRET')) |
| 34 | + |
| 35 | +def connect_db(): |
| 36 | + return sqlite3.connect('/tmp/bottler.db') |
| 37 | + |
| 38 | + |
| 39 | +def init_db(): |
| 40 | + with closing(connect_db()) as db: |
| 41 | + with app.resources.open('schema.sql') as f: |
| 42 | + db.cursor().executescript(f.read()) |
| 43 | + db.commit() |
| 44 | + |
| 45 | +#@app.before_request |
| 46 | +#def before_request(): |
| 47 | +# g.db = connect_db() |
| 48 | + |
| 49 | + |
| 50 | +#@app.teardown_request |
| 51 | +#def teardown_request(exception): |
| 52 | +# g.db.close() |
| 53 | + |
| 54 | +def write_entry(title, text): |
| 55 | + db.execute('insert into entries (title, text) values (?, ?)', |
| 56 | + [title, text]) |
| 57 | + db.commit() |
| 58 | + |
| 59 | + |
| 60 | +def get_all_entries(): |
| 61 | + db = connect_db() |
| 62 | + cur = db.execute('select title, text from entries order by id desc') |
| 63 | + entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()] |
| 64 | + return entries |
| 65 | + |
| 66 | +@app.route('/') |
| 67 | +def show_entries(): |
| 68 | + db = connect_db() |
| 69 | + cur = db.execute('select title, text from entries order by id desc') |
| 70 | + entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()] |
| 71 | + return template('show_entries.html', entries=entries) |
| 72 | + |
| 73 | +############################################################## |
| 74 | +# TEST PASS |
| 75 | +# |
| 76 | +#@app.route('/') |
| 77 | +#@app.route('/hello/<name>') |
| 78 | +#def greet(name='Stranger'): |
| 79 | +# return template('home.html', name=name) |
| 80 | +############################################################## |
| 81 | + |
| 82 | +@app.route('/add', methods=['POST']) |
| 83 | +def add_entry(): |
| 84 | + try: |
| 85 | + write_entry(request.form['title'], request.form['text']) |
| 86 | + flash('New entry was successfully posted') |
| 87 | + except sqlite3.Error as e: |
| 88 | + flash('There was an error: %s' % e.args[0]) |
| 89 | + return redirect(url('show_entries')) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + app.run(debug=True) |
0 commit comments