diff options
author | Honza Král <honza.kral@gmail.com> | 2009-08-15 13:34:21 +0000 |
---|---|---|
committer | Honza Král <honza.kral@gmail.com> | 2009-08-15 13:34:21 +0000 |
commit | 05524abf1beee11bb1efa1115eca345e0d96aa83 (patch) | |
tree | 35874c79fe2e40e426253a6355e4ad5993e83d49 | |
parent | 5ebafe3fb98ca99c7b7392087b899f1053d5cf5e (diff) | |
download | django-05524abf1beee11bb1efa1115eca345e0d96aa83.tar.gz |
[soc2009/model-validation] use validators in URLField model field
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11456 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r-- | django/core/validators.py | 11 | ||||
-rw-r--r-- | django/db/models/fields/__init__.py | 7 | ||||
-rw-r--r-- | tests/modeltests/validation/models.py | 1 | ||||
-rw-r--r-- | tests/modeltests/validation/tests.py | 12 |
4 files changed, 23 insertions, 8 deletions
diff --git a/django/core/validators.py b/django/core/validators.py index fd7585552c..6001a825ee 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -24,10 +24,17 @@ url_re = re.compile( class RegexValidator(object): regex = '' + message = _(u'Enter a valid value.') + code = 'invalid' - def __init__(self, regex=None): + def __init__(self, regex=None, message=None, code=None): if regex is not None: self.regex = regex + if message is not None: + self.message = message + if code is not None: + self.code = code + if isinstance(self.regex, basestring): self.regex = re.compile(regex) @@ -36,7 +43,7 @@ class RegexValidator(object): Validates that the input matches the regular expression. """ if not self.regex.search(smart_unicode(value)): - raise ValidationError(_(u'Enter a valid value.'), code='invalid') + raise ValidationError(self.message, code=self.code) class URLValidator(RegexValidator): regex = url_re diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index a084c976c3..5c389a3b9d 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1003,13 +1003,8 @@ class TimeField(Field): class URLField(CharField): def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs): kwargs['max_length'] = kwargs.get('max_length', 200) - self.verify_exists = verify_exists CharField.__init__(self, verbose_name, name, **kwargs) - - def formfield(self, **kwargs): - defaults = {'form_class': forms.URLField, 'verify_exists': self.verify_exists} - defaults.update(kwargs) - return super(URLField, self).formfield(**defaults) + self.validators.append(validators.URLValidator(verify_exists=verify_exists)) class XMLField(TextField): def __init__(self, verbose_name=None, name=None, schema_path=None, **kwargs): diff --git a/tests/modeltests/validation/models.py b/tests/modeltests/validation/models.py index 6a08f083f1..1260417426 100644 --- a/tests/modeltests/validation/models.py +++ b/tests/modeltests/validation/models.py @@ -23,6 +23,7 @@ class ModelToValidate(models.Model): number = models.IntegerField() parent = models.ForeignKey('self', blank=True, null=True) email = models.EmailField(blank=True) + url = models.URLField(blank=True) f_with_custom_validator = models.IntegerField(blank=True, null=True, validators=[validate_answer_to_universe, ValidateFieldNotEqualsOtherField('number')]) def validate(self): diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py index 4d2a71af35..25f9d0c005 100644 --- a/tests/modeltests/validation/tests.py +++ b/tests/modeltests/validation/tests.py @@ -39,6 +39,18 @@ class BaseModelValidationTests(ValidationTestCase): mtv = ModelToValidate(number=10, name='Some Name', email='valid@email.com') self.assertEqual(None, mtv.clean()) + def test_wrong_url_value_raises_error(self): + mtv = ModelToValidate(number=10, name='Some Name', url='not a url') + self.assertFieldFailsValidationWithMessage(mtv.clean, 'url', [u'Enter a valid value.']) + + def test_correct_url_but_nonexisting_gives_404(self): + mtv = ModelToValidate(number=10, name='Some Name', url='http://google.com/we-love-microsoft.html') + self.assertFieldFailsValidationWithMessage(mtv.clean, 'url', [u'This URL appears to be a broken link.']) + + def test_correct_url_value_passes(self): + mtv = ModelToValidate(number=10, name='Some Name', url='http://www.djangoproject.com/') + self.assertEqual(None, mtv.clean()) # This will fail if there's no Internet connection + def test_text_greater_that_charfields_max_length_eaises_erros(self): mtv = ModelToValidate(number=10, name='Some Name'*100) self.assertFailsValidation(mtv.clean, ['name',]) |