Exemple #1
0
    def actionExpand(self, request, tzid):
        """
        Expand a timezone within specified start/end dates.
        """

        if set(request.args.keys()) - set(("start", "end", "changedsince",)):
            self.problemReport("invalid-action", "Invalid request-URI query parameters", responsecode.BAD_REQUEST)

        start = request.args.get("start", ())
        if len(start) == 0:
            self.problemReport("invalid-start", "Missing start request-URI query parameter", responsecode.BAD_REQUEST)
        if len(start) > 1:
            self.problemReport("invalid-start", "Too many start request-URI query parameters", responsecode.BAD_REQUEST)
        elif len(start) == 1:
            try:
                if len(start[0]) != 20:
                    raise ValueError()
                start = DateTime.parseText(start[0], fullISO=True)
            except ValueError:
                self.problemReport("invalid-start", "Invalid start request-URI query parameter value", responsecode.BAD_REQUEST)

        end = request.args.get("end", ())
        if len(end) == 0:
            self.problemReport("invalid-end", "Missing end request-URI query parameter", responsecode.BAD_REQUEST)
        if len(end) > 1:
            self.problemReport("invalid-end", "Too many end request-URI query parameters", responsecode.BAD_REQUEST)
        elif len(end) == 1:
            try:
                if len(end[0]) != 20:
                    raise ValueError()
                end = DateTime.parseText(end[0], fullISO=True)
            except ValueError:
                self.problemReport("invalid-end", "Invalid end request-URI query parameter value", responsecode.BAD_REQUEST)
            if end <= start:
                self.problemReport("invalid-end", "Invalid end request-URI query parameter value - earlier than start", responsecode.BAD_REQUEST)

        tzdata = self.timezones.getTimezone(tzid)
        if tzdata is None:
            self.problemReport("tzid-not-found", "Time zone identifier not found", responsecode.NOT_FOUND)

        # Now do the expansion (but use a cache to avoid re-calculating TZs)
        observances = self.expandcache.get((tzid, start, end), None)
        if observances is None:
            observances = tzexpandlocal(tzdata, start, end, utc_onset=True)
            self.expandcache[(tzid, start, end)] = observances

        # Turn into JSON
        result = {
            "dtstamp": self.timezones.dtstamp,
            "tzid": tzid,
            "observances": [
                {
                    "name": name,
                    "onset": onset.getXMLText(),
                    "utc-offset-from": utc_offset_from,
                    "utc-offset-to": utc_offset_to,
                } for onset, utc_offset_from, utc_offset_to, name in observances
            ],
        }
        return JSONResponse(responsecode.OK, result, pretty=config.TimezoneService.PrettyPrintJSON)
    def actionExpand(self, request, tzid):
        """
        Expand a timezone within specified start/end dates.
        """

        if set(request.args.keys()) - set(("start", "end",)):
            self.problemReport("invalid-action", "Invalid request-URI query parameters", responsecode.BAD_REQUEST)

        start = request.args.get("start", ())
        if len(start) == 0:
            self.problemReport("invalid-start", "Missing start request-URI query parameter", responsecode.BAD_REQUEST)
        if len(start) > 1:
            self.problemReport("invalid-start", "Too many start request-URI query parameters", responsecode.BAD_REQUEST)
        elif len(start) == 1:
            try:
                if len(start[0]) != 20:
                    raise ValueError()
                start = DateTime.parseText(start[0], fullISO=True)
            except ValueError:
                self.problemReport("invalid-start", "Invalid start request-URI query parameter value", responsecode.BAD_REQUEST)

        end = request.args.get("end", ())
        if len(end) == 0:
            self.problemReport("invalid-end", "Missing end request-URI query parameter", responsecode.BAD_REQUEST)
        if len(end) > 1:
            self.problemReport("invalid-end", "Too many end request-URI query parameters", responsecode.BAD_REQUEST)
        elif len(end) == 1:
            try:
                if len(end[0]) != 20:
                    raise ValueError()
                end = DateTime.parseText(end[0], fullISO=True)
            except ValueError:
                self.problemReport("invalid-end", "Invalid end request-URI query parameter value", responsecode.BAD_REQUEST)
            if end <= start:
                self.problemReport("invalid-end", "Invalid end request-URI query parameter value - earlier than start", responsecode.BAD_REQUEST)

        tzdata = self.timezones.getTimezone(tzid)
        if tzdata is None:
            self.problemReport("tzid-not-found", "Time zone identifier not found", responsecode.NOT_FOUND)

        # Now do the expansion (but use a cache to avoid re-calculating TZs)
        observances = self.expandcache.get((tzid, start, end), None)
        if observances is None:
            observances = tzexpandlocal(tzdata, start, end, utc_onset=True)
            self.expandcache[(tzid, start, end)] = observances

        # Turn into JSON
        result = {
            "dtstamp": self.timezones.dtstamp,
            "tzid": tzid,
            "observances": [
                {
                    "name": name,
                    "onset": onset.getXMLText(),
                    "utc-offset-from": utc_offset_from,
                    "utc-offset-to": utc_offset_to,
                } for onset, utc_offset_from, utc_offset_to, name in observances
            ],
        }
        return JSONResponse(responsecode.OK, result, pretty=config.TimezoneService.PrettyPrintJSON)
    def doExpand(self, request):
        """
        Expand a timezone within specified start/end dates.
        """

        tzids = request.args.get("tzid", ())
        if len(tzids) != 1:
            raise HTTPError(JSONResponse(
                responsecode.BAD_REQUEST,
                {
                    "error": "invalid-tzid",
                    "description": "Invalid tzid query parameter",
                },
            ))

        try:
            start = request.args.get("start", ())
            if len(start) > 1:
                raise ValueError()
            elif len(start) == 1:
                start = PyCalendarDateTime.parseText(start[0])
            else:
                start = PyCalendarDateTime.getToday()
                start.setDay(1)
                start.setMonth(1)
        except ValueError:
            raise HTTPError(JSONResponse(
                responsecode.BAD_REQUEST,
                {
                    "error": "invalid-start",
                    "description": "Invalid start query parameter",
                }
            ))

        try:
            end = request.args.get("end", ())
            if len(end) > 1:
                raise ValueError()
            elif len(end) == 1:
                end = PyCalendarDateTime.parseText(end[0])
            else:
                end = PyCalendarDateTime.getToday()
                end.setDay(1)
                end.setMonth(1)
                end.offsetYear(10)
            if end <= start:
                raise ValueError()
        except ValueError:
            raise HTTPError(JSONResponse(
                responsecode.BAD_REQUEST,
                {
                    "error": "invalid-end",
                    "description": "Invalid end query parameter",
                }
            ))

        tzid = tzids[0]
        tzdata = self.timezones.getTimezone(tzid)
        if tzdata is None:
            raise HTTPError(JSONResponse(
                responsecode.NOT_FOUND,
                {
                    "error": "missing-tzid",
                    "description": "Tzid could not be found",
                }
            ))

        # Now do the expansion (but use a cache to avoid re-calculating TZs)
        observances = self.expandcache.get((tzid, start, end), None)
        if observances is None:
            observances = tzexpandlocal(tzdata, start, end)
            self.expandcache[(tzid, start, end)] = observances

        # Turn into JSON
        result = {
            "dtstamp": self.timezones.dtstamp,
            "observances": [
                {
                    "name": name,
                    "onset": onset.getXMLText(),
                    "utc-offset-from": utc_offset_from,
                    "utc-offset-to": utc_offset_to,
                } for onset, utc_offset_from, utc_offset_to, name in observances
            ],
        }
        return JSONResponse(responsecode.OK, result)