Exemplo n.º 1
0
    def to_python(self, value):
        """
        Converts the input value into the timedelta Python data type, raising
        django.core.exceptions.ValidationError if the data can't be converted.
        Returns the converted value as a timedelta.
        """

        # Note that value may be coming from the database column or a serializer so we should
        # handle a timedelta, string or an integer
        if value is None:
            return value

        if isinstance(value, timedelta):
            return value

        if isinstance(value, six.integer_types):
            return timedelta(microseconds=value)

        # Try to parse the value
        str_val = smart_text(value)
        if isinstance(str_val, six.string_types):
            try:
                return str_to_timedelta(str_val)
            except ValueError:
                raise exceptions.ValidationError(
                    self.default_error_messages['invalid'])

        raise exceptions.ValidationError(
            self.default_error_messages['unknown_type'])
Exemplo n.º 2
0
    def to_python(self, value):
        """
        Converts the input value into the timedelta Python data type, raising
        django.core.exceptions.ValidationError if the data can't be converted.
        Returns the converted value as a timedelta.
        """

        # Note that value may be coming from the database column or a serializer so we should
        # handle a timedelta, string or an integer
        if value is None:
            return value

        if isinstance(value, timedelta):
            return value

        if isinstance(value, six.integer_types):
            return timedelta(microseconds=value)

        # As of Django 1.10, it appears that during some aggregation operations
        # we may receive decimal.Decimal instances via from_db_value.
        # WARNING: potentially lossy conversion
        if isinstance(value, decimal.Decimal):
            return timedelta(microseconds=float(value))

        # Try to parse the value
        str_val = smart_text(value)
        if isinstance(str_val, six.string_types):
            try:
                return str_to_timedelta(str_val)
            except ValueError:
                raise exceptions.ValidationError(self.default_error_messages['invalid'])

        raise exceptions.ValidationError(self.default_error_messages['unknown_type'])
Exemplo n.º 3
0
 def testInputAll(self):
     delta = timestring.str_to_timedelta(
         "1 year, 10 months, 3 weeks, 2 days, 3:40:50")
     days = ((1 * 365) + (10 * 30) + (3 * 7) + 2)
     seconds = ((3 * 60 * 60) + (40 * 60) + 50)
     self.assertEqual(days, delta.days)
     self.assertEqual(seconds, delta.seconds)
Exemplo n.º 4
0
    def to_python(self, value):
        """
        Converts the input value into the timedelta Python data type, raising
        django.core.exceptions.ValidationError if the data can't be converted.
        Returns the converted value as a timedelta.
        """

        # Note that value may be coming from the database column or a serializer so we should
        # handle a timedelta, string or an integer
        if value is None:
            return value

        if isinstance(value, timedelta):
            return value

        if isinstance(value, int) or isinstance(value, long):
            return timedelta(seconds=value)

        # Try to parse the value
        str_val = smart_str(value)
        if isinstance(str_val, basestring):
            try:
                return str_to_timedelta(str_val)
            except ValueError:
                raise exceptions.ValidationError(self.default_error_messages['invalid'])

        raise exceptions.ValidationError(self.default_error_messages['unknown_type'])
Exemplo n.º 5
0
 def clean(self, value):
     """
     Returns a datetime.timedelta object.
     """
     super(DurationField, self).clean(value)
     try:
         return str_to_timedelta(value)
     except ValueError, e:
         raise ValidationError(self.default_error_messages['invalid'])
Exemplo n.º 6
0
 def clean(self, value):
     """
     Returns a datetime.timedelta object.
     """
     super(DurationField, self).clean(value)
     try:
         return str_to_timedelta(value)
     except ValueError:
         raise ValidationError(self.default_error_messages['invalid'])
Exemplo n.º 7
0
 def testInputAll(self):
     delta = timestring.str_to_timedelta("1Y 10M 3w 2d 3m")
     days = (
         (1 * 365) +
         (10 * 30) +
         (3 * 7) +
         2
     )
     seconds = (
         (3 * 60)
     )
     self.assertEqual(
         days, delta.days
     )
     self.assertEqual(
         seconds, delta.seconds
     )
Exemplo n.º 8
0
 def testInputAllAbbreviated(self):
     delta = timestring.str_to_timedelta("2Y 9M 1w 20d 0m")
     days = (
         (2 * 365) +
         (9 * 30) +
         (1 * 7) +
         20
     )
     seconds = (
         (0 * 60 * 60) 
     )
     self.assertEqual(
         days, delta.days
     )
     self.assertEqual(
         seconds, delta.seconds
     )
Exemplo n.º 9
0
 def testInputAll(self):
     delta = timestring.str_to_timedelta("1 year, 10 months, 3 weeks, 2 days, 3:40:50")
     days = (
         (1 * 365) +
         (10 * 30) +
         (3 * 7) +
         2
     )
     seconds = (
         (3 * 60 * 60) +
         (40 * 60) +
         50
     )
     self.assertEqual(
         days, delta.days
     )
     self.assertEqual(
         seconds, delta.seconds
     )
Exemplo n.º 10
0
 def testInputAllAbbreviated(self):
     delta = timestring.str_to_timedelta("2y 9m 1w 20d 0:10:39")
     days = (
                 (2 * 365) +
                 (9 * 30) +
                 (1 * 7) +
                 20
             )
     seconds = (
                 (0 * 60 * 60) +
                 (10 * 60) + 
                 39
             )
     self.assertEquals(
                 days, delta.days
             )
     self.assertEquals(
                 seconds, delta.seconds
             )
Exemplo n.º 11
0
 def testInputTimeSecondsMicroseconds(self):
     delta = timestring.str_to_timedelta("11:20:22.000098")
     seconds = (11 * 60 * 60) + (20 * 60) + 22
     self.assertEqual(seconds, delta.seconds)
     self.assertEqual(98, delta.microseconds)
Exemplo n.º 12
0
 def testInputTimeSeonds(self):
     delta = timestring.str_to_timedelta("12:21:24")
     seconds = (12 * 60 * 60) + (21 * 60) + 24
     self.assertEqual(seconds, delta.seconds)
Exemplo n.º 13
0
 def testInputTime(self):
     delta = timestring.str_to_timedelta("10:23")
     seconds = (10 * 60 * 60) + (23 * 60)
     self.assertEqual(seconds, delta.seconds)
Exemplo n.º 14
0
 def to_python(self, value):
     try:
         return str_to_timedelta(value)
     except ValueError:
         raise ValidationError(self.default_error_messages['invalid'])
Exemplo n.º 15
0
 def testInputTimeSecondsMicroseconds(self):
     delta = timestring.str_to_timedelta("11:20:22.000098")
     seconds = (11 * 60 * 60) + (20 * 60) + 22
     self.assertEqual(seconds, delta.seconds)
     self.assertEqual(98, delta.microseconds)
Exemplo n.º 16
0
 def testInputTimeSecondsMicroseconds(self):
     delta = timestring.str_to_timedelta("11m")
     seconds = (11 * 60)
     self.assertEqual(seconds, delta.seconds)
Exemplo n.º 17
0
 def testInputTimeSeonds(self):
     delta = timestring.str_to_timedelta("12h 21m")
     seconds = (12 * 60 * 60) + (21 * 60)
     self.assertEqual(seconds, delta.seconds)
Exemplo n.º 18
0
 def testInputTimeMicrosecondsBothPadZeros(self):
     delta = timestring.str_to_timedelta("11:20:22.0160")
     self.assertEqual(16000, delta.microseconds)
Exemplo n.º 19
0
 def testTimedeltaStrRoundtrip(self):
     for td in self.test_tds:
         td_str = timedelta_to_string(td)
         td_from_str = timestring.str_to_timedelta(td_str)
         self.assertEqual(td_from_str, td)
Exemplo n.º 20
0
 def testInputDaysOnly(self):
     delta = timestring.str_to_timedelta("24 days")
     self.assertEqual(
         24, delta.days
     )
Exemplo n.º 21
0
 def testInputAll(self):
     delta = timestring.str_to_timedelta("1Y 10M 3w 2d 3m")
     days = ((1 * 365) + (10 * 30) + (3 * 7) + 2)
     seconds = ((3 * 60))
     self.assertEqual(days, delta.days)
     self.assertEqual(seconds, delta.seconds)
Exemplo n.º 22
0
 def testInputTimeSecondsMicroseconds(self):
     delta = timestring.str_to_timedelta("11m")
     seconds = (11 * 60)
     self.assertEqual(seconds, delta.seconds)
Exemplo n.º 23
0
 def testInputTimeMicrosecondsBothPadZeros(self):
     delta = timestring.str_to_timedelta("11:20:22.0160")
     self.assertEqual(16000, delta.microseconds)
Exemplo n.º 24
0
 def to_python(self, value):
     try:
         return str_to_timedelta(value)
     except ValueError, e:
         raise ValidationError(self.default_error_messages['invalid'])
Exemplo n.º 25
0
 def testInputAllAbbreviated(self):
     delta = timestring.str_to_timedelta("2Y 9M 1w 20d 0m")
     days = ((2 * 365) + (9 * 30) + (1 * 7) + 20)
     seconds = ((0 * 60 * 60))
     self.assertEqual(days, delta.days)
     self.assertEqual(seconds, delta.seconds)
Exemplo n.º 26
0
 def testInputTime(self):
     delta = timestring.str_to_timedelta("10h 23m")
     seconds = (10 * 60 * 60) + (23 * 60)
     self.assertEqual(seconds, delta.seconds)
Exemplo n.º 27
0
 def testTimedeltaStrRoundtrip(self):
     for td in self.test_tds:
         td_str = str(td)
         td_from_str = timestring.str_to_timedelta(td_str)
         self.assertEquals(td_from_str, td)
Exemplo n.º 28
0
 def testInputDaysOnly(self):
     delta = timestring.str_to_timedelta("24 days")
     self.assertEqual(24, delta.days)