Example #1
0
 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)
Example #2
0
 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)
Example #3
0
    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
        """
        obj = translate_iterable_to_single(obj)
        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))
Example #4
0
 def _translate(self, obj, skip_required=False):
     obj = translate_iterable_to_single(obj)
     if isinstance(obj, self.field_type):
         vals = obj.lower().split(',')
         if len(vals) == 2:
             return vals[0], ASCENDING if vals[1] == 'asc' else DESCENDING
     if obj is None:
         return obj
     raise ValueError('Not a valid sort option: %s' % obj)
Example #5
0
    def _translate(self, obj, skip_required=False):
        """
        Attempts to convert the object to a string type

        :param object obj:
        :return: The object in its string representation
        :rtype: unicode
        :raises: TranslationsException
        """
        obj = translate_iterable_to_single(obj)
        return six.text_type(obj)
Example #6
0
    def _translate(self, obj, skip_required=False):
        """
        Attempts to convert the object to a string type

        :param object obj:
        :return: The object in its string representation
        :rtype: unicode
        :raises: TranslationsException
        """
        obj = translate_iterable_to_single(obj)
        return six.text_type(obj)
Example #7
0
    def _translate(self, obj, skip_required=False):
        """
        This method is responsible for translating an input
        string or object.

        :param object obj: The input from the request
            that is being translated
        :param bool skip_required: This is being ignored for now
        :return: The object in the appropriate form
        :rtype: object
        :raises: ripozo.exceptions.TranslationException
        """
        return translate_iterable_to_single(obj)
Example #8
0
    def _translate(self, obj, skip_required=False):
        """
        This method is responsible for translating an input
        string or object.

        :param object obj: The input from the request
            that is being translated
        :param bool skip_required: This is being ignored for now
        :return: The object in the appropriate form
        :rtype: object
        :raises: ripozo.exceptions.TranslationException
        """
        return translate_iterable_to_single(obj)
Example #9
0
    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)
Example #10
0
    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)
Example #11
0
 def test_translate_iterable_to_single_not_list(self):
     """
     Tests the case where the item is not an iterable
     """
     resp = translate_iterable_to_single(1)
     self.assertEqual(resp, 1)
Example #12
0
 def test_translate_iterable_to_single(self):
     """
     Tests the case where the item is an iterable
     """
     resp = translate_iterable_to_single([1, 2])
     self.assertEqual(resp, 1)
Example #13
0
 def test_translate_iterable_to_single_not_list(self):
     """
     Tests the case where the item is not an iterable
     """
     resp = translate_iterable_to_single(1)
     self.assertEqual(resp, 1)
Example #14
0
 def test_translate_iterable_to_single(self):
     """
     Tests the case where the item is an iterable
     """
     resp = translate_iterable_to_single([1, 2])
     self.assertEqual(resp, 1)