Exemple #1
0
 def as_xml(self):
     if self.conditionCode == PRECONDITION_CODE_MissingLockToken:
         assert len(self.hrefs) > 0, "lock-token-submitted requires at least one href"
     errorEL = etree.Element("{DAV:}error")
     condEL = etree.SubElement(errorEL, self.conditionCode)
     for href in self.hrefs:
         etree.SubElement(condEL, "{DAV:}href").text = href
     return errorEL
Exemple #2
0
def addPropertyResponse(multistatusEL, href, propList):
    """Append <response> element to <multistatus> element.

    <prop> node depends on the value type: 
      - str or unicode: add element with this content
      - None: add an empty element
      - etree.Element: add XML element as child 
      - DAVError: add an empty element to an own <propstatus> for this status code
    
    @param multistatusEL: etree.Element
    @param href: global URL of the resource, e.g. 'http://server:port/path'.
    @param propList: list of 2-tuples (name, value) 
    """
    # Split propList by status code and build a unique list of namespaces
    nsCount = 1
    nsDict = {}
    nsMap = {}
    propDict = {}

    for name, value in propList:
        status = "200 OK"
        if isinstance(value, DAVError):
            status = getHttpStatusString(value)
            # Always generate *empty* elements for props with error status
            value = None

        # Collect namespaces, so we can declare them in the <response> for
        # compacter output
        ns, _ = splitNamespace(name)
        if ns != "DAV:" and not ns in nsDict and ns != "":
            nsDict[ns] = True
            nsMap["NS%s" % nsCount] = ns
            nsCount += 1

        propDict.setdefault(status, []).append((name, value))

    # <response>
    responseEL = makeSubElement(multistatusEL, "{DAV:}response", nsmap=nsMap)

    #    log("href value:%s" % (stringRepr(href)))
    #    etree.SubElement(responseEL, "{DAV:}href").text = toUnicode(href)
    etree.SubElement(responseEL, "{DAV:}href").text = href
    #    etree.SubElement(responseEL, "{DAV:}href").text = urllib.quote(href, safe="/" + "!*'()," + "$-_|.")

    # One <propstat> per status code
    for status in propDict:
        propstatEL = etree.SubElement(responseEL, "{DAV:}propstat")
        # List of <prop>
        propEL = etree.SubElement(propstatEL, "{DAV:}prop")
        for name, value in propDict[status]:
            if value is None:
                etree.SubElement(propEL, name)
            elif isinstance(value, etree._Element):
                propEL.append(value)
            else:
                # value must be string or unicode
                #                log("%s value:%s" % (name, stringRepr(value)))
                #                etree.SubElement(propEL, name).text = value
                etree.SubElement(propEL, name).text = toUnicode(value)
        # <status>
        etree.SubElement(propstatEL,
                         "{DAV:}status").text = "HTTP/1.1 %s" % status