<s> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> from cache import Cache <EOL> class Paginator ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def get ( cls , count = <NUM_LIT:10> , q_filters = { } , search = None , start = None , model = None , order = '<STR_LIT>' , order_by = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if model == None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if callable ( model . gql ) == False : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> cache_string = "<STR_LIT>" <EOL> for q_filter in q_filters : <EOL> cache_string = cache_string + q_filter + "<STR_LIT:_>" + q_filters [ q_filter ] + "<STR_LIT:_>" <EOL> cache_string = cache_string + "<STR_LIT:index>" <EOL> c = Cache ( ) <EOL> if c . has_key ( cache_string ) : <EOL> return c [ cache_string ] <EOL> query = model . all ( ) <EOL> if len ( q_filters ) > <NUM_LIT:0> : <EOL> for q_filter in q_filters : <EOL> query . filter ( q_filter + "<STR_LIT>" , q_filters [ q_filter ] ) <EOL> if start : <EOL> if order . lower ( ) == "<STR_LIT>" . lower ( ) : <EOL> query . filter ( order_by + "<STR_LIT>" , start ) <EOL> else : <EOL> query . filter ( order_by + "<STR_LIT>" , start ) <EOL> if search : <EOL> query . search ( search ) <EOL> if order . lower ( ) == "<STR_LIT>" . lower ( ) : <EOL> query . order ( "<STR_LIT:->" + order_by ) <EOL> else : <EOL> query . order ( order_by ) <EOL> results = query . fetch ( count + <NUM_LIT:1> ) <EOL> if len ( results ) == count + <NUM_LIT:1> : <EOL> next = getattr ( results [ count - <NUM_LIT:1> ] , order_by ) <EOL> if start is not None : <EOL> rquery = model . all ( ) <EOL> for q_filter in q_filters : <EOL> rquery . filter ( q_filter + "<STR_LIT>" , q_filters [ q_filter ] ) <EOL> if search : <EOL> query . search ( search ) <EOL> if order . lower ( ) == "<STR_LIT>" . lower ( ) : <EOL> rquery . order ( order_by ) <EOL> else : <EOL> rquery . order ( "<STR_LIT:->" + order_by ) <EOL> rresults = rquery . fetch ( count ) <EOL> previous = getattr ( results [ <NUM_LIT:0> ] , order_by ) <EOL> else : <EOL> previous = None <EOL> else : <EOL> next = None <EOL> return { <EOL> "<STR_LIT>" : results , <EOL> "<STR_LIT>" : next , <EOL> "<STR_LIT>" : previous <EOL> } </s>
