Python django.forms.DurationField() Examples

The following are 30 code examples of django.forms.DurationField(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.forms , or try the search function .
Example #1
Source File: __init__.py    From python2017 with MIT License 5 votes vote down vote up
def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.DurationField,
        }
        defaults.update(kwargs)
        return super(DurationField, self).formfield(**defaults) 
Example #2
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_formfield(self):
        field = models.DurationField()
        self.assertIsInstance(field.formfield(), forms.DurationField) 
Example #3
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_invalid_string(self):
        field = models.DurationField()
        with self.assertRaises(exceptions.ValidationError) as cm:
            field.clean('not a datetime', None)
        self.assertEqual(cm.exception.code, 'invalid')
        self.assertEqual(
            cm.exception.message % cm.exception.params,
            "'not a datetime' value has an invalid format. "
            "It must be in [DD] [HH:[MM:]]ss[.uuuuuu] format."
        ) 
Example #4
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_durationfield_prepare_value(self):
        field = DurationField()
        td = datetime.timedelta(minutes=15, seconds=30)
        self.assertEqual(field.prepare_value(td), duration_string(td))
        self.assertEqual(field.prepare_value('arbitrary'), 'arbitrary')
        self.assertIsNone(field.prepare_value(None)) 
Example #5
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_durationfield_integer_value(self):
        f = DurationField()
        self.assertEqual(datetime.timedelta(0, 10800), f.clean(10800)) 
Example #6
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_overflow_translation(self):
        msg = "Le nombre de jours doit ĂȘtre entre {min_days} et {max_days}.".format(
            min_days=datetime.timedelta.min.days,
            max_days=datetime.timedelta.max.days,
        )
        with translation.override('fr'):
            with self.assertRaisesMessage(ValidationError, msg):
                DurationField().clean('1000000000 00:00:00') 
Example #7
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_overflow(self):
        msg = "The number of days must be between {min_days} and {max_days}.".format(
            min_days=datetime.timedelta.min.days,
            max_days=datetime.timedelta.max.days,
        )
        f = DurationField()
        with self.assertRaisesMessage(ValidationError, msg):
            f.clean('1000000000 00:00:00')
        with self.assertRaisesMessage(ValidationError, msg):
            f.clean('-1000000000 00:00:00') 
Example #8
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_durationfield_clean(self):
        f = DurationField()
        self.assertEqual(datetime.timedelta(seconds=30), f.clean('30'))
        self.assertEqual(datetime.timedelta(minutes=15, seconds=30), f.clean('15:30'))
        self.assertEqual(datetime.timedelta(hours=1, minutes=15, seconds=30), f.clean('1:15:30'))
        self.assertEqual(
            datetime.timedelta(days=1, hours=1, minutes=15, seconds=30, milliseconds=300),
            f.clean('1 1:15:30.3')
        ) 
Example #9
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_formfield(self):
        field = models.DurationField()
        self.assertIsInstance(field.formfield(), forms.DurationField) 
Example #10
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_invalid_string(self):
        field = models.DurationField()
        with self.assertRaises(exceptions.ValidationError) as cm:
            field.clean('not a datetime', None)
        self.assertEqual(cm.exception.code, 'invalid')
        self.assertEqual(
            cm.exception.message % cm.exception.params,
            "'not a datetime' value has an invalid format. "
            "It must be in [DD] [HH:[MM:]]ss[.uuuuuu] format."
        ) 
Example #11
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_durationfield_integer_value(self):
        f = DurationField()
        self.assertEqual(datetime.timedelta(0, 10800), f.clean(10800)) 
Example #12
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_durationfield_render(self):
        self.assertWidgetRendersTo(
            DurationField(initial=datetime.timedelta(hours=1)),
            '<input id="id_f" type="text" name="f" value="01:00:00" required>',
        ) 
Example #13
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_overflow_translation(self):
        msg = "Le nombre de jours doit ĂȘtre entre {min_days} et {max_days}.".format(
            min_days=datetime.timedelta.min.days,
            max_days=datetime.timedelta.max.days,
        )
        with translation.override('fr'):
            with self.assertRaisesMessage(ValidationError, msg):
                DurationField().clean('1000000000 00:00:00') 
Example #14
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_overflow(self):
        msg = "The number of days must be between {min_days} and {max_days}.".format(
            min_days=datetime.timedelta.min.days,
            max_days=datetime.timedelta.max.days,
        )
        f = DurationField()
        with self.assertRaisesMessage(ValidationError, msg):
            f.clean('1000000000 00:00:00')
        with self.assertRaisesMessage(ValidationError, msg):
            f.clean('-1000000000 00:00:00') 
Example #15
Source File: test_durationfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_durationfield_clean(self):
        f = DurationField()
        self.assertEqual(datetime.timedelta(seconds=30), f.clean('30'))
        self.assertEqual(datetime.timedelta(minutes=15, seconds=30), f.clean('15:30'))
        self.assertEqual(datetime.timedelta(hours=1, minutes=15, seconds=30), f.clean('1:15:30'))
        self.assertEqual(
            datetime.timedelta(days=1, hours=1, minutes=15, seconds=30, milliseconds=300),
            f.clean('1 1:15:30.3')
        ) 
Example #16
Source File: __init__.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def get_internal_type(self):
        return "DurationField" 
Example #17
Source File: __init__.py    From python2017 with MIT License 5 votes vote down vote up
def get_db_converters(self, connection):
        converters = []
        if not connection.features.has_native_duration_field:
            converters.append(connection.ops.convert_durationfield_value)
        return converters + super(DurationField, self).get_db_converters(connection) 
Example #18
Source File: __init__.py    From python2017 with MIT License 5 votes vote down vote up
def get_internal_type(self):
        return "DurationField" 
Example #19
Source File: __init__.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.DurationField,
        }
        defaults.update(kwargs)
        return super(DurationField, self).formfield(**defaults) 
Example #20
Source File: __init__.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def get_db_converters(self, connection):
        converters = []
        if not connection.features.has_native_duration_field:
            converters.append(connection.ops.convert_durationfield_value)
        return converters + super(DurationField, self).get_db_converters(connection) 
Example #21
Source File: __init__.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def get_internal_type(self):
        return "DurationField" 
Example #22
Source File: __init__.py    From python with Apache License 2.0 5 votes vote down vote up
def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.DurationField,
        }
        defaults.update(kwargs)
        return super(DurationField, self).formfield(**defaults) 
Example #23
Source File: __init__.py    From python with Apache License 2.0 5 votes vote down vote up
def get_db_converters(self, connection):
        converters = []
        if not connection.features.has_native_duration_field:
            converters.append(connection.ops.convert_durationfield_value)
        return converters + super(DurationField, self).get_db_converters(connection) 
Example #24
Source File: __init__.py    From python with Apache License 2.0 5 votes vote down vote up
def get_internal_type(self):
        return "DurationField" 
Example #25
Source File: __init__.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def formfield(self, **kwargs):
        return super().formfield(**{
            'form_class': forms.DurationField,
            **kwargs,
        }) 
Example #26
Source File: __init__.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def get_internal_type(self):
        return "DurationField" 
Example #27
Source File: __init__.py    From bioforum with MIT License 5 votes vote down vote up
def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.DurationField,
        }
        defaults.update(kwargs)
        return super().formfield(**defaults) 
Example #28
Source File: __init__.py    From bioforum with MIT License 5 votes vote down vote up
def get_internal_type(self):
        return "DurationField" 
Example #29
Source File: __init__.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.DurationField,
        }
        defaults.update(kwargs)
        return super(DurationField, self).formfield(**defaults) 
Example #30
Source File: __init__.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def get_db_converters(self, connection):
        converters = []
        if not connection.features.has_native_duration_field:
            converters.append(connection.ops.convert_durationfield_value)
        return converters + super(DurationField, self).get_db_converters(connection)