Cleanup and regex enhancement of CurrencyField

This commit is contained in:
Sebastian Lohff 2011-10-26 01:38:28 +02:00
parent 38068e1ebb
commit 27962ad107
1 changed files with 1 additions and 42 deletions

View File

@ -3,21 +3,6 @@ from django.core.exceptions import ValidationError
import re import re
from decimal import Decimal, InvalidOperation from decimal import Decimal, InvalidOperation
#class CurrencyField(forms.IntegerField):
# default_error_message = {
# 'invalid': _(u'Enter a number.'),
# }
# def __init__(self, positive=True, *args, **kwargs):
# super(IntegerField, self).__init__(*args, **kwargs)
# self.positive = positive
# def to_python(self, value):
# value = super(IntegerField, self).to_python(value)
# if value in validators.EMPTY_VALUES:
# return None
# try:
# value = float(value.replace(",", "."))
# except:
# pass
class CurrencyInput (forms.TextInput): class CurrencyInput (forms.TextInput):
def render(self, name, value, attrs=None): def render(self, name, value, attrs=None):
@ -32,41 +17,15 @@ class CurrencyInput (forms.TextInput):
class CurrencyField (forms.RegexField): class CurrencyField (forms.RegexField):
""" Represents a currency field for django forms... or something. """ """ Represents a currency field for django forms... or something. """
widget = CurrencyInput widget = CurrencyInput
currencyRe = re.compile(r'^[0-9]{1,5}([,\.][0-9][0-9]?)?$') currencyRe = re.compile(r'^(\+|-)?[0-9]{1,5}([,\.][0-9][0-9]?)?$')
#currencyRe = re.compile(r'^[0-9]{1,5}(.[0-9][0-9]?)?$')
# currencyRe = re.compile(r"^[0-9]")
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(CurrencyField, self).__init__( super(CurrencyField, self).__init__(
self.currencyRe, None, None, *args, **kwargs) self.currencyRe, None, None, *args, **kwargs)
#class CurrencyField(forms.FloatField):
# widget = CurrencyInput
# moneyre = re.compile(r"^[0-9]{1,5}([\.][0-9][0-9])?$")
# def __init__(self, positive=False, allowPerCent=False, *args, **kwargs):
# super(forms.FloatField, self).__init__(*args, **kwargs)
# self.positive = positive
# self.allowPerCent = allowPerCent
def to_python(self, value): def to_python(self, value):
#return super(forms.FloatField, self).to_python(value)
#raise ValidationError("WTF WTF WTF %s " % value)
#value = value.replace(",", ".")
#try:
# value = super(forms.FloatField, self).to_python(value)
#except ValidationError, e:
# raise NotImplementedError("%s %s" % (e, value))
#return value
value = value.replace(",", ".")
try: try:
value = Decimal(value) value = Decimal(value)
except (ValueError, TypeError, InvalidOperation): except (ValueError, TypeError, InvalidOperation):
raise ValidationError("Bitte gib eine Zahl ein") raise ValidationError("Bitte gib eine Zahl ein")
# if not self.allowPerCent:
# tmpval = 100.0*value
# if tmpval == int(tmpval):
# raise ValidationError("Deine Zahl sollte nicht mehr als zwei Nachkommastellen haben. %s %s %s" % (tmpval, int(tmpval), tmpval-int(tmpval)))
# if self.positive and self.value < 0.0:
# raise ValidationError("Bitte gib eine positive Zahl an.")
return value return value
def clean(self, value): def clean(self, value):
value = super(CurrencyField, self).clean(value) value = super(CurrencyField, self).clean(value)