Commits

Hynek Cernoch  committed 2e6489f

Fixed bugs and inconsistencies related to empty values in forms. Closes #25

  • Participants
  • Parent commits b74f97c

Comments (0)

Files changed (1)

File livesettings/values.py

     def to_editor(self, value):
         "Returns a value suitable for display in a form widget"
         if value == NOTSET:
-            return NOTSET
+            return NOTSET  # TODO this was not a good idea for an editor: "<object object 0x123..>"
         return unicode(value)
 
 ###############
                
             def clean(self, value):
                 value = super(forms.DecimalField, self).clean(value)
+                if value == None:
+                    return Decimal("0")
                 try:
                     return unicode(Decimal(value))
                 except:
                     raise forms.ValidationError('This value must be a decimal number.')
 
     def to_python(self, value):
-        if value==NOTSET:
+        if value == NOTSET:
             return Decimal("0")
 
         try:
 class DurationValue(Value):
 
     class field(forms.CharField):
+
+        def __init__(self, *args, **kwargs):
+            kwargs['required'] = False
+            forms.CharField.__init__(self, *args, **kwargs)
+
         def clean(self, value):
+            if value == '':
+                value = 0
             try:
                 return datetime.timedelta(seconds=float(value))
             except (ValueError, TypeError):
             forms.FloatField.__init__(self, *args, **kwargs)
 
     def to_python(self, value):
-        if value == NOTSET:
+        if value in (NOTSET, None) :
             value = 0
         return float(value)
 
             forms.IntegerField.__init__(self, *args, **kwargs)
 
     def to_python(self, value):
-        if value == NOTSET:
+        if value in (NOTSET, None):
             value = 0
         return int(value)
 
             forms.DecimalField.__init__(self, 100, 0, 5, 2, *args, **kwargs)
 
         def clean(self, value):
+            if value == '':
+                value = 0
             value = super(forms.DecimalField, self).clean(value)
             try:
                 value = Decimal(value)
     class field(forms.IntegerField):
 
         def __init__(self, *args, **kwargs):
+            kwargs['required'] = False
             kwargs['min_value'] = 0
             forms.IntegerField.__init__(self, *args, **kwargs)
 
             return load_module("%s.%s" % (value, module))
 
     def to_python(self, value):
-        if value == NOTSET:
-            v = {}
+        if value in (NOTSET, ''):
+            v = {}    # TODO this was probably not a good idea
         else:
             try:
                 v = load_module(value)
         return v
 
     def get_db_prep_save(self, value):
-        return value.__name__
+        return getattr(value, '__name__', '')
 
     def to_editor(self, value):
         if value == NOTSET: