Permalink
Browse files

Added uri style connection handling

  • Loading branch information...
rozza committed Jan 30, 2012
1 parent f59aa92 commit 0301135f96be83ee613664e875409a9d4b590137
Showing with 41 additions and 6 deletions.
  1. +1 −0 docs/changelog.rst
  2. +7 −0 docs/guide/connecting.rst
  3. +20 −6 mongoengine/connection.py
  4. +13 −0 tests/connection.py
View
@@ -5,6 +5,7 @@ Changelog
Changes in dev
==============
- Added uri support for connections
- Added scalar for efficiently returning partial data values (aliased to values_list)
- Fixed limit skip bug
- Improved Inheritance / Mixin
@@ -20,6 +20,13 @@ provide :attr:`host` and :attr:`port` arguments to
connect('project1', host='192.168.1.35', port=12345)
Uri style connections are also supported as long as you include the database
name - just supply the uri as the :attr:`host` to
:func:`~mongoengine.connect`::
connect('project1', host='mongodb://localhost/database_name')
Multiple Databases
==================
View
@@ -1,5 +1,5 @@
import pymongo
from pymongo import Connection
from pymongo import Connection, uri_parser
__all__ = ['ConnectionError', 'connect', 'register_connection',
@@ -38,6 +38,17 @@ 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 'database' not in uri_dict:
raise ConnectionError("If using URI style connection include "\
"database name in string")
uri_dict['name'] = uri_dict.get('database')
_connection_settings[alias] = uri_dict
return
_connection_settings[alias] = {
'name': name,
'host': host,
@@ -48,8 +59,10 @@ def register_connection(alias, name, host='localhost', port=27017,
'password': password,
'read_preference': read_preference
}
_connection_settings[alias].update(kwargs)
def disconnect(alias=DEFAULT_CONNECTION_NAME):
global _connections
global _dbs
@@ -83,11 +96,12 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
conn_settings.pop('password')
else:
# Get all the slave connections
slaves = []
for slave_alias in conn_settings['slaves']:
slaves.append(get_connection(slave_alias))
conn_settings['slaves'] = slaves
conn_settings.pop('read_preference')
if 'slaves' in conn_settings:
slaves = []
for slave_alias in conn_settings['slaves']:
slaves.append(get_connection(slave_alias))
conn_settings['slaves'] = slaves
conn_settings.pop('read_preference')
try:
_connections[alias] = Connection(**conn_settings)
View
@@ -30,6 +30,19 @@ def test_connect(self):
conn = get_connection('testdb')
self.assertTrue(isinstance(conn, pymongo.connection.Connection))
def test_connect_uri(self):
"""Ensure that the connect() method works properly with uri's
"""
connect("testdb_uri", host='mongodb://username:password@localhost/mongoenginetest')
conn = get_connection()
self.assertTrue(isinstance(conn, pymongo.connection.Connection))
db = get_db()
self.assertTrue(isinstance(db, pymongo.database.Database))
self.assertEqual(db.name, 'mongoenginetest')
def test_register_connection(self):
"""Ensure that connections with different aliases may be registered.
"""

0 comments on commit 0301135

Please sign in to comment.