<s> from django . conf import settings <EOL> from tastypie . exceptions import BadRequest <EOL> from urllib import urlencode <EOL> class Paginator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request_data , objects , resource_uri = None , limit = None , offset = <NUM_LIT:0> , max_limit = <NUM_LIT:1000> , collection_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . request_data = request_data <EOL> self . objects = objects <EOL> self . limit = limit <EOL> self . max_limit = max_limit <EOL> self . offset = offset <EOL> self . resource_uri = resource_uri <EOL> self . collection_name = collection_name <EOL> def get_limit ( self ) : <EOL> """<STR_LIT>""" <EOL> settings_limit = getattr ( settings , '<STR_LIT>' , <NUM_LIT:20> ) <EOL> if '<STR_LIT>' in self . request_data : <EOL> limit = self . request_data [ '<STR_LIT>' ] <EOL> elif self . limit is not None : <EOL> limit = self . limit <EOL> else : <EOL> limit = settings_limit <EOL> try : <EOL> limit = int ( limit ) <EOL> except ValueError : <EOL> raise BadRequest ( "<STR_LIT>" % limit ) <EOL> if limit == <NUM_LIT:0> : <EOL> if self . limit : <EOL> limit = self . limit <EOL> else : <EOL> limit = settings_limit <EOL> if limit < <NUM_LIT:0> : <EOL> raise BadRequest ( "<STR_LIT>" % limit ) <EOL> if self . max_limit and limit > self . max_limit : <EOL> return self . max_limit <EOL> return limit <EOL> def get_offset ( self ) : <EOL> """<STR_LIT>""" <EOL> offset = self . offset <EOL> if '<STR_LIT>' in self . request_data : <EOL> offset = self . request_data [ '<STR_LIT>' ] <EOL> try : <EOL> offset = int ( offset ) <EOL> except ValueError : <EOL> raise BadRequest ( "<STR_LIT>" % offset ) <EOL> if offset < <NUM_LIT:0> : <EOL> raise BadRequest ( "<STR_LIT>" % offset ) <EOL> return offset <EOL> def get_slice ( self , limit , offset ) : <EOL> """<STR_LIT>""" <EOL> if limit == <NUM_LIT:0> : <EOL> raise BadRequest ( "<STR_LIT>" % limit ) <EOL> return self . objects [ offset : offset + limit ] <EOL> def get_count ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . objects . count ( ) <EOL> except ( AttributeError , TypeError ) : <EOL> return len ( self . objects ) <EOL> def get_previous ( self , limit , offset ) : <EOL> """<STR_LIT>""" <EOL> if offset - limit < <NUM_LIT:0> : <EOL> return None <EOL> return self . _generate_uri ( limit , offset - limit ) <EOL> def get_next ( self , limit , offset , count ) : <EOL> """<STR_LIT>""" <EOL> if offset + limit >= count : <EOL> return None <EOL> return self . _generate_uri ( limit , offset + limit ) <EOL> def _generate_uri ( self , limit , offset ) : <EOL> if self . resource_uri is None : <EOL> return None <EOL> try : <EOL> request_params = self . request_data . copy ( ) <EOL> if '<STR_LIT>' in request_params : <EOL> del request_params [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in request_params : <EOL> del request_params [ '<STR_LIT>' ] <EOL> request_params . update ( { '<STR_LIT>' : limit , '<STR_LIT>' : offset } ) <EOL> encoded_params = request_params . urlencode ( ) <EOL> except AttributeError : <EOL> request_params = { } <EOL> for k , v in self . request_data . items ( ) : <EOL> if isinstance ( v , unicode ) : <EOL> request_params [ k ] = v . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> request_params [ k ] = v <EOL> if '<STR_LIT>' in request_params : <EOL> del request_params [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in request_params : <EOL> del request_params [ '<STR_LIT>' ] <EOL> request_params . update ( { '<STR_LIT>' : limit , '<STR_LIT>' : offset } ) <EOL> encoded_params = urlencode ( request_params ) <EOL> return '<STR_LIT>' % ( <EOL> self . resource_uri , <EOL> encoded_params <EOL> ) <EOL> def page ( self ) : <EOL> """<STR_LIT>""" <EOL> limit = self . get_limit ( ) <EOL> offset = self . get_offset ( ) <EOL> count = self . get_count ( ) <EOL> objects = self . get_slice ( limit , offset ) <EOL> meta = { <EOL> '<STR_LIT>' : offset , <EOL> '<STR_LIT>' : limit , <EOL> '<STR_LIT>' : count , <EOL> } <EOL> if limit : <EOL> meta [ '<STR_LIT>' ] = self . get_previous ( limit , offset ) <EOL> meta [ '<STR_LIT>' ] = self . get_next ( limit , offset , count ) <EOL> return { <EOL> self . collection_name : objects , <EOL> '<STR_LIT>' : meta , <EOL> } </s>
