Example #1
0
    def process_resource(self, req, resp, resource, params):
        """Built-in Falcon middleware method."""
        if not isinstance(resource, BackendResource):
            return

        prefer = req.context.prefer = Prefer(req)

        # Common request validaton.
        prefer_time_zone = prefer.get('outlook.timezone', raw=True)
        if prefer_time_zone:
            try:
                prefer_tzinfo = to_timezone(prefer_time_zone)
            except Exception:
                logging.debug('unsupported timezone value received in request: %s', prefer_time_zone)
                raise falcon.HTTPBadRequest(description="Provided prefer timezone value is not supported.")
            prefer.update('outlook.timezone', (prefer_tzinfo, prefer_time_zone))

        # Backend selection.

        backend = None

        # userid prefixed with backend name, e.g. imap.userid
        userid = params.get('userid')
        if userid:
            # TODO handle unknown backend
            for name in self.name_backend:
                if userid.startswith(name + '.'):
                    backend = self.name_backend[name]
                    params['userid'] = userid[len(name) + 1:]
                    break

        # fall back to default backend for type
        if not backend:
            backend = resource.default_backend

        resource_cls = getattr(backend, resource.name)

        # Add server store object for the user to the resource instance.
        if hasattr(resource_cls, "need_store") and resource_cls.need_store:
            backend_name = next(iter(self.name_backend))
            utils = API.import_backend("{}.utils".format(backend_name), None)
            userid = params.pop('userid') if 'userid' in params else None
            try:
                server, store, userid = utils._server_store(req, userid, self.options)
            except MAPIErrorInvalidEntryid:
                raise falcon.HTTPBadRequest("Invalid entryid provided")
            # User should have store.
            if not store:
                raise falcon.HTTPForbidden("No store found for the user")

            # Todo(mort), store User object in the Context instead of userid.
            req.context.server_store = server, store, userid

        # result: eg ldap.UserResource() or kopano.MessageResource()
        req.context.resource = resource_cls(self.options)
Example #2
0
def parse_datetime_timezone(datetime_timezone, field):
    try:
        tz = to_timezone(datetime_timezone.get('timeZone', 'UTC'))
    except Exception:
        logging.debug('failed to parse timezone value when setting date to \'%s\'', field)
        raise HTTPBadRequest('The timeZone value of field \'%s\' is not supported.' % field)
    try:
        d = dateutil.parser.parse(datetime_timezone['dateTime'], ignoretz=True)
    except ValueError:
        logging.debug('failed to parse date when setting to \'%s\'', exc_info=True)
        raise HTTPBadRequest('The date value of field \'%s\' is invalid.' % field)

    return tz.localize(d).astimezone(LOCAL).replace(tzinfo=None)
Example #3
0
def set_date(item, field, arg):
    try:
        tz = to_timezone(arg.get('timeZone', 'UTC'))
    except Exception:
        logging.debug('failed to parse timezone value when setting date to \'%s\'', field)
        raise HTTPBadRequest('The timeZone value of field \'%s\' is not supported.' % field)
    try:
        d = dateutil.parser.parse(arg['dateTime'], ignoretz=True)
    except ValueError:
        logging.debug('failed to parse date when setting to \'%s\'', exc_info=True)
        raise HTTPBadRequest('The date value of field \'%s\' is invalid.' % field)

    # Set timezone as provided and convert to naive LOCAL time since that is what pyko uses internally.
    d = tz.localize(d).astimezone(LOCAL).replace(tzinfo=None)
    setattr(item, field, d)