diff options
author | Milo Casagrande <milo.casagrande@linaro.org> | 2014-10-22 15:59:26 +0200 |
---|---|---|
committer | Milo Casagrande <milo.casagrande@linaro.org> | 2014-10-22 15:59:26 +0200 |
commit | 7cf8a824af79c32cfb1cc9236500b87f661c6c00 (patch) | |
tree | 6523f03d8bf0d8a141fa7cb95504e6680b3fae20 /app/handlers/base.py | |
parent | 879f02f86456bab6b018db8f14da798083037994 (diff) |
handlers: Use db connection parameters.
* Make sure the db connection parameters are used when
setting up the connection.
* Fix tests.
Change-Id: Ie811565acfcfc82b937152bdb0078a3632cadea1
Diffstat (limited to 'app/handlers/base.py')
-rw-r--r-- | app/handlers/base.py | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/app/handlers/base.py b/app/handlers/base.py index 4342275..67d936b 100644 --- a/app/handlers/base.py +++ b/app/handlers/base.py @@ -57,6 +57,7 @@ class BaseHandler(RequestHandler): def __init__(self, application, request, **kwargs): super(BaseHandler, self).__init__(application, request, **kwargs) + self._db = None @property def executor(self): @@ -71,7 +72,19 @@ class BaseHandler(RequestHandler): @property def db(self): """The database instance associated with the object.""" - return self.settings['client'][DB_NAME] + if self._db is None: + db_options = self.settings['dboptions'] + client = self.settings['client'] + + db_pwd = db_options['dbpassword'] + db_user = db_options['dbuser'] + + self._db = client[DB_NAME] + + if all([db_user, db_pwd]): + self._db.authenticate(db_user, password=db_pwd) + + return self._db @property def log(self): @@ -198,6 +211,7 @@ class BaseHandler(RequestHandler): if is_valid_json(json_obj, self._valid_keys('POST')): kwargs['json_obj'] = json_obj + kwargs['db_options'] = self.settings['dboptions'] response = self._post(*args, **kwargs) else: response = HandlerResponse(400) @@ -242,6 +256,8 @@ class BaseHandler(RequestHandler): This method will receive a named argument containing the JSON object with the POST request data. The argument is called `json_obj`. + It will also receive a named argument containing the mongodb database + connection parameters. The argument is called `db_options`. :return A `HandlerResponse` object. """ |