DELETE requests can delete calendars (closes #514)

This commit is contained in:
Guillaume Ayoub 2011-11-29 17:41:08 +01:00
parent bfe527dd41
commit 1f2f39a87a
3 changed files with 25 additions and 8 deletions

View File

@ -267,8 +267,15 @@ class Application(object):
def delete(self, environ, calendars, content, user):
"""Manage DELETE request."""
calendar = calendars[0]
item = calendar.get_item(
xmlutils.name_from_path(environ["PATH_INFO"], calendar))
if calendar.local_path == environ["PATH_INFO"].strip("/"):
# Path matching the calendar, the item to delete is the calendar
item = calendar
else:
# Try to get an item matching the path
item = calendar.get_item(
xmlutils.name_from_path(environ["PATH_INFO"], calendar))
if item and environ.get("HTTP_IF_MATCH", item.etag) == item.etag:
# No ETag precondition or precondition verified, delete item
answer = xmlutils.delete(environ["PATH_INFO"], calendar)

View File

@ -172,6 +172,7 @@ class Calendar(object):
self.encoding = "utf-8"
split_path = path.split("/")
self.path = os.path.join(FOLDER, path.replace("/", os.sep))
self.props_path = self.path + '.props'
if principal and split_path and os.path.isdir(self.path):
# Already existing principal calendar
self.owner = split_path[0]
@ -300,6 +301,11 @@ class Calendar(object):
self.write(items=items)
def delete(self):
"""Delete the calendar."""
os.remove(self.path)
os.remove(self.props_path)
def remove(self, name):
"""Remove object named ``name`` from calendar."""
components = [
@ -415,16 +421,15 @@ class Calendar(object):
@contextmanager
def props(self):
"""Get the calendar properties."""
props_path = self.path + '.props'
# On enter
properties = {}
if os.path.exists(props_path):
with open(props_path) as prop_file:
if os.path.exists(self.props_path):
with open(self.props_path) as prop_file:
properties.update(json.load(prop_file))
yield properties
# On exit
self._create_dirs(props_path)
with open(props_path, 'w') as prop_file:
self._create_dirs(self.props_path)
with open(self.props_path, 'w') as prop_file:
json.dump(properties, prop_file)
@property

View File

@ -153,7 +153,12 @@ def delete(path, calendar):
"""
# Reading request
calendar.remove(name_from_path(path, calendar))
if calendar.local_path == path.strip("/"):
# Delete the whole calendar
calendar.delete()
else:
# Remove an item from the calendar
calendar.remove(name_from_path(path, calendar))
# Writing answer
multistatus = ET.Element(_tag("D", "multistatus"))