def wrapper(*args, **kwargs): form = get_form_from_request(request) if form is None: current_app.logger.warning( "Unsupported request method for unpacking 'start' and 'duration' from request." ) return invalid_method(request.method) if "start" in form: start = parse_isodate_str(form["start"]) if not start: current_app.logger.warning("Cannot parse 'start' value") return invalid_period() if start.tzinfo is None: current_app.logger.warning("Cannot parse timezone of 'start' value") return invalid_timezone( "Start time should explicitly state a timezone." ) else: current_app.logger.warning("Request missing 'start'.") return invalid_period() kwargs["start"] = start if "duration" in form: duration = parse_duration(form["duration"], start) if not duration: current_app.logger.warning("Cannot parse 'duration' value") return invalid_period() else: current_app.logger.warning("Request missing 'duration'.") return invalid_period() kwargs["duration"] = duration return fn(*args, **kwargs)
def decorated_service(*args, **kwargs): form = get_form_from_request(request) if form is None: current_app.logger.warning( "Unsupported request method for unpacking 'horizon' from request." ) return invalid_method(request.method) rolling = True if "horizon" in form: horizon, rolling = parse_horizon(form["horizon"]) if horizon is None: current_app.logger.warning("Cannot parse 'horizon' value") return invalid_horizon() elif ex_post is True: if horizon > timedelta(hours=0): extra_info = "Meter data must have a zero or negative horizon to indicate observations after the fact." return invalid_horizon(extra_info) elif infer_missing is True: # A missing horizon is only accepted if the server can infer it if "start" in form and "duration" in form: start = parse_isodate_str(form["start"]) duration = parse_duration(form["duration"], start) if not start: extra_info = "Cannot parse 'start' value." current_app.logger.warning(extra_info) return invalid_period(extra_info) if start.tzinfo is None: current_app.logger.warning( "Cannot parse timezone of 'start' value" ) return invalid_timezone( "Start time should explicitly state a timezone." ) if not duration: extra_info = "Cannot parse 'duration' value." current_app.logger.warning(extra_info) return invalid_period(extra_info) if current_app.config.get("FLEXMEASURES_MODE", "") == "play": horizon = timedelta(hours=0) else: horizon = start + duration - server_now() rolling = False else: current_app.logger.warning( "Request missing both 'horizon', 'start' and 'duration'." ) extra_info = "Specify a 'horizon' value, or 'start' and 'duration' values so that the horizon can be inferred." return invalid_horizon(extra_info) else: # Otherwise, a missing horizon is fine horizon = None kwargs["horizon"] = horizon if infer_missing is True: kwargs["rolling"] = rolling return fn(*args, **kwargs)
def decorated_service(*args, **kwargs): duration_arg = parser.parse( {"duration": DurationField()}, request, location="args_and_json", unknown=marshmallow.EXCLUDE, ) if "duration" in duration_arg: duration = duration_arg["duration"] duration = DurationField.ground_from( duration, kwargs.get("start", kwargs.get("datetime", None)), ) if not duration: # TODO: deprecate extra_info = "Cannot parse 'duration' value." current_app.logger.warning(extra_info) return invalid_period(extra_info) kwargs["duration"] = duration else: kwargs["duration"] = default_duration return fn(*args, **kwargs)
def decorated_service(*args, **kwargs): duration_arg = parser.parse( {"duration": fields.Str(validate=validate_duration_field)}, request, location="args_and_json", unknown=marshmallow.EXCLUDE, ) if "duration" in duration_arg: duration = parse_duration( duration_arg["duration"], kwargs.get("start", kwargs.get("datetime", None)), ) if not duration: extra_info = "Cannot parse 'duration' value." current_app.logger.warning(extra_info) return invalid_period(extra_info) kwargs["duration"] = duration else: kwargs["duration"] = default_duration return fn(*args, **kwargs)