Permalink
Browse files

Implement support of ``REDIS_URL`` setting.

  • Loading branch information...
1 parent 3d19646 commit f4abf9be9c0d05aa3bdaeb0f50ce844fb6f1e226 @playpauseandstop committed Mar 30, 2012
Showing with 54 additions and 1 deletion.
  1. +32 −1 flask_redis/__init__.py
  2. +22 −0 testapp/tests.py
View
@@ -1,4 +1,5 @@
import inspect
+import urlparse
from redis import Redis as BaseRedis
@@ -14,7 +15,7 @@ class Redis(BaseRedis):
def __init__(self, app):
"""
Overwrite default ``Redis.__init__`` method, read all necessary
- settings from Flask app instead of positional and keyword args.
+ settings from Flask app config instead of positional and keyword args.
The possible settings are:
@@ -28,7 +29,37 @@ def __init__(self, app):
* REDIS_ERRORS
* REDIS_UNIX_SOCKET_PATH
+ Advanced usage
+ --------------
+
+ Also if you want to use this extension on Heroku or other build
+ services where redis URL stored in environment var you could to
+ determine full URL to redis server.
+
+ For example for Heroku apps which used ``REDIS_TO_GO`` environ app,
+ you'll need to update your project settings with::
+
+ import os
+
+ REDIS_URL = 'redis://localhost:6379/0'
+ REDIS_URL = os.environ.get('REDIS_TO_GO', REDIS_URL)
+
"""
+ url = app.config.get('REDIS_URL')
+
+ if url:
+ urlparse.uses_netloc.append('redis')
+ url = urlparse.urlparse(url)
+
+ # URL could contains host, port, user, password and db
+ # values. Store their to config
+ app.config['REDIS_HOST'] = url.hostname
+ app.config['REDIS_PORT'] = url.port
+ app.config['REDIS_USER'] = url.username
+ app.config['REDIS_PASSWORD'] = url.password
+ app.config['REDIS_DB'] = \
+ url.path if url.path.isdigit() else None
+
spec = inspect.getargspec(BaseRedis.__init__)
args = set(spec.args).difference(set(['self']))
kwargs = {}
View
@@ -18,6 +18,9 @@ class TestFlaskRedis(unittest.TestCase):
def setUp(self):
self.old_COUNTER_KEY = app.config['COUNTER_KEY']
+ self.old_REDIS_HOST = app.config['REDIS_HOST']
+ self.old_REDIS_PORT = app.config['REDIS_PORT']
+ self.old_REDIS_DB = app.config['REDIS_DB']
app.config['COUNTER_KEY'] += '_test'
app.config['TESTING'] = True
@@ -31,6 +34,9 @@ def tearDown(self):
redis.delete(app.config['COUNTER_KEY'])
app.config['COUNTER_KEY'] = self.old_COUNTER_KEY
+ app.config['REDIS_HOST'] = self.old_REDIS_HOST
+ app.config['REDIS_PORT'] = self.old_REDIS_PORT
+ app.config['REDIS_DB'] = self.old_REDIS_DB
app.config['TESTING'] = False
def url(self, *args, **kwargs):
@@ -45,6 +51,12 @@ def test_custom_behaviour(self):
obj = Redis(app)
self.assertRaises(ConnectionError, obj.ping)
+ def test_custom_behaviour_url(self):
+ app.config['REDIS_URL'] = 'redis://wrong-host:8080/0'
+
+ obj = Redis(app)
+ self.assertRaises(ConnectionError, obj.ping)
+
def test_default_behaviour(self):
response = self.app.get(self.home_url)
self.assertEqual(response.status_code, 200)
@@ -68,6 +80,16 @@ def test_default_behaviour(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, 'Hello, visitor!')
+ def test_default_behaviour_url(self):
+ host = app.config.pop('REDIS_HOST')
+ port = app.config.pop('REDIS_PORT')
+ db = app.config.pop('REDIS_DB')
+
+ app.config['REDIS_URL'] = 'redis://%s:%d/%d' % (host, port, db)
+
+ obj = Redis(app)
+ obj.ping()
+
if __name__ == '__main__':
unittest.main()

0 comments on commit f4abf9b

Please sign in to comment.