<reponame>avsej/build-infra<filename>builddb_rest/builddb_rest/couch_db.py
"""
Module to give access to build database connection via requests
in Cornice; yes, the method MUST be called 'includeme'
"""

import cbbuild.cbutil.db as cbutil_db


def includeme(config):
    """
    Set up database connection and make it accessible to all
    requests generated via Cornice
    """

    settings = config.registry.settings

    # Store DB connection in registry
    db_info = {
        'db_uri': settings['builddb_uri'],
        'username': settings['builddb_user'],
        'password': settings['<PASSWORD>'],
    }
    conn = cbutil_db.CouchbaseDB(db_info)
    settings['db_conn'] = conn

    # Make DB connection accessible as a request property
    def _get_db(request):
        _settings = request.registry.settings
        db = _settings['db_conn']

        return db

    config.set_request_property(_get_db, 'db', reify=True)
