dj-database-url/dj_database_url.py
bencord0 34e1863af3 RedHat's OpenShift platform uses the 'postgresql' scheme.
Note:
Use .config(env=OPENSHIFT_DB_URL) or some other way to set DATABASE_URL=$OPENSHIFT_DB_URL
2012-06-17 16:09:29 +02:00

55 lines
1.2 KiB
Python

# -*- coding: utf-8 -*-
import os
import urlparse
# Register database schemes in URLs.
urlparse.uses_netloc.append('postgres')
urlparse.uses_netloc.append('postgis')
urlparse.uses_netloc.append('mysql')
urlparse.uses_netloc.append('sqlite')
DEFAULT_ENV = 'DATABASE_URL'
def config(env=DEFAULT_ENV, default=None):
"""Returns configured DATABASE dictionary from DATABASE_URL."""
config = {}
s = os.environ.get(env, default)
if s:
config = parse(s)
return config
def parse(url):
"""Parses a database URL."""
config = {}
url = urlparse.urlparse(url)
# Update with environment configuration.
config.update({
'NAME': url.path[1:],
'USER': url.username,
'PASSWORD': url.password,
'HOST': url.hostname,
'PORT': url.port,
})
if url.scheme == 'postgres' or url.scheme == 'postgresql':
config['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
if url.scheme == 'postgis':
config['ENGINE'] = 'django.contrib.gis.db.backends.postgis'
if url.scheme == 'mysql':
config['ENGINE'] = 'django.db.backends.mysql'
if url.scheme == 'sqlite':
config['ENGINE'] = 'django.db.backends.sqlite3'
return config