Commits

Kevin Wetzels committed 4c7d4e0

Fix bug with integer numbers being interpreted as minutes instead of hours for forms.MinutesField

Comments (0)

Files changed (2)

stdfields/forms.py

             value = (hours * 60) + minutes
         else:
             value = value.replace(',', '.')
-            if '.' in value:
-                parts = value.split('.')
-                try:
-                    hours = int(parts[0])
-                    fraction = int(parts[1])
-                    if not fraction in (5, 25, 50, 75):
-                        msg = self.error_messages['invalid']
-                        raise forms.ValidationError(msg)
-                    if fraction == 5:
-                        fraction = 50
-                    value = int((hours * 60) + (60 / 100 * fraction))
-                except (ValueError, TypeError):
+            if not '.' in value:
+                value = u'%s.0' % (value)
+            parts = value.split('.')
+            try:
+                hours = int(parts[0])
+                fraction = int(parts[1])
+                if not fraction in (0, 5, 25, 50, 75):
                     msg = self.error_messages['invalid']
                     raise forms.ValidationError(msg)
+                if fraction == 5:
+                    fraction = 50
+                value = int((hours * 60) + (60 / 100 * fraction))
+            except (ValueError, TypeError):
+                msg = self.error_messages['invalid']
+                raise forms.ValidationError(msg)
         return super(MinutesField, self).clean(value)

stdfields/tests.py

 
     def test_minutes_field(self):
         f = MinutesFormField()
-        self.assertEqual(121, f.clean(121))
+        self.assertEqual(120, f.clean(2))
         self.assertEqual(121, f.clean('2:01'))
         self.assertEqual(121, f.clean('2:1'))
+        self.assertEqual(120, f.clean('2'))
         self.assertEqual(150, f.clean('2.5'))
         self.assertEqual(150, f.clean('2,5'))
         self.assertEqual(150, f.clean('2.50'))