def _ascii_only_ldap_safe_str_strip(val): # XXX: forbidding "LDAP-unsafe" chars is needed only temporarily (until LDAP stuff is dropped) val = _verified_as_ascii_only_str(val) val = val.strip() if val.startswith('#'): raise FieldValueError( public_message='Value {value!r} starts with illegal "#" prefix.'.format(value=val)) illegal_characters = LDAP_UNSAFE_CHARACTERS.intersection(val) if illegal_characters: raise FieldValueError( public_message='Value {value!r} contains illegal character(s): {chars}.'.format( value=val, chars=', '.join(sorted("'{}'".format(ascii_str(ch)) for ch in illegal_characters)))) return val
def _to_none_if_empty_or_whitespace(val): if not isinstance(val, basestring): raise FieldValueError( public_message='Illegal type of value for a string-type field.') if not val.strip(): return None return val
def validate_time_hour_minute_only(val): hour_minute_format = '%H:%M' try: datetime.datetime.strptime(val, hour_minute_format) except (TypeError, ValueError) as exc: raise FieldValueError(public_message=invalid_field_template_msg.format(value=val, exc=exc)) return val
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()
def clean_result_value(self, value): if value is None: # Note that it is relevant only to *non-nullable* columns, # because for nullable ones our Base's metaclass ensures # that for `None` values validators are not used. raise FieldValueError(public_message=u'The value is missing') return super(_BaseNameOrTokenField, self).clean_result_value(value)
def _time_object_from_string(cls, value): try: return datetime.datetime.strptime(value, cls.time_format).time() except (TypeError, ValueError): raise FieldValueError( public_message='"{}" is not a valid *hour:minute* time ' 'specification'.format(ascii_str(value)))
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))
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))
def _verified_as_ascii_only_str(val): val = _verified_as_str(val) try: # ensure it contains only pure-ASCII characters val.encode('ascii', 'strict') except UnicodeEncodeError: raise FieldValueError( public_message='Value {value!r} contains non-ASCII characters.'.format(value=val)) return val
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
def _ascii_only_to_unicode(val): val = _to_unicode(val) try: # ensure it contains only pure-ASCII characters val.encode('ascii', 'strict') except UnicodeEncodeError as exc: raise FieldValueError( public_message='Value {value!r} contains non-ASCII characters.') assert isinstance(val, unicode) return val
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='Value {value!r} caused {exc.__class__.__name__}: {exc}'.format( value=val, exc=exc)) return val
def _to_json_or_none(val): if val is None: return None elif isinstance(val, str): val = val.strip() if not val: return None try: json.loads(val) except ValueError: raise FieldValueError(public_message="Cannot decode value: {!a} " "as JSON object.".format(val)) else: return val else: try: return json.dumps(val) except (TypeError, ValueError): raise FieldValueError(public_message="Cannot encode value {!a} " "as JSON object.".format(val))
def clean_result_value(self, value): value = super(DateTimeCustomizedField, self).clean_result_value(value) # get rid of the fractional part of seconds value = value.replace(microsecond=0) # do not accept times that are *not* representable as UNIX timestamps if value < self.min_datetime: raise FieldValueError(public_message=( 'The given date+time {} is older ' 'than the required minimum {}'.format( value.isoformat(), self.min_datetime.isoformat()))) return value
def _validate_value(self, value): forbidden_characters = self._get_additionally_forbidden_characters() illegal_characters = forbidden_characters.intersection(value) if illegal_characters: raise FieldValueError( public_message= '"{value}" contains illegal character(s): {chars}.'.format( value=ascii_str(value), chars=', '.join( sorted("'{}'".format(ascii_str(ch)) for ch in illegal_characters)))) super(EmailCustomizedField, self)._validate_value(value)
def clean_result_value(self, value): value = super(TimeHourMinuteField, self).clean_result_value(value) if isinstance(value, datetime.time): return self._clean_time_object(value) elif isinstance(value, basestring): return self._clean_string(value) elif isinstance(value, int): return self._clean_integer(value) else: raise FieldValueError( public_message='Value {value!r} is of a wrong type:' ' {val_type!r} to be validated as a proper ' 'database `Time` column record.'.format(value=value, val_type=type(value)))
def make_adjuster_using_data_spec(self, value, spec_field_name, on_too_long=None): spec_field = getattr(self.data_spec, spec_field_name) if spec_field.sensitive and on_too_long is not None: raise RuntimeError( '{!r}.sensitive is true so the `on_too_long` ' 'argument should be None (got: on_too_long={!r})'.format( spec_field, on_too_long)) try: return spec_field.clean_result_value(value) except FieldValueTooLongError as exc: # If value might be sensitive we want an exception without any details # (see: `raise FieldValueError...` at the end of this function...). if (not spec_field.sensitive) or getattr(exc, 'safe_for_sensitive', False): if on_too_long is None: raise LOGGER.warning( 'calling %r as the on-too-long callback, because: %s', on_too_long, exc) try: assert hasattr(exc, 'checked_value') assert hasattr(exc, 'max_length') assert isinstance( exc.max_length, (int, long)) and exc.max_length > 0 #3: `long`-- processed_value = on_too_long(exc.checked_value, exc.max_length) except Exception as e: if not hasattr(e, 'propagate_it_anyway'): e.propagate_it_anyway = True raise return spec_field.clean_result_value(processed_value) except Exception as exc: # If value might be sensitive we want an exception without any details # (see: `raise FieldValueError...` at the end of this function...). if (not spec_field.sensitive) or getattr(exc, 'safe_for_sensitive', False): raise assert spec_field.sensitive raise FieldValueError( public_message=spec_field.default_error_msg_if_sensitive)
def _verified_as_str(val): if not isinstance(val, str): raise FieldValueError(public_message='Illegal type of value for a string-type field.') return val
def check_if_lowercase(val): if not val.islower(): raise FieldValueError(public_message="CA label {!r} has to be lowercase.".format(val)) return val
def validate_cert_serial_number(val): if not is_cert_serial_number_valid(val): raise FieldValueError(public_message="Value {!r} is not a valid " "certificate serial number".format(val)) return val
def test_get_exception_message_field_value_error(self): exc = FieldValueError( public_message='Example FieldValueError message.') exc_message = get_exception_message(exc) self.assertEqual(exc_message, 'Example FieldValueError message.')
def _verified_as_ca_label_containing_no_uppercase(ca_label): if ca_label != ca_label.lower(): raise FieldValueError(public_message="CA label {!r} contains illegal " "upper-case characters.".format(ca_label)) return ca_label
def __verify_is_str(val): str = basestring #3-- if not isinstance(val, str): raise FieldValueError(public_message='Illegal type of value for a string-type field.')