Exemple #1
0
 def clean_value(self, val):
     try:
         if val is not None:
             return float(val)
         elif val is None and self.default is not None:
             return float(
                 self.default() if callable(self.default) else self.default)
     except ValueError:
         raise ValidationError("%r could not be cast to float" % val)
Exemple #2
0
 def clean_value(self, val):
     try:
         if val:
             return str(val)
         else:
             return str(
                 self.default() if self.default else uuid.uuid4().hex)
     except ValueError:
         raise ValidationError("%r could not be cast to string" % val)
Exemple #3
0
 def __setattr__(self, key, val):
     if (key not in self.__dict__ and key not in self.__class__.__dict__
             and key not in self._fields):
         matches = list(
             set(difflib.get_close_matches(key, self._prop_list, 4, 0.5)))
         error_msg = "Unexpected assignment, do you mistyped a field name \"%s\"." % key
         if matches:
             error_msg += '\n\nDid you mean one of these?  ' % '""'.join(
                 matches)
         print(self._prop_list)
         raise AttributeError(error_msg)
     if key not in self._fields:
         _attr = getattr(self, key)
         if _attr is not None and _attr.__class__.__name__ != val.__class__.__name__:
             raise ValidationError(
                 "Assigned object's (%s) type (%s) does not matches to \"%s %s\" "
                 % (key, val.__class__.__name__, _attr.__class__.__name__,
                    getattr(_attr, '_TYPE', None)))
     object.__setattr__(self, key, val)
Exemple #4
0
    def clean_value(self, val):
        """
        val =
        :param dict val: {"content":"", "name":"", "ext":"", "type":""}
        :return:
        """
        if isinstance(val, dict):
            if self.random_name:
                val['random_name'] = self.random_name
            if 'file_name' in val.keys():
                val['name'] = val.pop('file_name')
                val['content'] = val.pop('file_content')
            return self.file_manager().store_file(**val)

        # If val is not instance of dict, it should be return itself because the val is the key of
        # the file
        try:
            return str(val)
        except ValueError:
            raise ValidationError("%r could not be cast to string" % val)