[Checkins] SVN: mongopersist/trunk/ - When adding an item to ``MongoContainer`` and the key is ``None``, then the

Stephen Richter cvs-admin at zope.org
Mon Mar 12 02:05:06 UTC 2012


Log message for revision 124575:
  - When adding an item to ``MongoContainer`` and the key is ``None``, then the
    OID is chosen as the key. Ids are perfect key, because they are guaranteed
    to be unique within the collection.
  
  

Changed:
  U   mongopersist/trunk/CHANGES.txt
  U   mongopersist/trunk/src/mongopersist/serialize.py
  U   mongopersist/trunk/src/mongopersist/zope/container.py
  U   mongopersist/trunk/src/mongopersist/zope/tests/test_container.py

-=-
Modified: mongopersist/trunk/CHANGES.txt
===================================================================
--- mongopersist/trunk/CHANGES.txt	2012-03-11 19:12:32 UTC (rev 124574)
+++ mongopersist/trunk/CHANGES.txt	2012-03-12 02:05:02 UTC (rev 124575)
@@ -46,6 +46,10 @@
   container removal is ``_m_remove_documents`` is ``True``. The default is
   ``True``.
 
+- When adding an item to ``MongoContainer`` and the key is ``None``, then the
+  OID is chosen as the key. Ids are perfect key, because they are guaranteed
+  to be unique within the collection.
+
 - Removed ``fields`` argument from the ``MongoContainer.find(...)`` and
   ``MongoContainer.find_one(...)`` methods, since it was not used.
 

Modified: mongopersist/trunk/src/mongopersist/serialize.py
===================================================================
--- mongopersist/trunk/src/mongopersist/serialize.py	2012-03-11 19:12:32 UTC (rev 124574)
+++ mongopersist/trunk/src/mongopersist/serialize.py	2012-03-12 02:05:02 UTC (rev 124575)
@@ -293,7 +293,7 @@
                         klass = self.simple_resolve(name_map_item['path'])
                         break
                 else:
-                    raise ImportError(path)
+                    raise ImportError(dbref)
             OID_CLASS_LRU[dbref.id] = klass
             return klass
 
@@ -379,8 +379,9 @@
             coll = self._jar._get_collection(
                 obj._p_oid.database, obj._p_oid.collection)
             doc = coll.find_one({'_id': obj._p_oid.id})
-            doc.pop('_id')
-            doc.pop('_py_persistent_type', None)
+        # Remove unwanted attributes.
+        doc.pop('_id')
+        doc.pop('_py_persistent_type', None)
         # Store the serial, if conflict detection is enabled.
         if self._jar.detect_conflicts:
             obj._p_serial = p64(doc.pop('_py_serial', 0))

Modified: mongopersist/trunk/src/mongopersist/zope/container.py
===================================================================
--- mongopersist/trunk/src/mongopersist/zope/container.py	2012-03-11 19:12:32 UTC (rev 124574)
+++ mongopersist/trunk/src/mongopersist/zope/container.py	2012-03-12 02:05:02 UTC (rev 124575)
@@ -175,9 +175,16 @@
         setattr(value, self._m_mapping_key, key)
         if self._m_parent_key is not None:
             setattr(value, self._m_parent_key, self._m_get_parent_key_value())
-        self._m_jar.insert(value)
 
     def __setitem__(self, key, value):
+        # Make sure the value is in the database, since we might want to use
+        # its oid.
+        if value._p_oid is None:
+            self._m_jar.insert(value)
+        # When the key is None, we use the object is as name.
+        if key is None:
+            key = unicode(value._p_oid.id)
+        # We want to be as close as possible to using the Zope semantics.
         contained.setitem(self, self._real_setitem, key, value)
 
     def __delitem__(self, key):

Modified: mongopersist/trunk/src/mongopersist/zope/tests/test_container.py
===================================================================
--- mongopersist/trunk/src/mongopersist/zope/tests/test_container.py	2012-03-11 19:12:32 UTC (rev 124574)
+++ mongopersist/trunk/src/mongopersist/zope/tests/test_container.py	2012-03-12 02:05:02 UTC (rev 124575)
@@ -321,6 +321,24 @@
       <Person Adam>
 """
 
+def doctest_MongoContainer_setitem_with_no_key():
+    """MongoContainer: __setitem__(None, obj)
+
+    Whenever an item is added with no key, the OID is used.
+
+      >>> transaction.commit()
+      >>> dm.root['people'] = container.MongoContainer('person')
+      >>> dm.root['people'][None] = Person(u'Stephan')
+
+    Let's now search and receive documents as result:
+
+      >>> sorted(dm.root['people'].keys())
+      [u'...']
+      >>> stephan = dm.root['people'].values()[0]
+      >>> stephan.__name__ == str(stephan._p_oid.id)
+      True
+"""
+
 def doctest_MongoContainer_find():
     """MongoContainer: find
 



More information about the checkins mailing list