def _translate(self, obj, skip_required=False): """ First checks if the obj is None or already a datetime object Returns that if true. Otherwise assumes that it is a string and attempts to parse it out using the formats in self.valid_formats and the datetime.strptime method Additionally it strips out any whitespace from the beginning and end of the input before attempting to parse out a datetime string :param unicode obj: The input that is being translated :return: The parse datetime object :rtype: datetime """ if isinstance(obj, datetime): return obj obj = obj.strip() for date_format in self.valid_formats: try: return datetime.strptime(obj, date_format) except ValueError: continue raise TranslationException( self.error_message or 'The object ({0}) could not be parsed as a datetime ' 'string using the formats {1}'.format(obj, self.valid_formats))
def _translate(self, obj, skip_required=False): try: return list(obj) except TypeError: raise TranslationException( self.error_message or '{0} requires an iterable. The object' ' {1} is not'.format(self.name, obj))
def _translate(self, obj, skip_required=False): obj = translate_iterable_to_single(obj) try: return self.field_type(obj) except ValueError: msg = self.error_message or 'Not a valid integer type: {0}'.format(obj) raise TranslationException(msg)
def _translate(self, obj, skip_required=False): if not hasattr(obj, 'get'): raise TranslationException(self.error_message or 'A dictionary field must have a get method ' 'that allows for retrieving an item with a default. ' 'For example a dictionary.') return obj
def _translate(self, obj, skip_required=False): # A none input should be handled by the validator if obj is None: return obj obj = super(IntegerField, self)._translate(obj, skip_required=skip_required) try: return int(obj) except ValueError: raise TranslationException( self.error_message or 'Not a valid integer type: {0}'.format(obj))
def _translate(self, obj, skip_required=False): if obj is None: # let the validation handle it. return obj if not isinstance(obj, ( list, set, tuple, )): raise TranslationException( self.error_message or 'A list field must be an instance of a list, ' 'tuple, or set') return obj
def _translate(self, obj, skip_required=False): # A none input should be handled by the validator if obj is None: return obj obj = super(FloatField, self)._translate(obj, skip_required=skip_required) try: return float(obj) except (ValueError, TypeError): raise TranslationException( self.error_message or 'obj is not castable to float: {0}'.format(obj))
def _translate(self, obj, skip_required=False): obj = translate_iterable_to_single(obj) if isinstance(obj, bool): return obj if isinstance(obj, six.string_types): if obj.lower() == 'false': return False elif obj.lower() == 'true': return True msg = self.error_message or ('{0} is not a valid boolean.' ' Either "true" or "false" is ' 'required (case insensitive)'.format(obj)) raise TranslationException(msg)
def _translate(self, obj, skip_required=False): # A none input should be handled by the validator obj = super(BooleanField, self)._translate(obj, skip_required=skip_required) if obj is None: return obj if isinstance(obj, bool): return obj if isinstance(obj, six.string_types): if obj.lower() == 'false': return False elif obj.lower() == 'true': return True raise TranslationException( self.error_message or '{0} is not a valid boolean. Either' ' "true" or "false" is required (case insensitive)'.format(obj))