Author: Honza_Kral
Date: 2009-06-01 10:40:18 -0500 (Mon, 01 Jun 2009)
New Revision: 10872

Modified:
   django/branches/soc2009/model-validation/django/db/models/fields/__init__.py
   django/branches/soc2009/model-validation/django/forms/models.py
   
django/branches/soc2009/model-validation/tests/modeltests/model_forms/models.py
   
django/branches/soc2009/model-validation/tests/modeltests/model_formsets/models.py
   
django/branches/soc2009/model-validation/tests/modeltests/validation/models.py
   
django/branches/soc2009/model-validation/tests/regressiontests/model_fields/tests.py
Log:
[soc2009/model-validation] Added test for field.blank

We still have to decide what to do with fields that have duplicate errors:
 - required from form
 - blank on model
This change resulted in some tests being rendered as invalid. For
example excluding a required field or doing save_as_new on InlineFormset
with non-existing instance.

Modified: 
django/branches/soc2009/model-validation/django/db/models/fields/__init__.py
===================================================================
--- 
django/branches/soc2009/model-validation/django/db/models/fields/__init__.py    
    2009-06-01 15:39:46 UTC (rev 10871)
+++ 
django/branches/soc2009/model-validation/django/db/models/fields/__init__.py    
    2009-06-01 15:40:18 UTC (rev 10872)
@@ -134,6 +134,12 @@
             raise exceptions.ValidationError(
                 ugettext_lazy("This field cannot be null."))
 
+        # cannot do if not value because of 0 passed to integer fields
+        if not self.blank and value in ( None, '' ):
+            raise exceptions.ValidationError(
+                ugettext_lazy("This field cannot be blank."))
+
+
     def clean(self, value, model_instance):
         """
         Convert the value's type and wun validation. Validation errors from 
to_python

Modified: django/branches/soc2009/model-validation/django/forms/models.py
===================================================================
--- django/branches/soc2009/model-validation/django/forms/models.py     
2009-06-01 15:39:46 UTC (rev 10871)
+++ django/branches/soc2009/model-validation/django/forms/models.py     
2009-06-01 15:40:18 UTC (rev 10872)
@@ -242,6 +242,22 @@
         opts = self._meta
         self.instance = make_instance(self, self.instance, opts.fields, 
opts.exclude)
         self.validate_unique()
+        try:
+            # FIMXE: what to do about duplicate errors? (is required etc.)
+            self.instance.clean()
+        except ValidationError, e:
+            for k, v in e.message_dict.items():
+                if k != NON_FIELD_ERRORS:
+                    self._errors.setdefault(k, []).extend(v)
+
+                    # Remove the data from the cleaned_data dict since it was 
invalid
+                    if k in self.cleaned_data:
+                        del self.cleaned_data[k]
+
+            # what about fields that don't validate but aren't present on the 
form?
+            if NON_FIELD_ERRORS in e.message_dict:
+                raise ValidationError(e.message_dict[NON_FIELD_ERRORS])
+            
         return self.cleaned_data
 
     def validate_unique(self):

Modified: 
django/branches/soc2009/model-validation/tests/modeltests/model_forms/models.py
===================================================================
--- 
django/branches/soc2009/model-validation/tests/modeltests/model_forms/models.py 
    2009-06-01 15:39:46 UTC (rev 10871)
+++ 
django/branches/soc2009/model-validation/tests/modeltests/model_forms/models.py 
    2009-06-01 15:40:18 UTC (rev 10872)
@@ -449,9 +449,9 @@
 If you call save() with invalid data, you'll get a ValueError.
 >>> f = CategoryForm({'name': '', 'slug': 'not a slug!', 'url': 'foo'})
 >>> f.errors['name']
-[u'This field is required.']
+[u'This field is required.', u'This field cannot be blank.']
 >>> f.errors['slug']
-[u"Enter a valid 'slug' consisting of letters, numbers, underscores or 
hyphens."]
+[u"Enter a valid 'slug' consisting of letters, numbers, underscores or 
hyphens.", u'This field cannot be blank.']
 >>> f.cleaned_data
 Traceback (most recent call last):
 ...
@@ -555,6 +555,8 @@
 <option value="3">Third test</option>
 </select>  Hold down "Control", or "Command" on a Mac, to select more than 
one.</li>
 >>> f = TestArticleForm({'headline': u'Test headline', 'slug': 
 >>> 'test-headline', 'pub_date': u'1984-02-06', 'writer': u'1', 'article': 
 >>> 'Hello.'}, instance=art)
+>>> f.errors
+{}
 >>> f.is_valid()
 True
 >>> test_art = f.save()
@@ -1102,16 +1104,6 @@
 
 >>> instance.delete()
 
-# Test the non-required FileField
-
->>> f = TextFileForm(data={'description': u'Assistance'})
->>> f.fields['file'].required = False
->>> f.is_valid()
-True
->>> instance = f.save()
->>> instance.file
-<FieldFile: None>
-
 >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': 
 >>> SimpleUploadedFile('test3.txt', 'hello world')}, instance=instance)
 >>> f.is_valid()
 True
@@ -1390,13 +1382,14 @@
 >>> form._errors
 {'__all__': [u'Price with this Price and Quantity already exists.']}
 
+# this form is never valid because quantity is blank=False
 >>> class PriceForm(ModelForm):
 ...     class Meta:
 ...         model = Price
 ...         exclude = ('quantity',)
 >>> form = PriceForm({'price': '6.00'})
 >>> form.is_valid()
-True
+False
 
 # Unique & unique together with null values
 >>> class BookForm(ModelForm):

Modified: 
django/branches/soc2009/model-validation/tests/modeltests/model_formsets/models.py
===================================================================
--- 
django/branches/soc2009/model-validation/tests/modeltests/model_formsets/models.py
  2009-06-01 15:39:46 UTC (rev 10871)
+++ 
django/branches/soc2009/model-validation/tests/modeltests/model_formsets/models.py
  2009-06-01 15:40:18 UTC (rev 10872)
@@ -543,10 +543,6 @@
 ...     'book_set-2-title': '',
 ... }
 
->>> formset = AuthorBooksFormSet(data, instance=Author(), save_as_new=True)
->>> formset.is_valid()
-True
-
 >>> new_author = Author.objects.create(name='Charles Baudelaire')
 >>> formset = AuthorBooksFormSet(data, instance=new_author, save_as_new=True)
 >>> [book for book in formset.save() if book.author.pk == new_author.pk]
@@ -988,19 +984,6 @@
 >>> formset._non_form_errors
 [u'Please correct the duplicate data for price and quantity, which must be 
unique.']
 
-# only the price field is specified, this should skip any unique checks since 
the unique_together is not fulfilled.
-# this will fail with a KeyError if broken.
->>> FormSet = modelformset_factory(Price, fields=("price",), extra=2)
->>> data = {
-...     'form-TOTAL_FORMS': '2',
-...     'form-INITIAL_FORMS': '0',
-...     'form-0-price': '24',
-...     'form-1-price': '24',
-... }
->>> formset = FormSet(data)
->>> formset.is_valid()
-True
-
 >>> FormSet = inlineformset_factory(Author, Book, extra=0)
 >>> author = Author.objects.order_by('id')[0]
 >>> book_ids = author.book_set.values_list('id', flat=True)

Modified: 
django/branches/soc2009/model-validation/tests/modeltests/validation/models.py
===================================================================
--- 
django/branches/soc2009/model-validation/tests/modeltests/validation/models.py  
    2009-06-01 15:39:46 UTC (rev 10871)
+++ 
django/branches/soc2009/model-validation/tests/modeltests/validation/models.py  
    2009-06-01 15:40:18 UTC (rev 10872)
@@ -18,11 +18,16 @@
     def test_missing_required_field_raises_error(self):
         mtv = ModelToValidate()
         self.assertRaises(ValidationError, mtv.clean)
+        try:
+            mtv.clean()
+        except ValidationError, e:
+            self.assertEquals(['name', 'number'], 
sorted(e.message_dict.keys()))
     
     def test_with_correct_value_model_validates(self):
-        mtv = ModelToValidate(number=10)
+        mtv = ModelToValidate(number=10, name='Some Name')
         self.assertEqual(None, mtv.clean())
 
     def test_custom_validate_method_is_called(self):
         mtv = ModelToValidate(number=11)
         self.assertRaises(ValidationError, mtv.clean)
+

Modified: 
django/branches/soc2009/model-validation/tests/regressiontests/model_fields/tests.py
===================================================================
--- 
django/branches/soc2009/model-validation/tests/regressiontests/model_fields/tests.py
        2009-06-01 15:39:46 UTC (rev 10871)
+++ 
django/branches/soc2009/model-validation/tests/regressiontests/model_fields/tests.py
        2009-06-01 15:40:18 UTC (rev 10872)
@@ -133,8 +133,12 @@
         self.assertEqual(bs.s, 'slug'*50)
 
 class ValidationTest(django.test.TestCase):
-    def test_charfield_cleans_empty_string(self):
+    def test_charfield_raises_error_on_empty_string(self):
         f = models.CharField()
+        self.assertRaises(ValidationError, f.clean, "", None)
+
+    def test_charfield_cleans_empty_string_when_blank_true(self):
+        f = models.CharField(blank=True)
         self.assertEqual('', f.clean('', None))
 
     def test_integerfield_cleans_valid_string(self):
@@ -153,8 +157,12 @@
         f = models.CharField(choices=[('a','A'), ('b','B')])
         self.assertRaises(ValidationError, f.clean, "not a", None)
 
-    def test_nullable_integerfield_cleans_none(self):
-        f = models.IntegerField(null=True)
+    def test_nullable_integerfield_raises_error_with_blank_false(self):
+        f = models.IntegerField(null=True, blank=False)
+        self.assertRaises(ValidationError, f.clean, None, None)
+
+    def test_nullable_integerfield_cleans_none_on_null_and_blank_true(self):
+        f = models.IntegerField(null=True, blank=True)
         self.assertEqual(None, f.clean(None, None))
 
     def test_integerfield_raises_error_on_empty_input(self):


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to