Code

Ticket #2443: durationfield.2.diff

File durationfield.2.diff, 1.1 KB (added by Marty Alchin <gulopine@…>, 7 years ago)

Updated DurationField location to actually work (I hadn't tested the other one)

Line 
1Index: __init__.py
2===================================================================
3--- __init__.py (revision 4944)
4+++ __init__.py (working copy)
5@@ -688,6 +688,24 @@
6     def get_manipulator_field_objs(self):
7         return [curry(oldforms.FloatField, max_digits=self.max_digits, decimal_places=self.decimal_places)]
8 
9+class DurationField(FloatField):
10+
11+    def __init__(self, *args, **kwargs):
12+        super(DurationField, self).__init__(max_digits=20, decimal_places=6)
13+
14+    def get_internal_type(self):
15+        return "FloatField"
16+
17+    def to_python(self, value):
18+        print "to_python: %r" % value
19+        try:
20+            return datetime.timedelta(seconds=value)
21+        except TypeError:
22+            raise validators.ValidationError('This value must be a real number.')
23+        except OverflowError:
24+            raise validators.ValidationError('The maximum allowed value is %s' % datetime.timedelta.max)
25+       
26+
27 class ImageField(FileField):
28     def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
29         self.width_field, self.height_field = width_field, height_field