Merge pull request #482 from Unrud/smallimprovements
Some small cosmetic improvements for xmlutils
This commit is contained in:
		@@ -33,6 +33,8 @@ from datetime import datetime, timedelta, timezone
 | 
				
			|||||||
from http import client
 | 
					from http import client
 | 
				
			||||||
from urllib.parse import unquote, urlparse
 | 
					from urllib.parse import unquote, urlparse
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from . import storage
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
MIMETYPES = {
 | 
					MIMETYPES = {
 | 
				
			||||||
    "VADDRESSBOOK": "text/vcard",
 | 
					    "VADDRESSBOOK": "text/vcard",
 | 
				
			||||||
@@ -419,12 +421,11 @@ def _param_filter_match(vobject_item, filter_, parent_name):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def name_from_path(path, collection):
 | 
					def name_from_path(path, collection):
 | 
				
			||||||
    """Return Radicale item name from ``path``."""
 | 
					    """Return Radicale item name from ``path``."""
 | 
				
			||||||
    collection_path = collection.path.strip("/")
 | 
					    path = path.strip("/") + "/"
 | 
				
			||||||
    collection_parts = collection_path.split("/") if collection_path else []
 | 
					    start = collection.path + "/"
 | 
				
			||||||
    path = path.strip("/")
 | 
					    if not path.startswith(start):
 | 
				
			||||||
    path_parts = path.split("/") if path else []
 | 
					        raise ValueError("'%s' doesn't start with '%s'" % (path, start))
 | 
				
			||||||
    if len(path_parts) - len(collection_parts):
 | 
					    return path[len(start):]
 | 
				
			||||||
        return path_parts[-1]
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def props_from_request(root, actions=("set", "remove")):
 | 
					def props_from_request(root, actions=("set", "remove")):
 | 
				
			||||||
@@ -535,10 +536,9 @@ def propfind(path, xml_request, read_collections, write_collections, user):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def _propfind_response(path, item, props, user, write=False):
 | 
					def _propfind_response(path, item, props, user, write=False):
 | 
				
			||||||
    """Build and return a PROPFIND response."""
 | 
					    """Build and return a PROPFIND response."""
 | 
				
			||||||
    # TODO: fix this
 | 
					    is_collection = isinstance(item, storage.BaseCollection)
 | 
				
			||||||
    is_collection = hasattr(item, "list")
 | 
					 | 
				
			||||||
    if is_collection:
 | 
					    if is_collection:
 | 
				
			||||||
        is_leaf = bool(item.get_meta("tag"))
 | 
					        is_leaf = item.get_meta("tag") in ("VADDRESSBOOK", "VCALENDAR")
 | 
				
			||||||
        collection = item
 | 
					        collection = item
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        collection = item.collection
 | 
					        collection = item.collection
 | 
				
			||||||
@@ -548,18 +548,11 @@ def _propfind_response(path, item, props, user, write=False):
 | 
				
			|||||||
    href = ET.Element(_tag("D", "href"))
 | 
					    href = ET.Element(_tag("D", "href"))
 | 
				
			||||||
    if is_collection:
 | 
					    if is_collection:
 | 
				
			||||||
        # Some clients expect collections to end with /
 | 
					        # Some clients expect collections to end with /
 | 
				
			||||||
        uri = item.path + "/"
 | 
					        uri = "/%s/" % item.path if item.path else "/"
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        # TODO: fix this
 | 
					        uri = "/" + posixpath.join(collection.path, item.href)
 | 
				
			||||||
        if path.split("/")[-1] == item.href:
 | 
					 | 
				
			||||||
            # Happening when depth is 0
 | 
					 | 
				
			||||||
            uri = path
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            # Happening when depth is 1
 | 
					 | 
				
			||||||
            uri = "/".join((path, item.href))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # TODO: fix this
 | 
					    href.text = _href(collection, uri)
 | 
				
			||||||
    href.text = _href(collection, uri.replace("//", "/"))
 | 
					 | 
				
			||||||
    response.append(href)
 | 
					    response.append(href)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    propstat404 = ET.Element(_tag("D", "propstat"))
 | 
					    propstat404 = ET.Element(_tag("D", "propstat"))
 | 
				
			||||||
@@ -792,16 +785,14 @@ def report(path, xml_request, collection):
 | 
				
			|||||||
        name = name_from_path(hreference, collection)
 | 
					        name = name_from_path(hreference, collection)
 | 
				
			||||||
        if name:
 | 
					        if name:
 | 
				
			||||||
            # Reference is an item
 | 
					            # Reference is an item
 | 
				
			||||||
            path = "/".join(hreference.split("/")[:-1]) + "/"
 | 
					 | 
				
			||||||
            item = collection.get(name)
 | 
					            item = collection.get(name)
 | 
				
			||||||
            if item is None:
 | 
					            if not item:
 | 
				
			||||||
                response = _item_response(hreference, found_item=False)
 | 
					                response = _item_response(hreference, found_item=False)
 | 
				
			||||||
                multistatus.append(response)
 | 
					                multistatus.append(response)
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
            items = [item]
 | 
					            items = [item]
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            # Reference is a collection
 | 
					            # Reference is a collection
 | 
				
			||||||
            path = hreference
 | 
					 | 
				
			||||||
            items = collection.pre_filtered_list(filters)
 | 
					            items = collection.pre_filtered_list(filters)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for item in items:
 | 
					        for item in items:
 | 
				
			||||||
@@ -835,13 +826,7 @@ def report(path, xml_request, collection):
 | 
				
			|||||||
                else:
 | 
					                else:
 | 
				
			||||||
                    not_found_props.append(element)
 | 
					                    not_found_props.append(element)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # TODO: fix this
 | 
					            uri = "/" + posixpath.join(collection.path, item.href)
 | 
				
			||||||
            if hreference.split("/")[-1] == item.href:
 | 
					 | 
				
			||||||
                # Happening when depth is 0
 | 
					 | 
				
			||||||
                uri = hreference
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                # Happening when depth is 1
 | 
					 | 
				
			||||||
                uri = posixpath.join(hreference, item.href)
 | 
					 | 
				
			||||||
            multistatus.append(_item_response(
 | 
					            multistatus.append(_item_response(
 | 
				
			||||||
                uri, found_props=found_props,
 | 
					                uri, found_props=found_props,
 | 
				
			||||||
                not_found_props=not_found_props, found_item=True))
 | 
					                not_found_props=not_found_props, found_item=True))
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user