Ejemplo n.º 1
0
 async def handle(self, request, environ, app):
     content_type = request.content_type
     base_content_type, params = webdav.parse_type(content_type)
     if base_content_type not in (
             "text/xml",
             "application/xml",
             None,
             "text/plain",
             "application/octet-stream",
     ):
         raise webdav.UnsupportedMediaType(content_type)
     href, path, resource = app._get_resource_from_environ(request, environ)
     if resource is not None:
         return webdav._send_simple_dav_error(
             request,
             "403 Forbidden",
             error=ET.Element("{DAV:}resource-must-be-null"),
             description=("Something already exists at %r" % path),
         )
     try:
         resource = app.backend.create_collection(path)
     except FileNotFoundError:
         return webdav.Response(status="409 Conflict")
     el = ET.Element("{DAV:}resourcetype")
     await app.properties["{DAV:}resourcetype"].get_value(
         href, resource, el, environ)
     ET.SubElement(el, "{urn:ietf:params:xml:ns:caldav}calendar")
     app.properties["{DAV:}resourcetype"].set_value(href, resource, el)
     if base_content_type in ("text/xml", "application/xml"):
         et = await webdav._readXmlBody(
             request,
             "{urn:ietf:params:xml:ns:caldav}mkcalendar",
             strict=app.strict,
         )
         propstat = []
         for el in et:
             if el.tag != "{DAV:}set":
                 raise webdav.BadRequestError(
                     "Unknown tag %s in mkcalendar" % el.tag)
             propstat.extend(
                 webdav.apply_modify_prop(el, href, resource,
                                          app.properties))
             ret = ET.Element(
                 "{urn:ietf:params:xml:ns:carldav:}mkcalendar-response")
         for propstat_el in webdav.propstat_as_xml(propstat):
             ret.append(propstat_el)
         return webdav._send_xml_response("201 Created", ret,
                                          webdav.DEFAULT_ENCODING)
     else:
         return webdav.Response(status="201 Created")
Ejemplo n.º 2
0
    async def report(self, environ, body, resources_by_hrefs,
                     properties, base_href, base_resource, depth):
        requested = None
        for el in body:
            if el.tag == '{urn:ietf:params:xml:ns:caldav}time-range':
                requested = el
            else:
                raise AssertionError("unexpected XML element")
        tz = get_calendar_timezone(base_resource)

        def tzify(dt):
            return as_tz_aware_ts(dt, tz).astimezone(pytz.utc)
        (start, end) = _parse_time_range(requested)
        assert start.tzinfo
        assert end.tzinfo
        ret = ICalendar()
        ret['VERSION'] = '2.0'
        ret['PRODID'] = PRODID
        fb = FreeBusy()
        fb['DTSTAMP'] = vDDDTypes(tzify(datetime.datetime.now()))
        fb['DTSTART'] = vDDDTypes(start)
        fb['DTEND'] = vDDDTypes(end)
        fb['FREEBUSY'] = [item async for item in iter_freebusy(
            webdav.traverse_resource(base_resource, base_href, depth),
            start, end, tzify)]
        ret.add_component(fb)
        return webdav.Response(status='200 OK', body=[ret.to_ical()])