Beispiel #1
0
 def to_python(self, value):
     if isinstance(value, int):
         return value
     if isinstance(value, six.string_types) is False:
         raise exceptions.ValidationError(self.error_messages['invalid'] %
                                          value)
     try:
         return int(value)
     except ValueError:
         msg = self.error_messages['invalid'] % value
         raise exceptions.ValidationError(msg)
Beispiel #2
0
    def to_python(self, value):
        if self.decimal_places is None:
            raise exceptions.ValidationError('Need to set decimal_places')
        if value is None or isinstance(value, decimal.Decimal):
            return value
        try:
            value = "%s.%s" % (value[:-self.decimal_places],
                               value[-self.decimal_places:])

            return decimal.Decimal(value)
        except decimal.InvalidOperation:
            msg = self.error_messages['invalid'] % value
            raise exceptions.ValidationError(msg)
Beispiel #3
0
 def to_python(self, value):
     if isinstance(value, int):
         value = str(value)
     if isinstance(value, six.string_types) is False:
         msg = self.error_messages['invalid'] % value
         raise exceptions.ValidationError(msg)
     return value.strip()
Beispiel #4
0
 def _add_fields_names(self, attrs):
     for name, field in attrs.items():
         if isinstance(field, Field):
             if name in RESERVED_FIELD_NAMES:
                 raise exceptions.ValidationError(
                     '%s in reserved names, please chose another name.' %
                     name)
             field.name = name
Beispiel #5
0
 def _value_to_string(self, value):
     try:
         value = decimal.Decimal(str(value))
         value = str(value.quantize(decimal.Decimal('0.01')))
         value = int(value.replace('.', '').replace(',', ''))
         mask = '%0' + str(self.size) + 'd'
         return mask % value
     except:
         msg = self.error_messages['invalid'] % value
         raise exceptions.ValidationError(msg)
Beispiel #6
0
 def clean_fields(self):
     errors = {}
     for field in self._meta.fields.values():
         raw_value = getattr(self, field.name)
         try:
             setattr(self, field.name, field.clean(raw_value, self))
         except Exception as e:
             errors[field.name] = str(e)
     if errors:
         raise exceptions.ValidationError(repr(errors))
Beispiel #7
0
 def to_python(self, value):
     if isinstance(value, datetime.datetime):
         return value.date()
     if isinstance(value, datetime.date):
         return value
     try:
         return datetime.date(int(value[:4]), int(value[4:6]),
                              int(value[6:]))
     except Exception:
         msg = self.error_messages['invalid'] % value
         raise exceptions.ValidationError(msg)
Beispiel #8
0
    def validate(self, value):
        """
        Validates value and throws ValidationError. Subclasses should override
        this to provide validation logic.
        """
        if self.size is None:
            msg = self.error_messages['invalid_size']
            raise exceptions.ValidationError(msg)

        if self.choices:
            options = map(lambda x: x[0], self.choices)
            if value not in options:
                msg = self.error_messages['invalid_choice'] % value
                raise exceptions.ValidationError(msg)

        str_value = self._value_to_string(value)
        if len(str_value) > self.size:
            raise exceptions.ValidationError(
                'Wrong formated size, '
                'max size: %s, '
                'formated value: %s, '
                'formated size: %s' % (self.size, str_value, len(str_value)))
Beispiel #9
0
 def run_validators(self, value):
     errors = []
     for validator in self.validators:
         try:
             validator(value)
         except exceptions.ValidationError as e:
             if hasattr(e, 'code') and e.code in self.error_messages:
                 message = self.error_messages[e.code]
                 if e.params:
                     message = message % e.params
                 errors.append(message)
             else:
                 errors.extend(e.messages)
     if errors:
         raise exceptions.ValidationError(errors)
Beispiel #10
0
 def to_python(self, value):
     if value is None:
         return None
     if isinstance(value, datetime.time):
         return value
     if isinstance(value, datetime.datetime):
         return value.time()
     try:
         if value is not None:
             if len(value) is 4:
                 parsed = datetime.time(int(value[:2]), int(value[2:]))
             if len(value) is 6:
                 parsed = datetime.time(int(value[:2]), int(value[2:4]),
                                        int(value[4:]))
             return parsed
     except ValueError:
         msg = self.error_messages['invalid_time'] % value
         raise exceptions.ValidationError(msg)
Beispiel #11
0
 def _init_validate(self):
     if self.size is None:
         raise exceptions.ValidationError('Size cannot be null.')
Beispiel #12
0
 def _validate_string(self, string):
     string_length = len(string)
     if string_length != self._meta.total_size:
         msg = 'String with wrong size, needed: %s, passed: %s' % (
             self._meta.total_size, string_length)
         raise exceptions.ValidationError(msg)