def validate(self): columns = self.__mapper__.columns for column in columns: value = getattr(self, column.name) if not self.type_compatible(value, column.type): raise exception.InvalidParameter( 'column %s value %r type is unexpected: %s' % (column.name, value, column.type))
def _get_fan(session, resource_id, order_by=[], limit=None, **kwargs): if isinstance(resource_id, int): resource_ids = {'eq': resource_id} elif isinstance(resource_id, list): resource_ids = {'in': resource_id} else: raise exception.InvalidParameter('parameter resource id format are not supported.') return \ utils.list_db_objects(session, Fan, order_by=order_by, limit=limit, resource_id=resource_ids, **kwargs)
def _get_history(session, gateway_id, uuid, order_by=[], limit=None, **kwargs): if isinstance(uuid, basestring): ids = {'eq': uuid} elif isinstance(uuid, list): ids = {'in': uuid} else: raise exception.InvalidParameter('parameter uuid format are not supported.') return \ utils.list_db_objects(session, SmsHistroy, order_by=order_by, limit=limit, gateway_id=gateway_id, uuid=ids, **kwargs)
def wrapper(*args, **filters): must_support_keys = set(support_keys) all_support_keys = must_support_keys | set(optional_support_keys) filter_keys = set(filters) unsupported_keys = (filter_keys - all_support_keys - set(ignore_support_keys)) if unsupported_keys: raise exception.InvalidParameter( 'filter keys %s are not supported' % str(list(unsupported_keys))) missing_keys = must_support_keys - filter_keys if missing_keys: raise exception.InvalidParameter('filter keys %s not found' % str(list(missing_keys))) filtered_filters = dict([(key, value) for key, value in filters.items() if key not in ignore_support_keys]) return func(*args, **filtered_filters)
def add_db_object(session, table, exception_when_existing=True, *args, **kwargs): """Create db object.""" with session.begin(subtransactions=True): logging.debug('session %s add object %s atributes %s to table %s', id(session), args, kwargs, table.__name__) argspec = inspect.getargspec(table.__init__) arg_names = argspec.args[1:] arg_defaults = argspec.defaults if not arg_defaults: arg_defaults = [] if not (len(arg_names) - len(arg_defaults) <= len(args) <= len(arg_names)): raise exception.InvalidParameter( 'arg names %s does not match arg values %s' % (arg_names, args)) db_keys = dict(zip(arg_names, args)) if db_keys: db_object = session.query(table).filter_by(**db_keys).first() else: db_object = None new_object = False if db_object: if exception_when_existing: raise exception.DuplicatedRecord('%s exists in table %s' % (db_keys, table.__name__)) else: db_object = table(**db_keys) new_object = True for key, value in kwargs.items(): setattr(db_object, key, value) if new_object: session.add(db_object) session.flush() db_object.initialize() db_object.validate() logging.debug('session %s db object %s added', id(session), db_object) return db_object