Exemplo n.º 1
0
def set_auto_update_enabled(system_id):
    """
    Configure system auto-update

    Configure whether auto-update is enabled for
     auto-updatable feeds in a system.

    The endpoint takes a single form parameter `enabled`
    which can either be `true` or `false` (case insensitive).

    Return code         | Description
    --------------------|-------------
    `204 NO CONTENT`    | The configuration was applied successfully.
    `400 BAD REQUEST`   | Returned if the form parameter is not provided or is invalid.
    `404 NOT FOUND`     | Returned if the system does not exist.
    """
    # TODO: this should just accept a URL parameter
    form_key_to_value = flask.request.form.to_dict()
    enabled = form_key_to_value.get("enabled")
    if enabled is None:
        raise exceptions.InvalidInput("The form variable 'enabled' is required")
    enabled = enabled.lower()
    if enabled not in {"false", "true"}:
        raise exceptions.InvalidInput(
            "The form variable 'enabled' has to be 'true' or 'false', not '{}'".format(
                enabled
            )
        )
    systemservice.set_auto_update_enabled(
        system_id, form_key_to_value["enabled"].lower() == "true"
    )
    return "", HttpStatus.NO_CONTENT
Exemplo n.º 2
0
def _list_systems(system_ids):
    system_ids = set(system_ids)
    if len(system_ids) <= 1:
        raise exceptions.InvalidInput(
            "At least two systems must be provided for a transfers config.")
    systems = systemqueries.list_all(system_ids)
    if len(system_ids) == len(systems):
        return systems
    missing_system_ids = set(system_ids)
    for system in systems:
        missing_system_ids.remove(system.id)
    raise exceptions.InvalidInput(
        f"The following system IDs are invalid: {', '.join(system_ids)}")
Exemplo n.º 3
0
def get_float_url_parameter(key, default=None, required=False):
    raw_value = flask.request.args.get(key)
    if raw_value is None:
        if not required:
            return default
        raise exceptions.InvalidInput(
            f"The URL parameter '{key}' is required.")
    try:
        return float(raw_value)
    except ValueError:
        raise exceptions.InvalidInput(
            f"Received non-float value '{raw_value}' for float URL parameter '{key}'."
        )
Exemplo n.º 4
0
def _get_config_file(config_source_url, uploaded_config_file):
    if config_source_url is not None:
        try:
            response = requests.get(config_source_url)
            response.raise_for_status()
        except requests.exceptions.RequestException:
            raise exceptions.InvalidInput(
                "Could not download YAML config file from '{}'".format(
                    config_source_url
                )
            )
        return response.text
    elif uploaded_config_file is not None:
        return uploaded_config_file.read().decode("utf-8")
    else:
        raise exceptions.InvalidInput("YAML config file not provided!")
Exemplo n.º 5
0
def get_list_url_parameter(key, required=False):
    raw = flask.request.args.getlist(key)
    if len(raw) == 0:
        if not required:
            return None
        raise exceptions.InvalidInput(
            f"The URL parameter '{key}' is required.")
    return raw
Exemplo n.º 6
0
def get_url_parameters(expected_keys, error_if_extra_keys=True):
    all_request_args = flask.request.args
    if error_if_extra_keys:
        extra_keys = set(all_request_args.keys()) - set(expected_keys)
        if len(extra_keys) > 0:
            raise exceptions.InvalidInput(
                "Unknown URL parameters: {}. Valid URL parameters for this endpoint: {}"
                .format(extra_keys, expected_keys))
    return {key: all_request_args.get(key) for key in expected_keys}
Exemplo n.º 7
0
def get_enum_url_parameter(key, enum_, default=None):
    raw_value = flask.request.args.get(key)
    if raw_value is None:
        return default
    try:
        return enum_[raw_value.upper()]
    except KeyError:
        raise exceptions.InvalidInput(
            ("Received unexpected value '{}' for URL parameter '{}'. "
             "Valid values are {} (case insensitive).").format(
                 raw_value,
                 key,
                 ", ".join(["'{}'".format(element.name) for element in enum_]),
             ))
Exemplo n.º 8
0
def create_feed_update(system_id, feed_id):
    """
    Perform a feed update

    Perform a feed update of the given feed.
    The response is a description of the feed update.

    This endpoint is provided for one-off feed updates and development work.
    In general feed updates should instead be scheduled periodically using the transit system configuration;
    see the [transit system documentation](systems.md) for more information.

    Return code         | Description
    --------------------|-------------
    `201 CREATED`       | Returned if the system and feed exist, in which case the update is _scheduled_ (and executed in the same thread, if sync).
    `404 NOT FOUND`     | Returned if either the system or the feed does not exist.
    """
    user_provided_content = flask.request.files.get("content")
    if user_provided_content is not None:
        user_provided_content = user_provided_content.read()
        if len(user_provided_content) == 0:
            raise exceptions.InvalidInput("No file or an empty file provided.")
        if not is_sync_request():
            raise exceptions.InvalidInput(
                "Feed updates with content provided must be run synchronously. "
                "Use the sync=true url parameter."
            )
    feed_update_pk = feedservice.create_and_execute_feed_update(
        system_id,
        feed_id,
        execute_async=not is_sync_request(),
        content=user_provided_content,
    )
    return (
        feedservice.get_update_in_feed_by_pk(system_id, feed_id, feed_update_pk),
        HttpStatus.CREATED,
    )