예제 #1
0
    def __init__(self, detail=None):
        """
        Create an instance of exception with users detail information if
        it is passed.

        :param detail: users detail information (string).
        """
        if detail is not None:
            self.detail = force_text(detail)
        else:
            self.detail = force_text(self.default_detail)
예제 #2
0
    def __init__(self, detail=None):
        """
        Create an instance of exception with users detail information if
        it is passed.

        :param detail: users detail information (string).
        """
        if detail is not None:
            self.detail = force_text(detail)
        else:
            self.detail = force_text(self.default_detail)
예제 #3
0
def smart_repr(value):
    # Convert any type of data to a string
    value = force_text(value)

    # Representations like
    # <django.core.validators.RegexValidator object at 0x1047af050>
    # Should be presented as
    # <django.core.validators.RegexValidator object>
    value = re.sub(' at 0x[0-9A-Fa-f]{4,32}>', '>', value)
    return value
예제 #4
0
def smart_repr(value):
    # Convert any type of data to a string
    value = force_text(value)

    # Representations like
    # <django.core.validators.RegexValidator object at 0x1047af050>
    # Should be presented as
    # <django.core.validators.RegexValidator object>
    value = re.sub(' at 0x[0-9A-Fa-f]{4,32}>', '>', value)
    return value
예제 #5
0
파일: utils.py 프로젝트: Relrin/aiorest-ws
def reverse(view_name, urlconf=None, args=[], kwargs={}, relative=False):
    """
    Generate URL to endpoint, which processing by Application instance, based
    on the `view_name` and passed arguments.

    :param view_name: view name.
    :param urlconf: urlconf instance (dictionary).
    :param args: tuple of data, which used by handler with this URL.
    :param kwargs: named arguments for the defined URL.
    :return: generated URL with the passed arguments.
    """
    if urlconf is None:
        urlconf = get_urlconf()

    root_path = ''
    api_path = ''
    try:
        # Get root path, when necessary to generate absolute URL
        root_path = '' if relative else urlconf['path'].strip('/')

        # Get path to a specific endpoint
        route = urlconf['routes'][view_name]
        api_path = route.path.strip('/')

        # Replace parameters in 'api_url' on the passed args
        dynamic_parameters = re.findall(DYNAMIC_PARAMETER, api_path)
        if len(dynamic_parameters) != len(args):
            raise ValueError(
                "Endpoint '{path}' must take {valid_count} parameters, "
                "but passed {invalid_count}.".format(
                    path=api_path,
                    valid_count=len(dynamic_parameters),
                    invalid_count=len(args)
                )
            )
        for parameter, value in zip(dynamic_parameters, args):
            api_path = api_path.replace(parameter, value)
    except KeyError:
        raise NoReverseMatch()

    url_parameters = _generate_url_parameters(kwargs) if kwargs else ""
    url = '/'.join([root_path, api_path, url_parameters])
    return force_text(url)
예제 #6
0
def reverse(view_name, urlconf=None, args=[], kwargs={}, relative=False):
    """
    Generate URL to endpoint, which processing by Application instance, based
    on the `view_name` and passed arguments.

    :param view_name: view name.
    :param urlconf: urlconf instance (dictionary).
    :param args: tuple of data, which used by handler with this URL.
    :param kwargs: named arguments for the defined URL.
    :return: generated URL with the passed arguments.
    """
    if urlconf is None:
        urlconf = get_urlconf()

    root_path = ''
    api_path = ''
    try:
        # Get root path, when necessary to generate absolute URL
        root_path = '' if relative else urlconf['path'].strip('/')

        # Get path to a specific endpoint
        route = urlconf['routes'][view_name]
        api_path = route.path.strip('/')

        # Replace parameters in 'api_url' on the passed args
        dynamic_parameters = re.findall(DYNAMIC_PARAMETER, api_path)
        if len(dynamic_parameters) != len(args):
            raise ValueError(
                "Endpoint '{path}' must take {valid_count} parameters, "
                "but passed {invalid_count}.".format(
                    path=api_path,
                    valid_count=len(dynamic_parameters),
                    invalid_count=len(args)))
        for parameter, value in zip(dynamic_parameters, args):
            api_path = api_path.replace(parameter, value)
    except KeyError:
        raise NoReverseMatch()

    url_parameters = _generate_url_parameters(kwargs) if kwargs else ""
    url = '/'.join([root_path, api_path, url_parameters])
    return force_text(url)
예제 #7
0
파일: fields.py 프로젝트: Relrin/aiorest-ws
    def to_internal_value(self, data):
        """
        Validate that the input is a decimal number and return a Decimal
        instance.
        """
        data = force_text(data).strip()
        if len(data) > self.MAX_STRING_LENGTH:
            self.raise_error('max_string_length')

        try:
            value = decimal.Decimal(data)
        except decimal.DecimalException:
            self.raise_error('invalid')

        # Check for NaN. It is the only value that isn't equal to itself,
        # so we can use this to identify NaN values.
        if value != value:
            self.raise_error('invalid')

        # Check for infinity and negative infinity.
        if value in (decimal.Decimal('Inf'), decimal.Decimal('-Inf')):
            self.raise_error('invalid')

        return self.validate_precision(value)
예제 #8
0
    def to_internal_value(self, data):
        """
        Validate that the input is a decimal number and return a Decimal
        instance.
        """
        data = force_text(data).strip()
        if len(data) > self.MAX_STRING_LENGTH:
            self.raise_error('max_string_length')

        try:
            value = decimal.Decimal(data)
        except decimal.DecimalException:
            self.raise_error('invalid')

        # Check for NaN. It is the only value that isn't equal to itself,
        # so we can use this to identify NaN values.
        if value != value:
            self.raise_error('invalid')

        # Check for infinity and negative infinity.
        if value in (decimal.Decimal('Inf'), decimal.Decimal('-Inf')):
            self.raise_error('invalid')

        return self.validate_precision(value)
예제 #9
0
def test_force_text_failed(value, kwargs, exception_cls):
    with pytest.raises(exception_cls):
        force_text(value, **kwargs)
예제 #10
0
def test_force_text(value, kwargs, expected):
    assert force_text(value, **kwargs) == expected
예제 #11
0
def capfirst(x):
    return x and force_text(x)[0].upper() + force_text(x)[1:]