Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

safe remove

  • Loading branch information...
commit b23b16d5ec7e3b8df71c9b2d26acc50c191d8b70 1 parent b4bb5a7
Kristina Chodorow authored
Showing with 54 additions and 14 deletions.
  1. +1 −2  README.md
  2. +17 −12 handlers.py
  3. +36 −0 t/post.py
View
3  README.md
@@ -238,6 +238,7 @@ Required arguments: none
Optional arguments:
* `criteria=criteria_for_removal` (object)
+* `safe=bool` (1 or 0)
Returns: `{"ok" : 1}`
@@ -247,8 +248,6 @@ Remove all documents where the "x" field is 2.
curl --data 'criteria={"x":2}' 'http://localhost:27080/foo/bar/_remove'
-TODO: just one, safe mode
-
#### Commands
http://localhost:27080/dbname/_cmd
View
29 handlers.py
@@ -291,6 +291,19 @@ def _insert(self, args, out, name = None, db = None, collection = None):
out(json.dumps(result, default=json_util.default))
+
+ def __safety_check(self, args, out, db):
+ safe = False
+ if "safe" in args:
+ safe = bool(args.getvalue("safe"));
+
+ if safe:
+ result = db.last_status()
+ out(json.dumps(result, default=json_util.default))
+ else:
+ out('{"ok" : 1}')
+
+
def _update(self, args, out, name = None, db = None, collection = None):
"""
update a doc
@@ -333,15 +346,7 @@ def _update(self, args, out, name = None, db = None, collection = None):
conn[db][collection].update(criteria, newobj, upsert=upsert, multi=multi)
- safe = False
- if "safe" in args:
- safe = bool(args.getvalue("safe"));
-
- if safe:
- result = conn[db].last_status()
- out(json.dumps(result, default=json_util.default))
- else:
- out('{"ok" : 1}')
+ self.__safety_check(args, out, conn[db]);
def _remove(self, args, out, name = None, db = None, collection = None):
"""
@@ -359,7 +364,7 @@ def _remove(self, args, out, name = None, db = None, collection = None):
if db == None or collection == None:
out('{"ok" : 0, "errmsg" : "db and collection must be defined"}')
- return
+ return
criteria = {}
if "criteria" in args:
@@ -367,6 +372,6 @@ def _remove(self, args, out, name = None, db = None, collection = None):
if criteria == None:
return
- conn[db][collection].remove(criteria)
- out('{"ok" : 1}')
+ result = conn[db][collection].remove(criteria)
+ self.__safety_check(args, out, conn[db]);
View
36 t/post.py
@@ -185,6 +185,42 @@ def test_multi(self):
self.assertEquals(obj['ok'], 1, str)
self.assertEquals(obj['n'], 3, str)
+ def test_remove(self):
+ POST("http://localhost:27080/test/mongoose/_insert",
+ params = {"docs" : '[{"x" : 1},{"x" : 1},{"x" : 1},{"y" : 1}]'},
+ async = False )
+
+ str = POST("http://localhost:27080/test/mongoose/_remove",
+ async = False )
+
+ obj = json.loads(str)
+
+ self.assertEquals(obj['ok'], 1, str)
+
+
+ def test_remove_safe(self):
+ POST("http://localhost:27080/test/mongoose/_insert",
+ params = {"docs" : '[{"x" : 1},{"x" : 1},{"x" : 1},{"y" : 1}]'},
+ async = False )
+
+ str = POST("http://localhost:27080/test/mongoose/_remove",
+ params = {"criteria" : '{"x" : 1}', "safe" : 1},
+ async = False )
+
+ obj = json.loads(str)
+
+ self.assertEquals(obj['ok'], 1, str)
+ self.assertEquals(obj['n'], 3, str)
+
+ str = POST("http://localhost:27080/test/mongoose/_remove",
+ params = {"safe" : "1"},
+ async = False )
+
+ obj = json.loads(str)
+
+ self.assertEquals(obj['ok'], 1, str)
+ self.assertEquals(obj['n'], 1, str)
+
if __name__ == '__main__':
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.