<s> from django import template <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> register = template . Library ( ) <EOL> from rapidsms . conf import settings <EOL> if "<STR_LIT>" not in settings . TEMPLATE_CONTEXT_PROCESSORS : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> @ register . inclusion_tag ( "<STR_LIT>" , takes_context = True ) <EOL> def paginator ( context , page , prefix = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> dom_id = prefix + "<STR_LIT>" <EOL> page_param = prefix + "<STR_LIT>" <EOL> request = context [ "<STR_LIT>" ] <EOL> def _link ( page_number ) : <EOL> return _self_link ( request , ** { page_param : page_number } ) <EOL> def _page ( number ) : <EOL> return { <EOL> "<STR_LIT>" : number , <EOL> "<STR_LIT>" : _link ( number ) , <EOL> "<STR_LIT>" : ( page . number == number ) } <EOL> num_border_links = min ( settings . PAGINATOR_BORDER_LINKS , page . paginator . num_pages ) <EOL> num_adjacent_links = min ( settings . PAGINATOR_ADJACENT_LINKS , page . paginator . num_pages ) <EOL> last_page_number = page . paginator . num_pages + <NUM_LIT:1> <EOL> pages = set ( [ page . number ] ) <EOL> for p in range ( <NUM_LIT:1> , num_border_links + <NUM_LIT:1> ) : <EOL> pages . add ( p ) <EOL> for p in range ( last_page_number - num_border_links , last_page_number ) : <EOL> pages . add ( p ) <EOL> first_adjacent = max ( <NUM_LIT:1> , page . number - num_adjacent_links ) <EOL> last_adjacent = min ( page . number + num_adjacent_links + <NUM_LIT:1> , last_page_number ) <EOL> for p in range ( first_adjacent , last_adjacent ) : <EOL> pages . add ( p ) <EOL> page_links = [ ] <EOL> pages = sorted ( pages ) <EOL> for i in range ( len ( pages ) - <NUM_LIT:1> ) : <EOL> page_links . append ( _page ( pages [ i ] ) ) <EOL> gap = pages [ i + <NUM_LIT:1> ] - pages [ i ] <EOL> if gap == <NUM_LIT:2> : <EOL> page_links . append ( _page ( pages [ i ] + <NUM_LIT:1> ) ) <EOL> elif gap > <NUM_LIT:2> : <EOL> page_links . append ( None ) <EOL> if pages : <EOL> page_links . append ( _page ( pages [ - <NUM_LIT:1> ] ) ) <EOL> subcontext = { <EOL> "<STR_LIT>" : dom_id , <EOL> "<STR_LIT>" : page_links } <EOL> if page . number > <NUM_LIT:1> : <EOL> subcontext . update ( { <EOL> "<STR_LIT>" : _link ( page . previous_page_number ( ) ) } ) <EOL> if page . number < page . paginator . num_pages : <EOL> subcontext . update ( { <EOL> "<STR_LIT>" : _link ( page . next_page_number ( ) ) } ) <EOL> return subcontext <EOL> def _self_link ( request , ** kwargs ) : <EOL> new_kwargs = request . GET . copy ( ) <EOL> for k , v in kwargs . items ( ) : <EOL> new_kwargs [ k ] = v <EOL> kwargs_enc = new_kwargs . urlencode ( ) <EOL> return "<STR_LIT>" % ( request . path , kwargs_enc ) </s>
