def paginate_query(query, model, limit, sort_keys, marker=None, sort_dir=None, sort_dirs=None):
 if ('id' not in sort_keys):
  LOG.warn(_('Id not in sort_keys; is sort_keys unique?'))
 assert (not (sort_dir and sort_dirs))
 if ((sort_dirs is None) and (sort_dir is None)):
  sort_dir = 'asc'
 if (sort_dirs is None):
  sort_dirs = [sort_dir for _sort_key in sort_keys]
 assert (len(sort_dirs) == len(sort_keys))
 for (current_sort_key, current_sort_dir) in zip(sort_keys, sort_dirs):
  sort_dir_func = {'asc': sqlalchemy.asc, 'desc': sqlalchemy.desc}[current_sort_dir]
  try:
   sort_key_attr = getattr(model, current_sort_key)
  except AttributeError:
   raise InvalidSortKey()
  query = query.order_by(sort_dir_func(sort_key_attr))
 if (marker is not None):
  marker_values = []
  for sort_key in sort_keys:
   v = getattr(marker, sort_key)
   marker_values.append(v)
  criteria_list = []
  for i in xrange(0, len(sort_keys)):
   crit_attrs = []
   for j in xrange(0, i):
    model_attr = getattr(model, sort_keys[j])
    crit_attrs.append((model_attr == marker_values[j]))
   model_attr = getattr(model, sort_keys[i])
   if (sort_dirs[i] == 'desc'):
    crit_attrs.append((model_attr < marker_values[i]))
   elif (sort_dirs[i] == 'asc'):
    crit_attrs.append((model_attr > marker_values[i]))
   else:
    raise ValueError(_("Unknown sort direction, must be 'desc' or 'asc'"))
   criteria = sqlalchemy.sql.and_(*crit_attrs)
   criteria_list.append(criteria)
  f = sqlalchemy.sql.or_(*criteria_list)
  query = query.filter(f)
 if (limit is not None):
  query = query.limit(limit)
 return query

def paginate_query ( query , model , limit , sort_keys , marker = None , sort_dir = None , sort_dirs = None ) : if ( 'id' not in sort_keys ) : LOG . warn ( _ ( 'Id not in sort_keys; is sort_keys unique?' ) ) assert ( not ( sort_dir and sort_dirs ) ) if ( ( sort_dirs is None ) and ( sort_dir is None ) ) : sort_dir = 'asc' if ( sort_dirs is None ) : sort_dirs = [ sort_dir for _sort_key in sort_keys ] assert ( len ( sort_dirs ) == len ( sort_keys ) ) for ( current_sort_key , current_sort_dir ) in zip ( sort_keys , sort_dirs ) : sort_dir_func = { 'asc' : sqlalchemy . asc , 'desc' : sqlalchemy . desc } [ current_sort_dir ] try : sort_key_attr = getattr ( model , current_sort_key ) except AttributeError : raise InvalidSortKey ( ) query = query . order_by ( sort_dir_func ( sort_key_attr ) ) if ( marker is not None ) : marker_values = [ ] for sort_key in sort_keys : v = getattr ( marker , sort_key ) marker_values . append ( v ) criteria_list = [ ] for i in xrange ( 0 , len ( sort_keys ) ) : crit_attrs = [ ] for j in xrange ( 0 , i ) : model_attr = getattr ( model , sort_keys [ j ] ) crit_attrs . append ( ( model_attr == marker_values [ j ] ) ) model_attr = getattr ( model , sort_keys [ i ] ) if ( sort_dirs [ i ] == 'desc' ) : crit_attrs . append ( ( model_attr < marker_values [ i ] ) ) elif ( sort_dirs [ i ] == 'asc' ) : crit_attrs . append ( ( model_attr > marker_values [ i ] ) ) else : raise ValueError ( _ ( "Unknown sort direction, must be 'desc' or 'asc'" ) ) criteria = sqlalchemy . sql . and_ ( * crit_attrs ) criteria_list . append ( criteria ) f = sqlalchemy . sql . or_ ( * criteria_list ) query = query . filter ( f ) if ( limit is not None ) : query = query . limit ( limit ) return query
returns paginated statement if page is provided .