def validate(self, value, model_instance): super(URLField, self).validate(value, model_instance) if self.empty(value): return parsed = urlparse(value) if not all((parsed.scheme, parsed.hostname)): raise ValidationError('invalid_url', self) if self.schemes and parsed.scheme.lower() not in self.schemes: schemes = ', '.join(self.schemes) raise ValidationError('invalid_scheme', self, schemes=schemes)
def validate(self, value, model_instance): """ Validates a coerced value (ie, passed through to_python) and throws a ValidationError if invalid. """ if self.required and self.empty(value): raise ValidationError('required', self) if self.id and any(c in value for c in INVALID_PATH_CHARS): raise ValidationError('invalid_path', self)
def validate(self, value, model_instance): super(GitObjectField, self).validate(value, model_instance) oid = model_instance.__dict__[self.name] try: obj = model_instance._meta.workspace.repo[oid] except (ValueError, KeyError): raise ValidationError('invalid_oid', self) if self.type and not isinstance(obj, self.type): raise ValidationError('invalid_type', self, type=self.type.__name__)
def to_python(self, value): if value is None: return None # we should only allow whole numbers. so we coerce to float first, then # check to see if it's divisible by 1 without a remainder try: value = float(value) except ValueError: raise ValidationError('invalid_int', self) if value % 1 != 0: raise ValidationError('invalid_int', self) return int(value)
def to_python(self, value): if value is None: return value if isinstance(value, time): return value if isinstance(value, basestring): try: return isodate.parse_iso_time(value) except isodate.InvalidFormat: raise ValidationError('invalid_format', self) except isodate.InvalidDate: raise ValidationError('invalid', self)
def to_python(self, value): if value is None: return None try: return float(value) except ValueError: raise ValidationError('invalid_float', self)
def to_python(self, value): if value is None: return None if isinstance(value, dict): return value try: return json.loads(value) except ValueError, e: raise ValidationError(e)
def to_python(self, value): if value is None: return None if type(value) == float: value = str(value) try: return decimal.Decimal(value) except decimal.InvalidOperation: raise ValidationError('invalid_decimal', self)
def to_python(self, value): if value is None: return value if isinstance(value, datetime): return value if isinstance(value, date): return datetime(value.year, value.month, value.day) if isinstance(value, basestring): try: return isodate.parse_iso_datetime(value) except isodate.InvalidFormat: # we also accept a date-only string try: return isodate.parse_iso_date(value) except isodate.InvalidFormat: raise ValidationError('invalid_format', self) except isodate.InvalidDate: raise ValidationError('invalid', self)
def validate(self, value, model_instance): super(EmailField, self).validate(value, model_instance) email_re = re.compile( r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+" r"(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]' r'|\\[\001-011\013\014\016-\177])*"' # quoted-string r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}' r'[A-Z0-9])?\.)+[A-Z]{2,6}\.?$', # domain re.IGNORECASE) if not email_re.match(value): raise ValidationError('invalid_email', self)
def to_python(self, value): if not isinstance(value, (basestring, pygit2.Oid, pygit2.Object)): raise ValidationError('invalid_object', self) if isinstance(value, pygit2.Oid): return value.hex return value
def validate(self, value, model_instance): super(SlugField, self).validate(value, model_instance) slug_re = re.compile(r'^[-\w]+$') if not slug_re.match(value): raise ValidationError('invalid_slug', self)