From 303a53eede0c7ce3c24ca3b1aad0a93216c3fa73 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Wed, 20 Apr 2016 08:09:00 +0900 Subject: [PATCH] Return Items in upload and update methods Related to #380 --- radicale/__init__.py | 7 +------ radicale/storage.py | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/radicale/__init__.py b/radicale/__init__.py index a588aa7..6f210a3 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -540,13 +540,8 @@ class Application(object): # Case 1: No item and no ETag precondition: Add new item # Case 2: Item and ETag precondition verified: Modify item # Case 3: Item and no Etag precondition: Force modifying item - xmlutils.put(environ["PATH_INFO"], content, collection) + new_item = xmlutils.put(environ["PATH_INFO"], content, collection) status = client.CREATED - # Try to return the etag in the header. - # If the added item doesn't have the same name as the one given - # by the client, then there's no obvious way to generate an - # etag, we can safely ignore it. - new_item = collection.get(item_name) if new_item: headers["ETag"] = new_item.etag else: diff --git a/radicale/storage.py b/radicale/storage.py index 2f830ff..5bb0f05 100644 --- a/radicale/storage.py +++ b/radicale/storage.py @@ -114,10 +114,9 @@ def path_to_filesystem(root, *paths): class Item: - def __init__(self, item, href, etag, last_modified=None): + def __init__(self, item, href, last_modified=None): self.item = item self.href = href - self.etag = etag self.last_modified = last_modified def __getattr__(self, attr): @@ -265,8 +264,7 @@ class Collection: last_modified = time.strftime( "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(os.path.getmtime(path))) - return Item( - vobject.readOne(text), href, get_etag(text), last_modified) + return Item(vobject.readOne(text), href, last_modified) else: log.LOGGER.debug( "Can't tranlate name safely to filesystem, " @@ -286,22 +284,22 @@ class Collection: """Check if an item exists by its href.""" return self.get(href) is not None - def upload(self, href, item): + def upload(self, href, vobject_item): """Upload a new item.""" # TODO: use returned object in code if is_safe_filesystem_path_component(href): path = path_to_filesystem(self._filesystem_path, href) if not os.path.exists(path): - text = item.serialize() + item = Item(vobject_item, href) with open(path, "w", encoding=STORAGE_ENCODING) as fd: - fd.write(text) - return href, get_etag(text) + fd.write(item.serialize()) + return item else: log.LOGGER.debug( "Can't tranlate name safely to filesystem, " "skipping component: %s", href) - def update(self, href, item, etag=None): + def update(self, href, vobject_item, etag=None): """Update an item.""" # TODO: use etag in code and test it here # TODO: use returned object in code @@ -311,10 +309,10 @@ class Collection: with open(path, encoding=STORAGE_ENCODING) as fd: text = fd.read() if not etag or etag == get_etag(text): - new_text = item.serialize() + item = Item(vobject_item, href) with open(path, "w", encoding=STORAGE_ENCODING) as fd: - fd.write(new_text) - return get_etag(new_text) + fd.write(item.serialize()) + return item else: log.LOGGER.debug( "Can't tranlate name safely to filesystem, "