﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
10492	Database connection string	lao	nobody	"I propose to use a connection string, instead of DATABASE_* settings 
{{{
# settings.py
...
DATABASE = 'postgresql://djangouser:djangopassword@host/djnagodb'
DATABASE_OPTIONS = {
    ...
}
}}}

Getting connection settings from '''someapp''', instead
{{{
#!python
# someapp.py
from django.conf import settings
dbname = settings.DATABASE_NAME
# or 
from django.db import settings
dbname = settings.DATABASE_NAME
}}}
using
{{{
#!python
# someapp.py
from django.db import db # or some other object
dbname = db.name
}}}
----
'''Example'''
(sory for long regex)
{{{
#!python
from re import compile

class DBConectionStr:
    """"""
    Formats:
        sqlite3://path/to/file
        dbtype://dbuser/dbname
        dbtype://dbuser:dbpassword/dbname
        dbtype://dbuser:dbpassword@dbhost/dbname 
        dbtype://dbuser:dbpassword@dbhost:port/dbname
        dbtype://dbuser(ROLE):dbpassword@dbhost/dbname
    Example:
       c = DBConection('sqlite3:///tmp/db.sqlite3') 
    """"""
    def __init__(self, cstr):
        __re_dbstr = compile(r'^(?P<engine>[^:]+)://((?P<user>[^\(:]+)(\((?P<role>.+)\))?(:(?P<password>[^@]+))?(@(?P<host>[^\/:]+)(:(?P<port>\d+))?)?)?/(?P<name>.+)')
        try:
            self.__db = __re_dbstr.search(cstr).groupdict()
            # Fix for sqlite path
            if self.__db['engine'].startswith('sqlite'):
                self.__db['name'] = ""/%s"" % self.__db['name']
        except:
            self.__db = {}
            #raise SomeException()

    def keys(self):
       return self.__db.keys()

    def items(self):
       return self.__db.items()

    def __getitem__(self, key):
        return self.__db.get(key, None)

    def __getattr__(self, key):
        return self.__db.get(key, None)

#c = DBConectionStr('sqlite3:///:memory:')
#c = DBConectionStr('sqlite3:///tmp/django.sqlite3')
#c = DBConectionStr('mysql://user1/django')
#c = DBConectionStr('mysql://user1:password1/django')
#c = DBConectionStr('mysql://user1:password1@host1/django') 
#c = DBConectionStr('mysql://user1:password1@host1:1234/django')
#c = DBConectionStr('postgresql://user1(role1):password1@host1/django')
c = DBConectionStr('postgresql://user1(role1):password1@host1:1234/django')
for i in c.items():
    print ""%-8s: %s"" % i
# or
#print c.engine, c.name 
}}}

'''Output'''
{{{
engine  : postgresql
name    : django
host    : host1
role    : role1
user    : user1
password: password1
port    : 1234
}}}"		closed	Database layer (models, ORM)			wontfix			Unreviewed	0	0	0	0	0	0
