Beispiel #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)
Beispiel #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)
Beispiel #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
Beispiel #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
Beispiel #5
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)
Beispiel #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)
Beispiel #7
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)
Beispiel #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)
Beispiel #9
0
def test_force_text_failed(value, kwargs, exception_cls):
    with pytest.raises(exception_cls):
        force_text(value, **kwargs)
Beispiel #10
0
def test_force_text(value, kwargs, expected):
    assert force_text(value, **kwargs) == expected
Beispiel #11
0
def capfirst(x):
    return x and force_text(x)[0].upper() + force_text(x)[1:]