<s> from math import ceil <EOL> from flask import abort <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> Model = declarative_base ( name = '<STR_LIT>' ) <EOL> class Pagination ( object ) : <EOL> def __init__ ( self , query , page , per_page , total , items ) : <EOL> self . query = query <EOL> self . page = page <EOL> self . per_page = per_page <EOL> self . total = total <EOL> self . items = items <EOL> def _paginate ( self , page ) : <EOL> if page < <NUM_LIT:1> : <EOL> abort ( <NUM_LIT> ) <EOL> offset = ( page - <NUM_LIT:1> ) * self . per_page <EOL> items = self . query . limit ( self . per_page ) . offset ( offset ) . all ( ) <EOL> if not items and page != <NUM_LIT:1> : <EOL> abort ( <NUM_LIT> ) <EOL> return Pagination ( self . query , page , self . per_page , <EOL> self . query . count ( ) , items ) <EOL> @ property <EOL> def pages ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( ceil ( self . total / float ( self . per_page ) ) ) <EOL> def prev ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . query is not None , ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return self . _paginate ( self . page - <NUM_LIT:1> ) <EOL> @ property <EOL> def prev_num ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . page - <NUM_LIT:1> <EOL> @ property <EOL> def has_prev ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . page > <NUM_LIT:1> <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . query is not None , ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return self . _paginate ( self . page + <NUM_LIT:1> ) <EOL> @ property <EOL> def has_next ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . page < self . pages <EOL> @ property <EOL> def next_num ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . page + <NUM_LIT:1> </s>
