mirror of
https://github.com/jazzband/dj-database-url.git
synced 2026-03-16 22:20:24 +00:00
simple enough
This commit is contained in:
parent
1d3e8ccd5a
commit
905db96bbb
1 changed files with 47 additions and 0 deletions
47
dj_database_url.py
Normal file
47
dj_database_url.py
Normal file
|
|
@ -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
|
||||
Loading…
Reference in a new issue