def validate_invalid_parameters(extension): if not extension.exten: raise InvalidParametersError(['Exten required']) if not extension.context: raise InvalidParametersError(['Context required']) if extension.commented not in [True, False]: raise InvalidParametersError(['Commented must be a bool'])
def _extract_datetimes(request_args): invalid_parameters = [] start = _extract_datetime('start_date', request_args, invalid_parameters) end = _extract_datetime('end_date', request_args, invalid_parameters) if invalid_parameters: raise InvalidParametersError(invalid_parameters) return start, end
def _get_sort_column_name(self, name=None): name = name or self._default_sort sort_columns = self._sort or self._columns.keys() if name not in sort_columns: raise InvalidParametersError(["ordering column '%s' does not exist" % name]) return name
def test_when_invalid_parameters_is_raised(self): expected_status_code = 400 expected_message = ["Invalid parameters: parameter"] exception = InvalidParametersError(['parameter']) response = make_error_response(exception) self.assertResponse(response, expected_status_code, expected_message)
def _validate_user_does_not_have_a_voicemail(user_voicemail): try: user_voicemail_dao.get_by_user_id(user_voicemail.user_id) raise InvalidParametersError([ 'user with id %s already has a voicemail' % user_voicemail.user_id ]) except ElementNotExistsError: pass
def _validate_empty(context): empty = [] for parameter in Context.MANDATORY: if getattr(context, parameter).strip() == '': empty.append(parameter) if len(empty) > 0: raise InvalidParametersError(empty)
def validate_data_from_api(mapping, data_dict): invalid_parameters = [] for api_key in data_dict.iterkeys(): if api_key not in mapping.values(): invalid_parameters.append(api_key) if invalid_parameters: raise InvalidParametersError(invalid_parameters)
def _check_invalid_parameters(user): invalid_parameters = [] if not user.firstname: invalid_parameters.append('firstname') if user.mobile_phone_number and not MOBILE_PHONE_NUMBER_REGEX.match( user.mobile_phone_number): invalid_parameters.append('mobile_phone_number') if user.password is not None and len(user.password) < 4: invalid_parameters.append('password') if invalid_parameters: raise InvalidParametersError(invalid_parameters)
def _check_invalid_parameters(line): invalid = [] if line.context.strip() == '': invalid.append('context cannot be empty') try: line.device_slot = int(line.device_slot) except ValueError: invalid.append('device_slot must be numeric') if line.device_slot <= 0: invalid.append('device_slot must be greater than 0') if len(invalid) > 0: raise InvalidParametersError(invalid)
def _validate_invalid_parameters(user_line): invalid_parameters = [] if not isinstance(user_line.user_id, int): invalid_parameters.append('user_id must be integer') if not isinstance(user_line.line_id, int): invalid_parameters.append('line_id must be integer') if hasattr(user_line, 'main_user') and not isinstance(user_line.main_user, bool): invalid_parameters.append('main_user must be boolean') if hasattr(user_line, 'main_line') and not isinstance(user_line.main_line, bool): invalid_parameters.append('main_line must be boolean') if invalid_parameters: raise InvalidParametersError(invalid_parameters)
def _validate_parameters(self, parameters): invalid = [] if parameters['skip'] < 0: invalid.append('skip must be a positive number or zero') if parameters['limit'] is not None and parameters['limit'] <= 0: invalid.append('limit must be a positive number') if parameters['direction'] not in self.SORT_DIRECTIONS.keys(): invalid.append("direction must be 'asc' or 'desc'") if invalid: raise InvalidParametersError(invalid)
def _check_invalid_parameters(device): invalid_parameters = [] if device.ip and not IP_REGEX.match(device.ip): invalid_parameters.append('ip') if device.mac and not MAC_REGEX.match(device.mac): invalid_parameters.append('mac') if device.options is not None: if not isinstance(device.options, dict): invalid_parameters.append('options') elif 'switchboard' in device.options and not isinstance( device.options['switchboard'], bool): invalid_parameters.append('options.switchboard') if invalid_parameters: raise InvalidParametersError(invalid_parameters)
def _convert_order_and_direction(order=None, direction=None): if direction and not order: raise InvalidParametersError("cannot use a direction without an order") if not order: return None if direction == 'asc': sort_direction = 1 elif direction == 'desc': sort_direction = -1 else: sort_direction = 1 return (order, sort_direction)
def update_from_data(self, data): parameters = data.keys() invalid = self.invalid_parameters(parameters) if len(invalid) > 0: raise InvalidParametersError(invalid) for model_field in self._MAPPING.itervalues(): model_field_value = data.get(model_field) if model_field_value is not None: setattr(self, model_field, model_field_value) for model_field in self._RELATION.itervalues(): model_field_value = data.get(model_field) if model_field_value is not None: setattr(self, model_field, model_field_value)
def _check_invalid_parameters(voicemail): invalid_parameters = [] if voicemail.name is not None and not voicemail.name: invalid_parameters.append('name') if voicemail.number is not None and not validator.is_positive_number(voicemail.number): invalid_parameters.append('number') if voicemail.max_messages is not None and not validator.is_positive_number(voicemail.max_messages): invalid_parameters.append('max_messages') if voicemail.password is not None and not validator.is_positive_number(voicemail.password): invalid_parameters.append('password') if voicemail.attach_audio is not None and not isinstance(voicemail.attach_audio, bool): invalid_parameters.append('attach_audio') if voicemail.delete_messages is not None and not isinstance(voicemail.delete_messages, bool): invalid_parameters.append('delete_messages') if voicemail.ask_password is not None and not isinstance(voicemail.ask_password, bool): invalid_parameters.append('ask_password') if invalid_parameters: raise InvalidParametersError(invalid_parameters)
def validate_order(cls, order): if order not in cls.all_columns(): raise InvalidParametersError( ["ordering column '%s' does not exist" % order])
def validate_no_device(line_id): line = line_dao.get(line_id) if line.device_id: raise InvalidParametersError(['A device is still associated to the line'])
def validate_direction(cls, direction): if direction not in cls.directions(): raise InvalidParametersError(["direction must be 'asc' or 'desc'"])
def _check_invalid(self): if self.invalid: raise InvalidParametersError(self.invalid)
def _validate_invalid_parameters(user_voicemail): if not isinstance(user_voicemail.enabled, bool): raise InvalidParametersError(['enabled must be a boolean'])
def _validate_user_has_line(user_voicemail): user_lines = user_line_dao.find_all_by_user_id(user_voicemail.user_id) if len(user_lines) == 0: raise InvalidParametersError([ 'user with id %s does not have any line' % user_voicemail.user_id ])
def _validate_user_not_associated_with_line(user_line): existing = user_line_dao.find_all_by_user_id(user_line.user_id) if len(existing) > 0: raise InvalidParametersError(['user is already associated to this line'])
def _is_allowed_to_dissociate(user_line): if user_line.main_user is True and user_line_dao.line_has_secondary_user(user_line): raise InvalidParametersError(['There are secondary users associated to this line'])
def _validate_no_extra_params(data, LIVE_RELOAD_PARAM): if len(data) > 1: raise InvalidParametersError('A single parameter %s is expected' % LIVE_RELOAD_PARAM)
def validate_extension_in_range(extension): if not context_services.is_extension_valid_for_context(extension): raise InvalidParametersError([ 'exten %s not inside range of context %s' % (extension.exten, extension.context) ])
def validate_destination_type(func_key): if not func_key_type_dao.find_destination_type_for_name(func_key.destination): raise InvalidParametersError(['destination of type %s does not exist' % func_key.destination])
def validate_type(func_key): if not func_key_type_dao.find_type_for_name(func_key.type): raise InvalidParametersError(['type %s does not exist' % func_key.type])
def _validate_exten(extension): if not extension.exten.isdigit(): raise InvalidParametersError( ['Alphanumeric extensions are not supported']) return extension.exten
def _check_invalid_parameters(self, parameters): invalid = self.invalid_parameters(parameters) if len(invalid) > 0: raise InvalidParametersError(invalid)
def _check_invalid_context(line): context = context_services.find_by_name(line.context) if not context: raise InvalidParametersError( ['context %s does not exist' % line.context])