Return new item from webdave put call
authorKeith Packard <keithp@keithp.com>
Tue, 6 May 2014 23:58:22 +0000 (16:58 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 6 May 2014 23:58:22 +0000 (16:58 -0700)
This lets the replace and append functions also return the new object,
so that do_PUT doesn't have to guess about what the object is named

Signed-off-by: Keith Packard <keithp@keithp.com>
calypso/__init__.py
calypso/webdav.py
calypso/xmlutils.py

index ba53288e2d2a2a9bc2609923fe41353f0b9f701d..1ad82f4cb99fd178d5154409f91ee3cc4496c98b 100644 (file)
@@ -411,12 +411,10 @@ class CollectionHTTPHandler(server.BaseHTTPRequestHandler):
                 # Case 2: Item and ETag precondition verified: Modify item
                 # Case 3: Item and no Etag precondition: Force modifying item
                 webdav_request = self._decode(self.xml_request)
-                xmlutils.put(self.path, webdav_request, self._collection, context=context)
+                new_item = xmlutils.put(self.path, webdav_request, self._collection, context=context)
                 
-                new_name = paths.resource_from_path(self.path)
-                log.debug("item_name %s new_name %s", item_name, new_name)
-                # We need to double get this item, because it just got created
-                etag = self._collection.get_item(new_name).etag
+                log.debug("item_name %s new_name %s", item_name, new_item.name)
+                etag = new_item.etag
                 #log.debug("replacement etag %s", etag)
 
                 self.send_calypso_response(client.CREATED, 0)
index 2292841d25040f7930d1f88d0032701b637f41bd..c23d4e4b8311deecf9dbad7afff5997f6e50d6ca 100644 (file)
@@ -442,6 +442,7 @@ class Collection(object):
 
         """
 
+        self.log.debug('append name %s', name)
         try:
             new_item = Item(text, name, None)
         except Exception, e:
@@ -452,6 +453,7 @@ class Collection(object):
             raise CalypsoError(new_item.name, "Item already present")
         self.log.debug("New item %s", new_item.name)
         self.create_file(new_item, context=context)
+        return new_item
 
     def remove(self, name, context):
         """Remove object named ``name`` from collection."""
@@ -476,10 +478,13 @@ class Collection(object):
 
         ret = False
         if path is not None:
+            self.log.debug('rewrite path %s', path)
             self.rewrite_file(new_item, context=context)
         else:
+            self.log.debug('remove and append item %s', name)
             self.remove(name)
             self.append(name, text, context=context)
+        return new_item
 
     def import_item(self, new_item, path):
         old_item = self.get_item(new_item.name)
index a1e8c8c0a15cad03d12da96724f6e939811663f8..89ed70d97b492a79e8d83282d966cffd5add90a3 100644 (file)
@@ -222,13 +222,15 @@ def propfind(path, xml_request, collection, depth):
 def put(path, webdav_request, collection, context):
     """Read PUT requests."""
     name = paths.resource_from_path(path)
+    log.debug('xmlutils put path %s name %s', path, name)
     if name in (item.name for item in collection.items):
         # PUT is modifying an existing item
-        collection.replace(name, webdav_request, context=context)
+        log.debug('Replacing item named %s', name)
+        return collection.replace(name, webdav_request, context=context)
     else:
         # PUT is adding a new item
         log.debug('Putting a new item, because name %s is not known', name)
-        collection.append(name, webdav_request, context=context)
+        return collection.append(name, webdav_request, context=context)
 
 
 def match_filter_element(vobject, fe):