<s> try : <EOL> set <EOL> except NameError : <EOL> from sets import Set as set <EOL> from django import template <EOL> from django . template . base import TOKEN_BLOCK <EOL> from django . http import Http404 <EOL> from django . core . paginator import Paginator , InvalidPage <EOL> from django . conf import settings <EOL> register = template . Library ( ) <EOL> DEFAULT_PAGINATION = getattr ( settings , '<STR_LIT>' , <NUM_LIT:20> ) <EOL> DEFAULT_WINDOW = getattr ( settings , '<STR_LIT>' , <NUM_LIT:4> ) <EOL> DEFAULT_ORPHANS = getattr ( settings , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> INVALID_PAGE_RAISES_404 = getattr ( settings , <EOL> '<STR_LIT>' , False ) <EOL> def do_autopaginate ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> expr = lambda obj : ( obj . token_type == TOKEN_BLOCK and len ( obj . split_contents ( ) ) > <NUM_LIT:0> and obj . split_contents ( ) [ <NUM_LIT:0> ] == "<STR_LIT>" ) <EOL> multiple_paginations = len ( filter ( expr , parser . tokens ) ) > <NUM_LIT:0> <EOL> split = token . split_contents ( ) <EOL> as_index = None <EOL> context_var = None <EOL> for i , bit in enumerate ( split ) : <EOL> if bit == '<STR_LIT>' : <EOL> as_index = i <EOL> break <EOL> if as_index is not None : <EOL> try : <EOL> context_var = split [ as_index + <NUM_LIT:1> ] <EOL> except IndexError : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" % split [ <NUM_LIT:0> ] ) <EOL> del split [ as_index : as_index + <NUM_LIT:2> ] <EOL> if len ( split ) == <NUM_LIT:2> : <EOL> return AutoPaginateNode ( split [ <NUM_LIT:1> ] , multiple_paginations = multiple_paginations ) <EOL> elif len ( split ) == <NUM_LIT:3> : <EOL> return AutoPaginateNode ( split [ <NUM_LIT:1> ] , paginate_by = split [ <NUM_LIT:2> ] , <EOL> context_var = context_var , multiple_paginations = multiple_paginations ) <EOL> elif len ( split ) == <NUM_LIT:4> : <EOL> try : <EOL> orphans = int ( split [ <NUM_LIT:3> ] ) <EOL> except ValueError : <EOL> raise template . TemplateSyntaxError ( u'<STR_LIT>' <EOL> % split [ <NUM_LIT:3> ] ) <EOL> return AutoPaginateNode ( split [ <NUM_LIT:1> ] , paginate_by = split [ <NUM_LIT:2> ] , orphans = orphans , <EOL> context_var = context_var , multiple_paginations = multiple_paginations ) <EOL> else : <EOL> raise template . TemplateSyntaxError ( '<STR_LIT>' + <EOL> '<STR_LIT>' % split [ <NUM_LIT:0> ] ) <EOL> class AutoPaginateNode ( template . Node ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , queryset_var , multiple_paginations , paginate_by = DEFAULT_PAGINATION , <EOL> orphans = DEFAULT_ORPHANS , context_var = None ) : <EOL> self . queryset_var = template . Variable ( queryset_var ) <EOL> if isinstance ( paginate_by , int ) : <EOL> self . paginate_by = paginate_by <EOL> else : <EOL> self . paginate_by = template . Variable ( paginate_by ) <EOL> self . orphans = orphans <EOL> self . context_var = context_var <EOL> self . multiple_paginations = multiple_paginations <EOL> def render ( self , context ) : <EOL> if self . multiple_paginations or context . has_key ( '<STR_LIT>' ) : <EOL> page_suffix = '<STR_LIT>' % self . queryset_var <EOL> else : <EOL> page_suffix = '<STR_LIT>' <EOL> key = self . queryset_var . var <EOL> value = self . queryset_var . resolve ( context ) <EOL> if isinstance ( self . paginate_by , int ) : <EOL> paginate_by = self . paginate_by <EOL> else : <EOL> paginate_by = self . paginate_by . resolve ( context ) <EOL> paginator = Paginator ( value , paginate_by , self . orphans ) <EOL> try : <EOL> page_obj = paginator . page ( context [ '<STR_LIT>' ] . page ( page_suffix ) ) <EOL> except InvalidPage : <EOL> if INVALID_PAGE_RAISES_404 : <EOL> raise Http404 ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> context [ key ] = [ ] <EOL> context [ '<STR_LIT>' ] = True <EOL> return u'<STR_LIT>' <EOL> if self . context_var is not None : <EOL> context [ self . context_var ] = page_obj . object_list <EOL> else : <EOL> context [ key ] = page_obj . object_list <EOL> context [ '<STR_LIT>' ] = paginator <EOL> context [ '<STR_LIT>' ] = page_obj <EOL> context [ '<STR_LIT>' ] = page_suffix <EOL> return u'<STR_LIT>' <EOL> def paginate ( context , window = DEFAULT_WINDOW , hashtag = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> paginator = context [ '<STR_LIT>' ] <EOL> page_obj = context [ '<STR_LIT>' ] <EOL> page_suffix = context . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> page_range = paginator . page_range <EOL> records = { '<STR_LIT>' : <NUM_LIT:1> + ( page_obj . number - <NUM_LIT:1> ) * paginator . per_page } <EOL> records [ '<STR_LIT>' ] = records [ '<STR_LIT>' ] + paginator . per_page - <NUM_LIT:1> <EOL> if records [ '<STR_LIT>' ] + paginator . orphans >= paginator . count : <EOL> records [ '<STR_LIT>' ] = paginator . count <EOL> first = set ( page_range [ : window ] ) <EOL> last = set ( page_range [ - window : ] ) <EOL> current_start = page_obj . number - <NUM_LIT:1> - window <EOL> if current_start < <NUM_LIT:0> : <EOL> current_start = <NUM_LIT:0> <EOL> current_end = page_obj . number - <NUM_LIT:1> + window <EOL> if current_end < <NUM_LIT:0> : <EOL> current_end = <NUM_LIT:0> <EOL> current = set ( page_range [ current_start : current_end ] ) <EOL> pages = [ ] <EOL> if len ( first . intersection ( current ) ) == <NUM_LIT:0> : <EOL> first_list = list ( first ) <EOL> first_list . sort ( ) <EOL> second_list = list ( current ) <EOL> second_list . sort ( ) <EOL> pages . extend ( first_list ) <EOL> diff = second_list [ <NUM_LIT:0> ] - first_list [ - <NUM_LIT:1> ] <EOL> if diff == <NUM_LIT:2> : <EOL> pages . append ( second_list [ <NUM_LIT:0> ] - <NUM_LIT:1> ) <EOL> elif diff == <NUM_LIT:1> : <EOL> pass <EOL> else : <EOL> pages . append ( None ) <EOL> pages . extend ( second_list ) <EOL> else : <EOL> unioned = list ( first . union ( current ) ) <EOL> unioned . sort ( ) <EOL> pages . extend ( unioned ) <EOL> if len ( current . intersection ( last ) ) == <NUM_LIT:0> : <EOL> second_list = list ( last ) <EOL> second_list . sort ( ) <EOL> diff = second_list [ <NUM_LIT:0> ] - pages [ - <NUM_LIT:1> ] <EOL> if diff == <NUM_LIT:2> : <EOL> pages . append ( second_list [ <NUM_LIT:0> ] - <NUM_LIT:1> ) <EOL> elif diff == <NUM_LIT:1> : <EOL> pass <EOL> else : <EOL> pages . append ( None ) <EOL> pages . extend ( second_list ) <EOL> else : <EOL> differenced = list ( last . difference ( current ) ) <EOL> differenced . sort ( ) <EOL> pages . extend ( differenced ) <EOL> to_return = { <EOL> '<STR_LIT>' : settings . MEDIA_URL , <EOL> '<STR_LIT>' : context [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : pages , <EOL> '<STR_LIT>' : records , <EOL> '<STR_LIT>' : page_obj , <EOL> '<STR_LIT>' : paginator , <EOL> '<STR_LIT>' : hashtag , <EOL> '<STR_LIT>' : paginator . count > paginator . per_page , <EOL> '<STR_LIT>' : page_suffix , <EOL> } <EOL> if '<STR_LIT>' in context : <EOL> getvars = context [ '<STR_LIT>' ] . GET . copy ( ) <EOL> if '<STR_LIT>' % page_suffix in getvars : <EOL> del getvars [ '<STR_LIT>' % page_suffix ] <EOL> if len ( getvars . keys ( ) ) > <NUM_LIT:0> : <EOL> to_return [ '<STR_LIT>' ] = "<STR_LIT>" % getvars . urlencode ( ) <EOL> else : <EOL> to_return [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return to_return <EOL> except KeyError , AttributeError : <EOL> return { } <EOL> register . inclusion_tag ( '<STR_LIT>' , takes_context = True ) ( <EOL> paginate ) <EOL> register . tag ( '<STR_LIT>' , do_autopaginate ) </s>
