Created
September 4, 2012 19:51
-
-
Save boris317/3625632 to your computer and use it in GitHub Desktop.
Subclass of wtforms.core.FloatField that handles floats with comma delimiters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| For reference wtforms.core.FloatField | |
| ------------------------------------- | |
| class FloatField(Field): | |
| widget = widgets.TextInput() | |
| def __init__(self, label=None, validators=None, **kwargs): | |
| super(FloatField, self).__init__(label, validators, **kwargs) | |
| def _value(self): | |
| if self.raw_data: | |
| return self.raw_data[0] | |
| elif self.data is not None: | |
| return text_type(self.data) | |
| else: | |
| return '' | |
| def process_formdata(self, valuelist): | |
| if valuelist: | |
| try: | |
| self.data = float(valuelist[0]) | |
| except ValueError: | |
| self.data = None | |
| raise ValueError(self.gettext('Not a valid float value')) | |
| """ | |
| class CommaFloatField(FloatField): | |
| """ | |
| Subclass that handles floats of this format 1.2 or 1,2. | |
| """ | |
| def process_formdata(self, valuelist): | |
| if valuelist: | |
| try: | |
| self.data = float(valuelist[0].replace(",", ".")) | |
| except ValueError: | |
| self.data = None | |
| raise ValueError(self.gettext('Not a valid float value')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment