Exemplo n.º 1
0
def ascii_only_to_unicode_stripped(val):
    if isinstance(val, unicode):
        try:
            val.encode('ascii', 'strict')  # just to check against encoding errors
        except UnicodeEncodeError as exc:
            raise FieldValueError(
                public_message=INVALID_FIELD_TEMPLATE_MSG.format(value=val, exc=exc))
    else:
        assert isinstance(val, str)
        try:
            val = val.decode('ascii', 'strict')
        except UnicodeDecodeError as exc:
            raise FieldValueError(
                public_message=INVALID_FIELD_TEMPLATE_MSG.format(value=val, exc=exc))
    assert isinstance(val, unicode)
    return val.strip()
Exemplo n.º 2
0
    def _clean_integer(value):
        """
        Validate, whether passed value is an integer in range
        0 to 23, so it can be set as an hour part in datetime.time
        object.

        Although the `Time` type of column represents datetime.time
        in Python, MySQL backend interprets passed integers
        differently than datetime.time. E.g. it converts a single-
        and double-digit numbers to seconds (not to hours, like
        datetime.time). Next places to the left reflect successively
        minutes and hours.

        In order to simplify this behavior, the method takes a number
        between 0 and 23 and converts it to a datetime.time object,
        that represents an hour only.

        Args:
            `value`:
                validated hour as integer.

        Returns:
            a datetime.time object.

        Raises:
            A FieldValueError if the validated number is out
            of expected range.
        """
        try:
            return datetime.time(value)
        except ValueError as exc:
            raise FieldValueError(
                public_message=INVALID_FIELD_TEMPLATE_MSG.format(value=value,
                                                                 exc=exc))
Exemplo n.º 3
0
 def _clean_string(cls, value):
     try:
         return datetime.datetime.strptime(value,
                                           cls.hour_minute_format).time()
     except (TypeError, ValueError) as exc:
         raise FieldValueError(
             public_message=INVALID_FIELD_TEMPLATE_MSG.format(value=value,
                                                              exc=exc))
Exemplo n.º 4
0
def _verified_as_str(val):
    __verify_is_str(val)
    if isinstance(val, bytes):                                                                         #3: remove the *if* statement (with its whole block)
        try:
            val = val.decode('utf-8', 'strict')
        except UnicodeDecodeError as exc:
            raise FieldValueError(
                public_message=INVALID_FIELD_TEMPLATE_MSG.format(value=val, exc=exc))
    return val
Exemplo n.º 5
0
def to_stripped(val):
    if not isinstance(val, basestring):
        raise FieldValueError(public_message='Illegal type of value for a string-type field.')
    if isinstance(val, str):
        try:
            val = val.decode('utf-8', 'strict')
        except UnicodeDecodeError as exc:
            raise FieldValueError(
                public_message=INVALID_FIELD_TEMPLATE_MSG.format(value=val, exc=exc))
    assert isinstance(val, unicode)
    return val.strip()