Пример #1
0
def get_train_id(request):
    """
    Get train ID from requst query string and unquote content.

    Args:
        request (FlaskRequest): Http request instance.

    Returns:
        str, unquoted train ID.
    """
    train_id = request.args.get('train_id')
    if train_id is not None:
        try:
            train_id = unquote(train_id, errors='strict')
        except UnicodeDecodeError:
            raise exceptions.ParamValueError('Unquote error with strict mode')
    return train_id
Пример #2
0
def unquote_args(request, arg_name):
    """
    Get args from requst query string and unquote content.

    Args:
        request (FlaskRequest): Http request instance.
        arg_name (str): The name of arg.

    Returns:
        str, unquoted arg.
    """
    arg_value = request.args.get(arg_name, "")
    if arg_value is not None:
        try:
            arg_value = unquote(arg_value, errors='strict')
        except UnicodeDecodeError:
            raise exceptions.ParamValueError('Unquote error with strict mode')
    return arg_value
Пример #3
0
def str_to_bool(param, param_name):
    """
    Check param and transform it to bool.

    Args:
        param (str): 'true' or 'false' is valid.
        param_name (str): Param name.

    Returns:
        bool, if param is 'true', case insensitive.

    Raises:
        ParamValueError: If the value of param is not 'false' and 'true'.

    """
    if not isinstance(param, str):
        raise exceptions.ParamTypeError(param_name, 'str')

    if param.lower() not in ['false', 'true']:
        raise exceptions.ParamValueError("The value of %s must be 'false' or 'true'." % param_name)
    param = (param.lower() == 'true')

    return param