Permalink
Browse files

Merge branch 'amol' into develop

  • Loading branch information...
moschlar committed Aug 27, 2013
2 parents de9d875 + 481926d commit 46d68b792f2076e5862730abf464dbf3ec93362b
Showing with 34 additions and 1 deletion.
  1. +6 −0 tests/test_validation.py
  2. +1 −1 tw2/core/__init__.py
  3. +27 −0 tw2/core/validation.py
@@ -510,6 +510,12 @@ class TestIPAddressValidator(tb.ValidatorTest):
params = ['123.123.123.123', 'asdf']
expected = ['123.123.123.123', ValidationError]
class TestUUIDValidator(tb.ValidatorTest):
validator = UUIDValidator
attrs = [{}, {}]
params = ['34a9c4dd-1562-4413-bb27-a439292a5e61', 'asdf']
expected = [None, ValidationError]
class TestValidatorMisc(TestCase):
def testRequired(self):
@@ -27,7 +27,7 @@
RegexValidator, IntValidator, OneOfValidator, DateValidator,
DateTimeValidator, ValidationError, Invalid, EmailValidator,
UrlValidator, IpAddressValidator, StringLengthValidator,
ListLengthValidator, RangeValidator, MatchValidator,
ListLengthValidator, RangeValidator, MatchValidator, UUIDValidator,
BoolValidator, BlankValidator, safe_validate, EmptyField,
CompoundValidator,
Any, All,
@@ -7,6 +7,7 @@
import functools
import webob
import warnings
import uuid
from .i18n import _
import six
@@ -570,6 +571,32 @@ def _validate_python(self, value, state=None):
raise ValidationError('badnetblock', self)
class UUIDValidator(Validator):
"""
Confirm the value is a valid uuid and convert to uuid.UUID.
"""
msgs = {
'badregex': ('baduuid', _('Value not recognised as a UUID')),
}
regex = re.compile(\
'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
def _validate_python(self, value, state=None):
if not self.regex.search(str(value)):
raise ValidationError('badregex', self)
def _convert_to_python(self, value, state=None):
try:
return uuid.UUID( "{%s}" % value )
except ValueError:
raise ValidationError('baduuid', self)
def _convert_from_python(self, value, state=None):
return str(value)
class MatchValidator(Validator):
"""Confirm a field matches another field

0 comments on commit 46d68b7

Please sign in to comment.