Merge pull request #455 from Unrud/home

Creating addressbooks and calendars in DAVdroid
This commit is contained in:
Guillaume Ayoub 2016-08-02 13:51:29 +02:00 committed by GitHub
commit 04010ff8d3
3 changed files with 33 additions and 13 deletions

View File

@ -302,6 +302,23 @@ class Application:
is_authenticated = self.is_authenticated(user, password) is_authenticated = self.is_authenticated(user, password)
is_valid_user = is_authenticated or not user is_valid_user = is_authenticated or not user
# Create principal collection
if user and is_authenticated:
principal_path = "/%s/" % user
if self.authorized(user, self.Collection(principal_path, True),
"w"):
with self.Collection.acquire_lock("r"):
principal = next(self.Collection.discover(principal_path),
None)
if not principal or principal.path != principal_path.strip("/"):
with self.Collection.acquire_lock("w"):
# the collection might exist by now
principal = next(self.Collection.discover(
principal_path), None)
if (not principal or
principal.path != principal_path.strip("/")):
self.Collection.create_collection(principal_path)
# Get content # Get content
content_length = int(environ.get("CONTENT_LENGTH") or 0) content_length = int(environ.get("CONTENT_LENGTH") or 0)
if content_length: if content_length:

View File

@ -364,25 +364,27 @@ class Collection(BaseCollection):
# path should already be sanitized # path should already be sanitized
sane_path = sanitize_path(path).strip("/") sane_path = sanitize_path(path).strip("/")
attributes = sane_path.split("/") attributes = sane_path.split("/")
if not attributes: if not attributes[0]:
return attributes.pop()
# Try to guess if the path leads to a collection or an item # Try to guess if the path leads to a collection or an item
folder = os.path.expanduser( folder = os.path.expanduser(
cls.configuration.get("storage", "filesystem_folder")) cls.configuration.get("storage", "filesystem_folder"))
if not os.path.isdir(path_to_filesystem(folder, sane_path)): if not os.path.isdir(path_to_filesystem(folder, sane_path)):
# path is not a collection # path is not a collection
if os.path.isfile(path_to_filesystem(folder, sane_path)): if attributes and os.path.isfile(path_to_filesystem(folder,
sane_path)):
# path is an item # path is an item
attributes.pop() attributes.pop()
elif os.path.isdir(path_to_filesystem(folder, *attributes[:-1])): elif attributes and os.path.isdir(path_to_filesystem(
folder, *attributes[:-1])):
# path parent is a collection # path parent is a collection
attributes.pop() attributes.pop()
# TODO: else: return? # TODO: else: return?
path = "/".join(attributes) path = "/".join(attributes)
principal = len(attributes) <= 1 principal = len(attributes) == 1
collection = cls(path, principal) collection = cls(path, principal)
yield collection yield collection
if depth != "0": if depth != "0":

View File

@ -589,16 +589,17 @@ def _propfind_response(path, item, props, user, write=False):
is404 = False is404 = False
if tag == _tag("D", "getetag"): if tag == _tag("D", "getetag"):
element.text = item.etag element.text = item.etag
elif tag == _tag("D", "principal-URL"):
tag = ET.Element(_tag("D", "href"))
tag.text = _href(collection, path)
element.append(tag)
elif tag == _tag("D", "getlastmodified"): elif tag == _tag("D", "getlastmodified"):
element.text = item.last_modified element.text = item.last_modified
elif tag in (_tag("D", "principal-collection-set"), elif tag == _tag("D", "principal-collection-set"):
_tag("C", "calendar-user-address-set"), tag = ET.Element(_tag("D", "href"))
_tag("CR", "addressbook-home-set"), tag.text = _href(collection, "/")
_tag("C", "calendar-home-set")): element.append(tag)
elif (tag in (_tag("C", "calendar-user-address-set"),
_tag("D", "principal-URL"),
_tag("CR", "addressbook-home-set"),
_tag("C", "calendar-home-set")) and
collection.is_principal):
tag = ET.Element(_tag("D", "href")) tag = ET.Element(_tag("D", "href"))
tag.text = _href(collection, path) tag.text = _href(collection, path)
element.append(tag) element.append(tag)