示例#1
0
 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)
示例#2
0
    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)
示例#3
0
 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__)
示例#4
0
 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)
示例#5
0
    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)
示例#6
0
 def to_python(self, value):
     if value is None:
         return None
     try:
         return float(value)
     except ValueError:
         raise ValidationError('invalid_float', self)
示例#7
0
 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)
示例#8
0
 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)
示例#9
0
    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)
示例#10
0
    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)
示例#11
0
 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
示例#12
0
 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)