From 0376910f3392ee58fa57558458e9f0aa363e2cbc Mon Sep 17 00:00:00 2001 From: Dan Crosta Date: Tue, 17 Apr 2012 19:47:54 -0400 Subject: [PATCH] refactor get_connection In the previous version, the requested ReadPreference was ignored in the case that the user specified a MongoDB URI. This rearranges the code to ensure that only those values which we explicitly parse out of the URI override values set as keyword arguments. This leaves open the possibility of conflicts between the URI and the kwargs -- we should consider whether to raise an exception if, e.g., username is specified as a kwarg *and* in the URI. --- mongoengine/connection.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/mongoengine/connection.py b/mongoengine/connection.py index 9cf8264a..96b8100d 100644 --- a/mongoengine/connection.py +++ b/mongoengine/connection.py @@ -39,22 +39,7 @@ def register_connection(alias, name, host='localhost', port=27017, """ global _connection_settings - # Handle uri style connections - if "://" in host: - uri_dict = uri_parser.parse_uri(host) - if uri_dict.get('database') is None: - raise ConnectionError("If using URI style connection include "\ - "database name in string") - _connection_settings[alias] = { - 'host': host, - 'name': uri_dict.get('database'), - 'username': uri_dict.get('username'), - 'password': uri_dict.get('password') - } - _connection_settings[alias].update(kwargs) - return - - _connection_settings[alias] = { + conn_settings = { 'name': name, 'host': host, 'port': port, @@ -64,7 +49,22 @@ def register_connection(alias, name, host='localhost', port=27017, 'password': password, 'read_preference': read_preference } - _connection_settings[alias].update(kwargs) + + # Handle uri style connections + if "://" in host: + uri_dict = uri_parser.parse_uri(host) + if uri_dict.get('database') is None: + raise ConnectionError("If using URI style connection include "\ + "database name in string") + conn_settings.update({ + 'host': host, + 'name': uri_dict.get('database'), + 'username': uri_dict.get('username'), + 'password': uri_dict.get('password'), + 'read_preference': read_preference, + }) + + _connection_settings[alias] = conn_settings def disconnect(alias=DEFAULT_CONNECTION_NAME):