Ported examples over to new config. documented upgrading

This commit is contained in:
Armin Ronacher 2010-05-27 21:17:25 +02:00
parent 02b916d509
commit dfecc86dd3
17 changed files with 122 additions and 39 deletions

View file

@ -10,7 +10,9 @@
~ How do I use it?
1. edit the configuration in the flaskr.py file
1. edit the configuration in the flaskr.py file or
export an FLASKR_SETTINGS environment variable
pointing to a configuration file.
2. fire up a python shell and run this:

View file

@ -24,13 +24,13 @@ PASSWORD = 'default'
# create our little application :)
app = Flask(__name__)
app.secret_key = SECRET_KEY
app.debug = DEBUG
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
def connect_db():
"""Returns a new connection to the database."""
return sqlite3.connect(DATABASE)
return sqlite3.connect(app.config['DATABASE'])
def init_db():
@ -76,9 +76,9 @@ def add_entry():
def login():
error = None
if request.method == 'POST':
if request.form['username'] != USERNAME:
if request.form['username'] != app.config['USERNAME']:
error = 'Invalid username'
elif request.form['password'] != PASSWORD:
elif request.form['password'] != app.config['PASSWORD']:
error = 'Invalid password'
else:
session['logged_in'] = True

View file

@ -18,14 +18,14 @@ class FlaskrTestCase(unittest.TestCase):
def setUp(self):
"""Before each test, set up a blank database"""
self.db_fd, flaskr.DATABASE = tempfile.mkstemp()
self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
self.app = flaskr.app.test_client()
flaskr.init_db()
def tearDown(self):
"""Get rid of the database again after each test."""
os.close(self.db_fd)
os.unlink(flaskr.DATABASE)
os.unlink(flaskr.app.config['DATABASE'])
def login(self, username, password):
return self.app.post('/login', data=dict(
@ -45,18 +45,22 @@ class FlaskrTestCase(unittest.TestCase):
def test_login_logout(self):
"""Make sure login and logout works"""
rv = self.login(flaskr.USERNAME, flaskr.PASSWORD)
rv = self.login(flaskr.app.config['USERNAME'],
flaskr.app.config['PASSWORD'])
assert 'You were logged in' in rv.data
rv = self.logout()
assert 'You were logged out' in rv.data
rv = self.login(flaskr.USERNAME + 'x', flaskr.PASSWORD)
rv = self.login(flaskr.app.config['USERNAME'] + 'x',
flaskr.app.config['PASSWORD'])
assert 'Invalid username' in rv.data
rv = self.login(flaskr.USERNAME, flaskr.PASSWORD + 'x')
rv = self.login(flaskr.app.config['USERNAME'],
flaskr.app.config['PASSWORD'] + 'x')
assert 'Invalid password' in rv.data
def test_messages(self):
"""Test that messages work"""
self.login(flaskr.USERNAME, flaskr.PASSWORD)
self.login(flaskr.app.config['USERNAME'],
flaskr.app.config['PASSWORD'])
rv = self.app.post('/add', data=dict(
title='<Hello>',
text='<strong>HTML</strong> allowed here'

View file

@ -10,7 +10,9 @@
~ How do I use it?
1. edit the configuration in the minitwit.py file
1. edit the configuration in the minitwit.py file or
export an MINITWIT_SETTINGS environment variable
pointing to a configuration file.
2. fire up a python shell and run this:

View file

@ -27,11 +27,13 @@ SECRET_KEY = 'development key'
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('MINITWIT_SETTINGS', silent=True)
def connect_db():
"""Returns a new connection to the database."""
return sqlite3.connect(DATABASE)
return sqlite3.connect(app.config['DATABASE'])
def init_db():
@ -237,12 +239,9 @@ def logout():
return redirect(url_for('public_timeline'))
# add some filters to jinja and set the secret key and debug mode
# from the configuration.
# add some filters to jinja
app.jinja_env.filters['datetimeformat'] = format_datetime
app.jinja_env.filters['gravatar'] = gravatar_url
app.secret_key = SECRET_KEY
app.debug = DEBUG
if __name__ == '__main__':

View file

@ -18,14 +18,14 @@ class MiniTwitTestCase(unittest.TestCase):
def setUp(self):
"""Before each test, set up a blank database"""
self.db_fd, minitwit.DATABASE = tempfile.mkstemp()
self.db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp()
self.app = minitwit.app.test_client()
minitwit.init_db()
def tearDown(self):
"""Get rid of the database again after each test."""
os.close(self.db_fd)
os.unlink(minitwit.DATABASE)
os.unlink(minitwit.app.config['DATABASE'])
# helper functions