@classmethod<EOL><INDENT>def paginate_query(self, query, count, offset=None, sort=None):<DEDENT>
assert isinstance(query, peewee.Query)<EOL>assert isinstance(count, int)<EOL>assert isinstance(offset, (str, int, type(None)))<EOL>assert isinstance(sort, (list, set, tuple, type(None)))<EOL>fields = query.model._meta.get_primary_keys()<EOL>if len(fields) == <NUM_LIT:0>:<EOL><INDENT>raise peewee.ProgrammingError(<EOL>'<STR_LIT>')<EOL><DEDENT>if len(fields) > <NUM_LIT:1>:<EOL><INDENT>raise peewee.ProgrammingError(<EOL>'<STR_LIT>')<EOL><DEDENT>if offset is not None:<EOL><INDENT>query = query.where(fields[<NUM_LIT:0>] >= offset)<EOL><DEDENT>order_bys = []<EOL>if sort:<EOL><INDENT>for field, direction in sort:<EOL><INDENT>if not isinstance(direction, str):<EOL><INDENT>raise ValueError("<STR_LIT>".format(field))<EOL><DEDENT>direction = direction.lower().strip()<EOL>if direction not in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>raise ValueError("<STR_LIT>".format(field))<EOL><DEDENT>order_by = peewee.SQL(field)<EOL>order_by = getattr(order_by, direction)()<EOL>order_bys += [order_by]<EOL><DEDENT><DEDENT>order_bys += [fields[<NUM_LIT:0>].asc()]<EOL>query = query.order_by(*order_bys)<EOL>query = query.limit(count)<EOL>return query<EOL>
Apply pagination to query

:attr query: Instance of `peewee.Query`
:attr count: Max rows to return
:attr offset: Pagination offset, str/int
:attr sort: List of tuples, e.g. [('id', 'asc')]

:returns: Instance of `peewee.Query`