remosu avatar remosu committed e7f7a0f

added the possibility of using client-side ids for models

Comments (0)

Files changed (3)

 CONNECTION = pymongo.Connection()
 DATABASE = "backmongo"
 DB = CONNECTION[DATABASE]
+OID_PREFIX = 'oid_'
+
+def clean_id(id):
+    if id.startswith(OID_PREFIX):
+        return ObjectId(id[len(OID_PREFIX):])
+    else:
+        return id
 
 def dict_to_json_converter(obj, **kwargs):
     if isinstance(obj, ObjectId):
-        return str(obj)
+        return OID_PREFIX + str(obj)
     else:
         return json_util.default(obj, **kwargs)
 
 def read(collection_name, id=None):
     if id is not None:
-        result = DB[collection_name].find_one(ObjectId(id))
+        result = DB[collection_name].find_one(clean_id(id))
     else:
         result = list(DB[collection_name].find())
     
     return json.dumps(result, default=dict_to_json_converter)
 
 def delete(collection_name, id):
-    DB[collection_name].remove(ObjectId(id))
+    DB[collection_name].remove(clean_id(id))
 
 def save(collection_name, model, id=None):
     if id is not None:
-        model['_id'] = ObjectId(model['_id'])
+        model['_id'] = clean_id(model['_id'])
     return DB[collection_name].save(model, manipulate=False)

test/acceptance/backmongo.js

         it("has this models", function(){
             expected = [
                 {
-                    "_id": "4f6b75e052ad624ff62cfc8b",
+                    "_id": "1",
                     "name": "manolo",
                     "age": 36
                 },
                 {
-                    "_id": "4f6b75e952ad624ff62cfc8c",
+                    "_id": "2",
                     "name": "silvio",
                     "age": 31
                 }
         var model;
 
         before(function(done){
-            model = new BMModel({_id: '4f6b75e952ad624ff62cfc8c'});
+            model = new BMModel({_id: '2'});
             model.fetch({success: function(){done();}});
         });
 
             var model = bmcollection.find(function(model){
                 return model.get('name') == 'paco';
             });
-            should.exist(model);     
+            should.exist(model);
+            model.id.substr(0, 4).should.equal('oid_');     
         });
     });
 
         });
 
         before(function(){
-            var model = bmcollection.get('4f6b75e952ad624ff62cfc8c');
+            var model = bmcollection.get('2');
             model.destroy({success: function(){done();}});
         });
 
 
         it("deletes the right one", function(){
             var model = bmcollection.find(function(model){
-                return model.get('_id') == '4f6b75e952ad624ff62cfc8c';
+                return model.get('_id') == '2';
             });
             should.not.exist(model);     
         });
         });
 
         before(function(){
-            var model = bmcollection.get('4f6b75e052ad624ff62cfc8b');
+            var model = bmcollection.get('1');
             model.set('name', 'pepito');
             model.save({success: function(){done();}});
         });
 
         it("update the right one", function(){
             var model = bmcollection.find(function(model){
-                return model.id == '4f6b75e052ad624ff62cfc8b' && 
+                return model.id == '1' && 
                     model.get('name') == 'pepito' && model.get('age') == 36;
             });
             should.exist(model);     

test/acceptance/mongodb_setup.py

 CONNECTION[DATABASE].drop_collection("collection_tests")
 collection_tests = CONNECTION[DATABASE]['collection_tests']
 
-collection_tests.insert({'_id': ObjectId('4f6b75e052ad624ff62cfc8b'), 'name':'manolo', 'age':36})
-collection_tests.insert({'_id': ObjectId('4f6b75e952ad624ff62cfc8c'), 'name':'silvio', 'age':31})
+collection_tests.insert({'_id': '1', 'name':'manolo', 'age':36})
+collection_tests.insert({'_id': '2', 'name':'silvio', 'age':31})
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.