Flavio Curella avatar Flavio Curella committed 329d2cc Merge

Merge master into py3k

Comments (0)

Files changed (3)


 from django.db import models
+from django.core.exceptions import ValidationError
 from collections import defaultdict
 import datetime
     description = "A datetime.timedelta object"
+    def __init__(self, *args, **kwargs):
+        self._min_value = kwargs.pop('min_value', None)
+        self._max_value = kwargs.pop('max_value', None)
+        super(TimedeltaField, self).__init__(*args, **kwargs)
     def to_python(self, value):
         if (value is None) or isinstance(value, datetime.timedelta):
             return value
         return super(TimedeltaField, self).formfield(*args, **defaults)
+    def validate(self, value, model_instance):
+        super(TimedeltaField, self).validate(value, model_instance)
+        if self._min_value is not None:
+            if self._min_value > value:
+                raise ValidationError('Less than minimum allowed value')
+        if self._max_value is not None:
+            if self._max_value < value:
+                raise ValidationError('More than maximum allowed value')
     def value_to_string(self, obj):
         value = self._get_val_from_obj(obj)
         return unicode(value)


+from django.db import models
+from django.core.exceptions import ValidationError
 from unittest import TestCase
 import datetime
-from forms import TimedeltaFormField
-from widgets import TimedeltaWidget
-from helpers import *
+from .forms import TimedeltaFormField
+from .fields import TimedeltaField
+from .widgets import TimedeltaWidget
+from .helpers import *
 class TimedeltaWidgetTest(TestCase):
     def test_render(self):
         u'<input type="text" name="" value="3 days, 12 hours" />'
+class MinMaxTestModel(models.Model):
+    min = TimedeltaField(min_value=datetime.timedelta(1))
+    max = TimedeltaField(max_value=datetime.timedelta(1))
+    minmax = TimedeltaField(min_value=datetime.timedelta(1), max_value=datetime.timedelta(7))
+class TimedeltaModelFieldTest(TestCase):
+    def test_validate(self):
+        test = MinMaxTestModel(
+            min=datetime.timedelta(1),
+            max=datetime.timedelta(1),
+            minmax=datetime.timedelta(1)
+        )
+        test.full_clean() # This should have met validation requirements.
+        test.min = datetime.timedelta(hours=23)
+        self.assertRaises(ValidationError, test.full_clean)
+        test.min = datetime.timedelta(hours=25)
+        test.full_clean()
+        test.max = datetime.timedelta(11)
+        self.assertRaises(ValidationError, test.full_clean)
+        test.max = datetime.timedelta(hours=20)
+        test.full_clean()
+        test.minmax = datetime.timedelta(0)
+        self.assertRaises(ValidationError, test.full_clean)
+        test.minmax = datetime.timedelta(22)
+        self.assertRaises(ValidationError, test.full_clean)
+        test.minmax = datetime.timedelta(6, hours=23, minutes=59, seconds=59)
+        test.full_clean()
 class TimedeltaFormFieldTest(TestCase):
     def test_clean(self):
         Traceback (most recent call last):
         ValidationError: [u'Enter a valid time span: e.g. "3 days, 4 hours, 2 minutes"']
 class TimedeltaHelpersTest(TestCase):
     def test_parse(self):

File contents unchanged.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.