|
5 | 5 | from flask import request |
6 | 6 | from flask import url_for |
7 | 7 | from flask import redirect |
8 | | -import sqlite3 |
| 8 | +from flask import session |
| 9 | +from flask import flash |
| 10 | + |
9 | 11 | from contextlib import closing |
10 | 12 |
|
| 13 | +import sqlite3 |
| 14 | + |
11 | 15 | app = Flask(__name__) |
12 | 16 |
|
13 | 17 | app.config.from_pyfile('microblog.cfg') |
14 | 18 |
|
15 | | - |
16 | 19 | def connect_db(): |
17 | | - return sqlite3.connect(app.config['DATABASE']) |
18 | | - |
| 20 | + return sqlite3.connect(app.config['DATABASE']) |
19 | 21 |
|
20 | 22 | def init_db(): |
21 | | - with closing(connect_db()) as db: |
22 | | - with app.open_resource('schema.sql') as f: |
23 | | - db.cursor().executescript(f.read()) |
24 | | - db.commit() |
25 | | - |
| 23 | + with closing(connect_db()) as db: |
| 24 | + with app.open_resource('schema.sql') as f: |
| 25 | + db.cursor().executescript(f.read()) |
| 26 | + db.commit() |
26 | 27 |
|
27 | 28 | def get_database_connection(): |
28 | | - db = getattr(g, 'db', None) |
29 | | - if db is None: |
30 | | - g.db = db = connect_db() |
31 | | - return db |
32 | | - |
| 29 | + db = getattr(g, 'db', None) |
| 30 | + if db is None: |
| 31 | + g.db = db = connect_db() |
| 32 | + return db |
33 | 33 |
|
34 | 34 | @app.teardown_request |
35 | 35 | def teardown_request(exception): |
36 | | - db = getattr(g, 'db', None) |
37 | | - if db is not None: |
38 | | - db.close() |
39 | | - |
| 36 | + db = getattr(g, 'db', None) |
| 37 | + if db is not None: |
| 38 | + db.close() |
40 | 39 |
|
41 | 40 | def write_entry(title, text): |
42 | 41 | con = get_database_connection() |
43 | 42 | con.execute('insert into entries (title, text) values (?, ?)', |
44 | | - [title, text]) |
| 43 | + [title, text]) |
45 | 44 | con.commit() |
46 | 45 |
|
47 | | - |
48 | 46 | def get_all_entries(): |
49 | 47 | con = get_database_connection() |
50 | 48 | cur = con.execute('SELECT title, text FROM entries ORDER BY id DESC') |
51 | 49 | return [dict(title=row[0], text=row[1]) for row in cur.fetchall()] |
52 | 50 |
|
53 | | - |
54 | 51 | @app.route('/') |
55 | 52 | def show_entries(): |
56 | | - entries = get_all_entries() |
57 | | - return render_template('show_entries.html', entries=entries) |
58 | | - |
| 53 | + entries = get_all_entries() |
| 54 | + return render_template('show_entries.html', entries = entries) |
59 | 55 |
|
60 | 56 | @app.route('/add', methods=['POST']) |
61 | 57 | def add_entry(): |
62 | | - try: |
63 | | - write_entry(request.form['title'], request.form['text']) |
64 | | - except sqlite3.Error: |
65 | | - abort(500) |
66 | | - return redirect(url_for('show_entries')) |
67 | | - |
| 58 | + if not session.get('logged_in'): |
| 59 | + abort(401) |
| 60 | + elif not request.form['title'] or not request.form['text']: |
| 61 | + flash('Please enter a title and an entry.') |
| 62 | + else: |
| 63 | + try: |
| 64 | + write_entry(request.form['title'], request.form['text']) |
| 65 | + flash('New entry posted!') |
| 66 | + except sqlite3.Error as e: |
| 67 | + flash('Your entry was NOT created! Error message: %s' % str(e)) |
| 68 | + return redirect(url_for('show_entries')) |
| 69 | + |
| 70 | + |
| 71 | +@app.route('/login', methods=['GET', 'POST']) |
| 72 | +def login(): |
| 73 | + error = None |
| 74 | + if request.method == 'POST': |
| 75 | + if request.form['username'] != app.config['USERNAME']: |
| 76 | + error = 'Invalid username' |
| 77 | + elif request.form['password'] != app.config['PASSWORD']: |
| 78 | + error = 'Invalid password' |
| 79 | + else: |
| 80 | + session['logged_in'] = True |
| 81 | + flash('You are logged in') |
| 82 | + return redirect(url_for('show_entries')) |
| 83 | + return render_template('login.html', error=error) |
| 84 | + |
| 85 | +@app.route('/logout') |
| 86 | +def logout(): |
| 87 | + session.pop('logged_in', None) |
| 88 | + flash('You have logged out') |
| 89 | + return redirect(url_for('show_entries')) |
68 | 90 |
|
69 | 91 | if __name__ == '__main__': |
70 | 92 | app.run(debug=True) |
0 commit comments