| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  |     Flaskr | 
					
						
							|  |  |  |     ~~~~~~ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     A microblog example application written as Flask tutorial with | 
					
						
							|  |  |  |     Flask and sqlite3. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-03 02:21:07 +08:00
										 |  |  |     :copyright: (c) 2014 by Armin Ronacher. | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |     :license: BSD, see LICENSE for more details. | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2013-05-22 07:33:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 06:07:13 +08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2010-12-02 00:22:55 +08:00
										 |  |  | from sqlite3 import dbapi2 as sqlite3 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  | from flask import Flask, request, session, g, redirect, url_for, abort, \ | 
					
						
							|  |  |  |      render_template, flash | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # create our little application :) | 
					
						
							|  |  |  | app = Flask(__name__) | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Load default config and override config from an environment variable | 
					
						
							|  |  |  | app.config.update(dict( | 
					
						
							| 
									
										
										
										
											2014-02-09 06:07:13 +08:00
										 |  |  |     DATABASE=os.path.join(app.root_path, 'flaskr.db'), | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  |     DEBUG=True, | 
					
						
							|  |  |  |     SECRET_KEY='development key', | 
					
						
							|  |  |  |     USERNAME='admin', | 
					
						
							|  |  |  |     PASSWORD='default' | 
					
						
							|  |  |  | )) | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  | app.config.from_envvar('FLASKR_SETTINGS', silent=True) | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  | def connect_db(): | 
					
						
							|  |  |  |     """Connects to the specific database.""" | 
					
						
							|  |  |  |     rv = sqlite3.connect(app.config['DATABASE']) | 
					
						
							|  |  |  |     rv.row_factory = sqlite3.Row | 
					
						
							|  |  |  |     return rv | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 18:46:04 +08:00
										 |  |  | def init_db(): | 
					
						
							|  |  |  |     """Initializes the database.""" | 
					
						
							| 
									
										
										
										
											2014-04-28 21:18:27 +08:00
										 |  |  |     db = get_db() | 
					
						
							|  |  |  |     with app.open_resource('schema.sql', mode='r') as f: | 
					
						
							|  |  |  |         db.cursor().executescript(f.read()) | 
					
						
							|  |  |  |     db.commit() | 
					
						
							| 
									
										
										
										
											2014-05-02 18:46:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @app.cli.command('initdb') | 
					
						
							|  |  |  | def initdb_command(): | 
					
						
							|  |  |  |     """Creates the database tables.""" | 
					
						
							|  |  |  |     init_db() | 
					
						
							| 
									
										
										
										
											2014-04-28 21:18:27 +08:00
										 |  |  |     print('Initialized the database.') | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-10 03:02:32 +08:00
										 |  |  | def get_db(): | 
					
						
							|  |  |  |     """Opens a new database connection if there is none yet for the
 | 
					
						
							|  |  |  |     current application context. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  |     if not hasattr(g, 'sqlite_db'): | 
					
						
							|  |  |  |         g.sqlite_db = connect_db() | 
					
						
							|  |  |  |     return g.sqlite_db | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-10 03:02:32 +08:00
										 |  |  | @app.teardown_appcontext | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  | def close_db(error): | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |     """Closes the database again at the end of the request.""" | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  |     if hasattr(g, 'sqlite_db'): | 
					
						
							|  |  |  |         g.sqlite_db.close() | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @app.route('/') | 
					
						
							|  |  |  | def show_entries(): | 
					
						
							| 
									
										
										
										
											2012-10-10 03:02:32 +08:00
										 |  |  |     db = get_db() | 
					
						
							|  |  |  |     cur = db.execute('select title, text from entries order by id desc') | 
					
						
							| 
									
										
										
										
											2012-11-05 08:00:46 +08:00
										 |  |  |     entries = cur.fetchall() | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |     return render_template('show_entries.html', entries=entries) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @app.route('/add', methods=['POST']) | 
					
						
							|  |  |  | def add_entry(): | 
					
						
							| 
									
										
										
										
											2010-04-15 08:21:46 +08:00
										 |  |  |     if not session.get('logged_in'): | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |         abort(401) | 
					
						
							| 
									
										
										
										
											2012-10-10 03:02:32 +08:00
										 |  |  |     db = get_db() | 
					
						
							|  |  |  |     db.execute('insert into entries (title, text) values (?, ?)', | 
					
						
							| 
									
										
										
										
											2014-04-28 21:18:27 +08:00
										 |  |  |                [request.form['title'], request.form['text']]) | 
					
						
							| 
									
										
										
										
											2012-10-10 03:02:32 +08:00
										 |  |  |     db.commit() | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |     flash('New entry was successfully posted') | 
					
						
							|  |  |  |     return redirect(url_for('show_entries')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @app.route('/login', methods=['GET', 'POST']) | 
					
						
							|  |  |  | def login(): | 
					
						
							|  |  |  |     error = None | 
					
						
							|  |  |  |     if request.method == 'POST': | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  |         if request.form['username'] != app.config['USERNAME']: | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |             error = 'Invalid username' | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  |         elif request.form['password'] != app.config['PASSWORD']: | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |             error = 'Invalid password' | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             session['logged_in'] = True | 
					
						
							|  |  |  |             flash('You were logged in') | 
					
						
							|  |  |  |             return redirect(url_for('show_entries')) | 
					
						
							|  |  |  |     return render_template('login.html', error=error) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @app.route('/logout') | 
					
						
							|  |  |  | def logout(): | 
					
						
							|  |  |  |     session.pop('logged_in', None) | 
					
						
							|  |  |  |     flash('You were logged out') | 
					
						
							|  |  |  |     return redirect(url_for('show_entries')) |