mirror of
https://github.com/jazzband/dj-database-url.git
synced 2026-03-16 22:20:24 +00:00
parse / config
This commit is contained in:
parent
7462e79d51
commit
37340e253d
1 changed files with 24 additions and 14 deletions
|
|
@ -10,26 +10,36 @@ urlparse.uses_netloc.append('mysql')
|
|||
DEFAULT_ENV = 'DATABASE_URL'
|
||||
|
||||
def config(env=DEFAULT_ENV):
|
||||
"""Returns configured DATABASES dictionary."""
|
||||
"""Returns configured DATABASE dictionary from DATABASE_URL."""
|
||||
|
||||
config = {}
|
||||
|
||||
if env in os.environ:
|
||||
url = urlparse.urlparse(os.environ[env])
|
||||
config = parse(os.environ[env])
|
||||
|
||||
# Update with environment configuration.
|
||||
config.update({
|
||||
'NAME': url.path[1:],
|
||||
'USER': url.username,
|
||||
'PASSWORD': url.password,
|
||||
'HOST': url.hostname,
|
||||
'PORT': url.port,
|
||||
})
|
||||
return config
|
||||
|
||||
if url.scheme == 'postgres':
|
||||
config['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
|
||||
|
||||
if url.scheme == 'mysql':
|
||||
config['ENGINE'] = 'django.db.backends.mysql'
|
||||
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.passwigord,
|
||||
'HOST': url.hostname,
|
||||
'PORT': url.port,
|
||||
})
|
||||
|
||||
if url.scheme == 'postgres':
|
||||
config['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
|
||||
|
||||
if url.scheme == 'mysql':
|
||||
config['ENGINE'] = 'django.db.backends.mysql'
|
||||
|
||||
return config
|
||||
Loading…
Reference in a new issue