Merge pull request #566 from Unrud/cleanxmlutils

Some small fixes for xmlutils.py
This commit is contained in:
Guillaume Ayoub 2017-04-15 09:24:38 +02:00 committed by GitHub
commit 26d8214296

View File

@ -180,6 +180,7 @@ def _prop_match(item, filter_):
for component in item.components(): for component in item.components():
if component.name in ("VTODO", "VEVENT", "VJOURNAL"): if component.name in ("VTODO", "VEVENT", "VJOURNAL"):
vobject_item = component vobject_item = component
break
else: else:
vobject_item = item.item vobject_item = item.item
if filter_length == 0: if filter_length == 0:
@ -669,39 +670,46 @@ def _propfind_response(base_prefix, path, item, props, user, write=False,
tag.text = _href(base_prefix, ("/%s/" % user) if user else "/") tag.text = _href(base_prefix, ("/%s/" % user) if user else "/")
element.append(tag) element.append(tag)
elif tag == _tag("D", "current-user-privilege-set"): elif tag == _tag("D", "current-user-privilege-set"):
privilege = ET.Element(_tag("D", "privilege")) privileges = [("D", "read")]
if write: if write:
privilege.append(ET.Element(_tag("D", "all"))) privileges.append(("D", "all"))
privilege.append(ET.Element(_tag("D", "write"))) privileges.append(("D", "write"))
privilege.append(ET.Element(_tag("D", "write-properties"))) privileges.append(("D", "write-properties"))
privilege.append(ET.Element(_tag("D", "write-content"))) privileges.append(("D", "write-content"))
privilege.append(ET.Element(_tag("D", "read"))) for ns, privilege_name in privileges:
privilege = ET.Element(_tag("D", "privilege"))
privilege.append(ET.Element(_tag(ns, privilege_name)))
element.append(privilege) element.append(privilege)
elif tag == _tag("D", "supported-report-set"): elif tag == _tag("D", "supported-report-set"):
for report_name in ( reports = [("D", "expand-property"), # not implemented
"principal-property-search", "sync-collection", ("D", "principal-search-property-set"), # not implemented
"expand-property", "principal-search-property-set"): ("D", "principal-property-search")] # not implemented
if is_collection and is_leaf:
reports.append(("D", "sync-collection"))
if item.get_meta("tag") == "VADDRESSBOOK":
reports.append(("CR", "addressbook-multiget"))
reports.append(("CR", "addressbook-query"))
elif item.get_meta("tag") == "VCALENDAR":
reports.append(("C", "calendar-multiget"))
reports.append(("C", "calendar-query"))
for ns, report_name in reports:
supported = ET.Element(_tag("D", "supported-report")) supported = ET.Element(_tag("D", "supported-report"))
report_tag = ET.Element(_tag("D", "report")) report_tag = ET.Element(_tag("D", "report"))
supported_report_tag = ET.Element(_tag("D", report_name)) supported_report_tag = ET.Element(_tag(ns, report_name))
report_tag.append(supported_report_tag) report_tag.append(supported_report_tag)
supported.append(report_tag) supported.append(report_tag)
element.append(supported) element.append(supported)
elif is_collection: elif is_collection:
if tag == _tag("D", "getcontenttype"): if tag == _tag("D", "getcontenttype"):
item_tag = item.get_meta("tag") if is_leaf:
if item_tag: element.text = MIMETYPES[item.get_meta("tag")]
element.text = MIMETYPES[item_tag]
else: else:
is404 = True is404 = True
elif tag == _tag("D", "resourcetype"): elif tag == _tag("D", "resourcetype"):
if item.is_principal: if item.is_principal:
tag = ET.Element(_tag("D", "principal")) tag = ET.Element(_tag("D", "principal"))
element.append(tag) element.append(tag)
item_tag = item.get_meta("tag") if is_leaf:
if is_leaf or item_tag:
# 2nd case happens when the collection is not stored yet,
# but the resource type is guessed
if item.get_meta("tag") == "VADDRESSBOOK": if item.get_meta("tag") == "VADDRESSBOOK":
tag = ET.Element(_tag("CR", "addressbook")) tag = ET.Element(_tag("CR", "addressbook"))
element.append(tag) element.append(tag)
@ -822,12 +830,21 @@ def report(base_prefix, path, xml_request, collection):
""" """
root = ET.fromstring(xml_request.encode("utf8")) root = ET.fromstring(xml_request.encode("utf8"))
if root.tag in (
_tag("D", "principal-search-property-set"),
_tag("D", "principal-property-search"),
_tag("D", "expand-property")):
# We don't support searching for principals or indirect retrieving of
# properties, just return an empty result.
# InfCloud asks for expand-property reports (even if we don't announce
# support for them) and stops working if an error code is returned.
collection.logger.warning("Unsupported report method: %s", root.tag)
return _pretty_xml(ET.Element(_tag("D", "multistatus")))
prop_element = root.find(_tag("D", "prop")) prop_element = root.find(_tag("D", "prop"))
props = ( props = (
[prop.tag for prop in prop_element] [prop.tag for prop in prop_element]
if prop_element is not None else []) if prop_element is not None else [])
if collection:
if root.tag in ( if root.tag in (
_tag("C", "calendar-multiget"), _tag("C", "calendar-multiget"),
_tag("CR", "addressbook-multiget")): _tag("CR", "addressbook-multiget")):
@ -844,10 +861,8 @@ def report(base_prefix, path, xml_request, collection):
else: else:
hreferences = (path,) hreferences = (path,)
filters = ( filters = (
root.findall(".//%s" % _tag("C", "filter")) + root.findall("./%s" % _tag("C", "filter")) +
root.findall(".//%s" % _tag("CR", "filter"))) root.findall("./%s" % _tag("CR", "filter")))
else:
hreferences = filters = ()
multistatus = ET.Element(_tag("D", "multistatus")) multistatus = ET.Element(_tag("D", "multistatus"))