Added CollectionManager, made connection module

All connection-related functions are now in connection.py.

Created a ConnectionManager class for interacting with a collection
in the database. Top-level document classes have an instance of
a ConnectionManager (Document.collection).

Defined a 'save' method on top-level document's that uses the collection
manager's '_save_document' method to save the document to the database.

Added tests for CollectionManagers -- all unit tests now require a valid
connection to the database, which is set up in the tests' setUp method.
This commit is contained in:
Harry Marr
2009-11-18 19:02:57 +00:00
parent 688fd5af66
commit c99f5c4ec1
9 changed files with 173 additions and 36 deletions

18
mongomap/collection.py Normal file
View File

@@ -0,0 +1,18 @@
from connection import _get_db
class CollectionManager(object):
def __init__(self, document):
"""Set up the collection manager for a specific document.
"""
db = _get_db()
self._document = document
self._collection_name = document._meta['collection']
# This will create the collection if it doesn't exist
self._collection = db[self._collection_name]
self._id_field = document._meta['object_id_field']
def _save_document(self, document):
"""Save the provided document to the collection.
"""
_id = self._collection.save(document)