jazzband / dj-database-url

Use Database URLs in your Django Application.
https://pypi.org/project/dj-database-url/
BSD 3-Clause "New" or "Revised" License
1.49k stars 205 forks source link

Add the URL that was used to the returned config? #12

Closed DannyGoodall closed 8 years ago

DannyGoodall commented 12 years ago

Would it be possible to return the URL that was used in the returned config?

dj-database-url currently looks for an environment variable and if not present it uses a default URL. I'd like to know the URL that was parsed by dj-database-url.

This allows the routine to both parse for Django DATABASE components, as well as a being a general routine for getting a URL from the environment or falling back to a default if it doesn't exist - MONGOLAB_URI, etc.

The code change seems minor. Something like...

def parse(url):
    """Parses a database URL."""

    #--->Initialise config with URL key instead of {}

    config = {
        'URL': url
    }

    url = urlparse.urlparse(url)

    # Remove query strings.
    path = url.path[1:]
    path = path.split('?', 2)[0]

    # Update with environment configuration.
    config.update({
        'NAME': path,
        'USER': url.username,
        'PASSWORD': url.password,
        'HOST': url.hostname,
        'PORT': url.port,
    })

    if url.scheme in SCHEMES:
        config['ENGINE'] = SCHEMES[url.scheme]

    return config

Apologies for not creating a pull request but I'm not completely au fait with github - not a 'real' programmer you see.

Thanks for your time.

kennethreitz commented 8 years ago

Thanks for this contribution! However, I'm going to pass on it, for now.