Repository: joke2k/django-environ
Pull request #61 (2015-12-25 17:40:14)

support all django redis packages
See #32 


Patch:
diff --git a/environ/environ.py b/environ/environ.py
index f2077b3..6715730 100644
--- a/environ/environ.py
+++ b/environ/environ.py
@@ -31,6 +31,17 @@ __version__ = tuple(VERSION.split('.'))
 def _cast_int(v):
     return int(v) if hasattr(v, 'isdigit') and v.isdigit() else v
 
+# back compatibility with redis_cache package
+DJANGO_REDIS_DRIVER = 'django_redis.cache.RedisCache'
+DJANGO_REDIS_CACHE_DRIVER = 'redis_cache.RedisCache'
+
+REDIS_DRIVER = DJANGO_REDIS_DRIVER
+try:
+    import redis_cache
+    REDIS_DRIVER = DJANGO_REDIS_CACHE_DRIVER
+except:
+    pass
+
 
 class NoValue(object):
 
@@ -79,8 +90,8 @@ class Env(object):
         'locmemcache': 'django.core.cache.backends.locmem.LocMemCache',
         'memcache': 'django.core.cache.backends.memcached.MemcachedCache',
         'pymemcache': 'django.core.cache.backends.memcached.PyLibMCCache',
-        'rediscache': 'django_redis.cache.RedisCache',
-        'redis': 'django_redis.cache.RedisCache',
+        'rediscache': REDIS_DRIVER,
+        'redis': REDIS_DRIVER,
     }
     _CACHE_BASE_OPTIONS = ['TIMEOUT', 'KEY_PREFIX', 'VERSION', 'KEY_FUNCTION', 'BINARY']
 
@@ -419,15 +430,23 @@ class Env(object):
             'LOCATION': location,
         }
 
+        # Add the drive to LOCATION
         if url.scheme == 'filecache':
             config.update({
                 'LOCATION': url.netloc + url.path,
             })
 
-        if url.path and url.scheme in ['memcache', 'pymemcache', 'rediscache']:
+        if url.path and url.scheme in ['memcache', 'pymemcache']:
             config.update({
                 'LOCATION': 'unix:' + url.path,
             })
+        elif url.scheme.startswith('redis'):
+            if url.hostname:
+                scheme = url.scheme.replace('cache', '')
+            else:
+                scheme = 'unix'
+
+            config['LOCATION'] = scheme + '://' + url.netloc + url.path
 
         if url.query:
             config_options = {}


Test patch:
diff --git a/environ/test.py b/environ/test.py
index 2a088e2..56841af 100644
--- a/environ/test.py
+++ b/environ/test.py
@@ -6,7 +6,7 @@ import unittest
 
 from django.core.exceptions import ImproperlyConfigured
 
-from environ import Env, Path
+from environ import Env, Path, REDIS_DRIVER
 
 
 class BaseTests(unittest.TestCase):
@@ -201,7 +201,7 @@ class EnvTests(BaseTests):
 
         redis_config = self.env.cache_url('CACHE_REDIS')
         self.assertEqual(redis_config['BACKEND'], 'django_redis.cache.RedisCache')
-        self.assertEqual(redis_config['LOCATION'], '127.0.0.1:6379:1')
+        self.assertEqual(redis_config['LOCATION'], 'redis://127.0.0.1:6379:1')
         self.assertEqual(redis_config['OPTIONS'], {
             'CLIENT_CLASS': 'django_redis.client.DefaultClient',
             'PASSWORD': 'secret',
@@ -435,8 +435,8 @@ class CacheTestSuite(unittest.TestCase):
         url = 'rediscache://127.0.0.1:6379:1?client_class=django_redis.client.DefaultClient&password=secret'
         url = Env.cache_url_config(url)
 
-        self.assertEqual(url['BACKEND'], 'django_redis.cache.RedisCache')
-        self.assertEqual(url['LOCATION'], '127.0.0.1:6379:1')
+        self.assertEqual(url['BACKEND'], REDIS_DRIVER)
+        self.assertEqual(url['LOCATION'], 'redis://127.0.0.1:6379:1')
         self.assertEqual(url['OPTIONS'], {
             'CLIENT_CLASS': 'django_redis.client.DefaultClient',
             'PASSWORD': 'secret',
@@ -446,7 +446,22 @@ class CacheTestSuite(unittest.TestCase):
         url = 'rediscache:///path/to/socket:1'
         url = Env.cache_url_config(url)
         self.assertEqual(url['BACKEND'], 'django_redis.cache.RedisCache')
-        self.assertEqual(url['LOCATION'], 'unix:/path/to/socket:1')
+        self.assertEqual(url['LOCATION'], 'unix:///path/to/socket:1')
+
+    def test_redis_with_password_parsing(self):
+        url = 'rediscache://:redispass@127.0.0.1:6379/0'
+        url = Env.cache_url_config(url)
+        self.assertEqual(REDIS_DRIVER, url['BACKEND'])
+        self.assertEqual(url['LOCATION'], 'redis://:redispass@127.0.0.1:6379/0')
+
+    def test_redis_socket_url(self):
+        url = 'redis://:redispass@/path/to/socket.sock?db=0'
+        url = Env.cache_url_config(url)
+        self.assertEqual(REDIS_DRIVER, url['BACKEND'])
+        self.assertEqual(url['LOCATION'], 'unix://:redispass@/path/to/socket.sock')
+        self.assertEqual(url['OPTIONS'], {
+            'DB': 0
+        })
 
     def test_options_parsing(self):
         url = 'filecache:///var/tmp/django_cache?timeout=60&max_entries=1000&cull_frequency=0'