def validate(self, value): # Make sure that shit gets encoded properly if isinstance(value, unicode): value = value.encode(self.encoding) value = super(Text, self).validate(value) if value is None: return None if not isinstance(value, basestring) and value is not None: raise ValidationError('{} is not a string'.format(type(value))) if self.max_length: if len(value) > self.max_length: raise ValidationError('{} is longer than {} characters'.format(self.column_name, self.max_length)) if self.min_length: if len(value) < self.min_length: raise ValidationError('{} is shorter than {} characters'.format(self.column_name, self.min_length)) return value
def validate(self, value): """ Returns a cleaned and validated value. Raises a ValidationError if there's a problem """ if value is None: if self.has_default: return self.get_default() elif self.required: raise ValidationError('{} - None values are not allowed'.format(self.column_name or self.db_field)) return value
def validate(self, value): val = super(Integer, self).validate(value) if val is None: return try: return long(val) except (TypeError, ValueError): raise ValidationError( "{} can't be converted to integral value".format(value))
def to_database(self, value): value = super(DateTime, self).to_database(value) if value is None: return if not isinstance(value, datetime): if not self.strict and isinstance(value, (basestring, int, float)): value = datetime.fromtimestamp(float(value)) else: raise ValidationError("'{}' is not a datetime object".format(value)) tmp = time.mktime(value.timetuple()) # gives us a float with .0 # microtime is a 6 digit int, so we bring it down to .xxx and add it to the float TS tmp = tmp + float(value.microsecond) / 1000000 return tmp