versatileimagefield/validators.py
def validate_ppoi(value, return_converted_tuple=False):
    """
    Converts, validates and optionally returns a string with formatting:
    '%(x_axis)dx%(y_axis)d' into a two position tuple.

    If a tuple is passed to `value` it is also validated.

    Both x_axis and y_axis must be floats or ints greater
    than 0 and less than 1.
    """

    valid_ppoi = True
    to_return = None
    if isinstance(value, tuple):
        valid_ppoi = validate_ppoi_tuple(value)
        if valid_ppoi:
            to_return = value
    else:
        tup = tuple()
        try:
            string_split = [
                float(segment.strip())
                for segment in value.split('x')
                if float(segment.strip()) >= 0 and float(segment.strip()) <= 1
            ]
        except Exception:
            valid_ppoi = False
        else:
            tup = tuple(string_split)

        valid_ppoi = validate_ppoi_tuple(tup)

        if valid_ppoi:
            to_return = tup
    if not valid_ppoi:
        raise ValidationError(
            message=INVALID_CENTERPOINT_ERROR_MESSAGE % str(value),
            code='invalid_ppoi'
        )
    else:
        if to_return and return_converted_tuple is True:
            return to_return
def validate_ppoi(value, return_converted_tuple=False):
    """
    Converts, validates and optionally returns a string with formatting:
    '%(x_axis)dx%(y_axis)d' into a two position tuple.

    If a tuple is passed to `value` it is also validated.

    Both x_axis and y_axis must be floats or ints greater
    than 0 and less than 1.
    """

    valid_ppoi = True
    to_return = None
    if isinstance(value, tuple):
        valid_ppoi = validate_ppoi_tuple(value)
        if valid_ppoi:
            to_return = value
    else:
        tup = tuple()
        try:
            string_split = [
                float(segment.strip())
                for segment in value.split('x')
                if float(segment.strip()) >= 0 and float(segment.strip()) <= 1
            ]
        except Exception:
            valid_ppoi = False
        else:
            tup = tuple(string_split)

        valid_ppoi = validate_ppoi_tuple(tup)

        if valid_ppoi:
            to_return = tup
    if not valid_ppoi:
        raise ValidationError(
            message=INVALID_CENTERPOINT_ERROR_MESSAGE % str(value),
            code='invalid_ppoi'
        )
    else:
        if to_return and return_converted_tuple is True:
            return to_return
Converts, validates and optionally returns a string with formatting:
    '%(x_axis)dx%(y_axis)d' into a two position tuple.

    If a tuple is passed to `value` it is also validated.

    Both x_axis and y_axis must be floats or ints greater
    than 0 and less than 1.