Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Decimal validator #109

Open
lambacck opened this issue Apr 4, 2016 · 0 comments
Open

Add Decimal validator #109

lambacck opened this issue Apr 4, 2016 · 0 comments

Comments

@lambacck
Copy link
Contributor

lambacck commented Apr 4, 2016

Needs some work on docstring basically:

class Decimal(RangeValidator):
    """Convert a value to an integer.
    Example::
        >>> Decimal.to_python('10')
        10
        >>> Decimal.to_python('ten')
        Traceback (most recent call last):
            ...
        Invalid: Please enter an integer value
        >>> Int(min=5).to_python('6')
        6
        >>> Int(max=10).to_python('11')
        Traceback (most recent call last):
            ...
        Invalid: Please enter a number that is 10 or smaller
    """

    messages = dict(
        decimal=_('Please enter a decimal value'))

    def _convert_to_python(self, value, state):
        try:
            return decimal.Decimal(value)
        except (ValueError, TypeError):
            raise Invalid(self.message('decimal', state), value, state)

    _convert_from_python = _convert_to_python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant