Commits

Kevin Wetzels committed 5be2e87

Add model field MinutesField

Comments (0)

Files changed (2)

 # -*- coding: utf-8 -*-
 from django.db import models
 
+from forms import MinutesField as MinutesFormField
+
+
+class MinutesField(models.PositiveIntegerField):
+    """
+    This is simply an extension of a ``PositiveIntegerField`` that will use a 
+    ``forms.MinutesField`` in forms.
+    """
+
+    description = "A field representing a duration in minutes"
+
+    def formfield(self, **kwargs):
+        defaults = {'form_class': MinutesFormField}
+        defaults.update(kwargs)
+        return super(MinutesField, self).formfield(**defaults)
+
 
 class EnumIntegerField(models.PositiveIntegerField):
     """
     possible choices.
     """
 
+    description = "An enumeration of integer values"
+
     def __init__(self, *args, **kwargs):
         if 'enum' in kwargs:
             self.enum = kwargs.pop('enum')
     possible choices.
     """
 
+    description = "An enumeration of character values"
+
     def __init__(self, *args, **kwargs):
         if 'enum' in kwargs:
             self.enum = kwargs.pop('enum')
 try:
     # Let South know it should be able to handle these fields
     from south.modelsinspector import add_introspection_rules
+    add_introspection_rules([], ["^stdfields\.fields\.MinutesField"])
     add_introspection_rules([], ["^stdfields\.fields\.EnumIntegerField"])
     add_introspection_rules([], ["^stdfields\.fields\.EnumCharField"])
 except ImportError:
 # -*- coding: utf-8 -*-
 """
-Test cases for MinutesField and MinutesWidget.
+Test cases for forms.MinutesField, forms.MinutesWidget, fields.MinutesField,
+fields.EnumIntegerField and fields.EnumCharField.
 """
 from django.test import TestCase
-from django.forms import ValidationError
+from django.forms import ValidationError, ModelForm
 from django.db import models
 
 from stdfields.models import Enumeration
-from stdfields.fields import EnumIntegerField, EnumCharField
-from stdfields.forms import MinutesField
+from stdfields.fields import MinutesField, EnumIntegerField, EnumCharField
+from stdfields.forms import MinutesField as MinutesFormField
 from stdfields.widgets import MinutesWidget
 
 # -- --------------------------------------------------------------------------
 # -- MinutesField + MinutesWidget test
 
+class Task(models.Model):
+    minutes = MinutesField()
+
+
+class TaskForm(ModelForm):
+
+    class Meta:
+        model = Task
+
+
 class MinutesFieldTest(TestCase):
 
     def test_minutes_field(self):
-        f = MinutesField()
+        t = Task(minutes=121)
+        self.assertEqual(121, t.minutes)
+        t.save()
+        self.assertEqual(121, t.minutes)
+
+    def test_form_field(self):
+        form = TaskForm()
+        self.assertEqual(MinutesFormField, type(form.fields['minutes']))
+
+
+class MinutesFormFieldTest(TestCase):
+
+    def test_minutes_field(self):
+        f = MinutesFormField()
         self.assertEqual(121, f.clean(121))
         self.assertEqual(121, f.clean('2:01'))
         self.assertEqual(121, f.clean('2:1'))
             self.assertEqual(minutes + i, f.clean('8:%d' % (i)))
 
     def test_minutes_field_invalid(self):
-        f = MinutesField()
+        f = MinutesFormField()
         self._should_raise_validation_error(f, '2:60')
         self._should_raise_validation_error(f, 'x:60')
         self._should_raise_validation_error(f, 'x:y')