diff --git a/dj_database_url.py b/dj_database_url.py new file mode 100644 index 0000000..bffc404 --- /dev/null +++ b/dj_database_url.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- + +""" +dj-database-url +~~~~~~~~~~~~~~~ + +A swift samaurai blade for you Django applications. + +""" + + +import os +import urlparse + +# Register database schemes in URLs. +urlparse.uses_netloc.append('postgres') +urlparse.uses_netloc.append('mysql') + + +def config(config=None): + """Returns configured DATABASES dictionary.""" + + if config is None: + config = {} + + if 'DATABASE_URL' in os.environ: + url = urlparse.urlparse(os.environ['DATABASE_URL']) + + # Ensure default database exists. + config.setdefault('default', {}) + + # Update with environment configuration. + config['default'].update({ + 'NAME': url.path[1:], + 'USER': url.username, + 'PASSWORD': url.password, + 'HOST': url.hostname, + 'PORT': url.port, + }) + + if url.scheme == 'postgres': + config['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2' + + if url.scheme == 'mysql': + config['default']['ENGINE'] = 'django.db.backends.mysql' + + return config \ No newline at end of file