def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
 try:
  paginator = context['paginator']
  page_obj = context['page_obj']
  page_suffix = context.get('page_suffix', '')
  page_range = paginator.page_range
  records = {'first': (1 + ((page_obj.number - 1) * paginator.per_page))}
  records['last'] = ((records['first'] + paginator.per_page) - 1)
  if ((records['last'] + paginator.orphans) >= paginator.count):
   records['last'] = paginator.count
  first = set(page_range[:window])
  last = set(page_range[(- window):])
  current_start = ((page_obj.number - 1) - window)
  if (current_start < 0):
   current_start = 0
  current_end = ((page_obj.number - 1) + window)
  if (current_end < 0):
   current_end = 0
  current = set(page_range[current_start:current_end])
  pages = []
  if (len(first.intersection(current)) == 0):
   first_list = list(first)
   first_list.sort()
   second_list = list(current)
   second_list.sort()
   pages.extend(first_list)
   diff = (second_list[0] - first_list[(-1)])
   if (diff == 2):
    pages.append((second_list[0] - 1))
   elif (diff == 1):
    pass
   else:
    pages.append(None)
   pages.extend(second_list)
  else:
   unioned = list(first.union(current))
   unioned.sort()
   pages.extend(unioned)
  if (len(current.intersection(last)) == 0):
   second_list = list(last)
   second_list.sort()
   diff = (second_list[0] - pages[(-1)])
   if (diff == 2):
    pages.append((second_list[0] - 1))
   elif (diff == 1):
    pass
   else:
    pages.append(None)
   pages.extend(second_list)
  else:
   differenced = list(last.difference(current))
   differenced.sort()
   pages.extend(differenced)
  to_return = {'MEDIA_URL': settings.MEDIA_URL, 'request': context['request'], 'pages': pages, 'records': records, 'page_obj': page_obj, 'paginator': paginator, 'hashtag': hashtag, 'is_paginated': (paginator.count > paginator.per_page), 'page_suffix': page_suffix}
  if ('request' in context):
   getvars = context['request'].GET.copy()
   if (('page%s' % page_suffix) in getvars):
    del getvars[('page%s' % page_suffix)]
   if (len(getvars.keys()) > 0):
    to_return['getvars'] = ('&%s' % getvars.urlencode())
   else:
    to_return['getvars'] = ''
  return to_return
 except KeyError as AttributeError:
  return {}

def paginate ( context , window = DEFAULT_WINDOW , hashtag = '' ) : try : paginator = context [ 'paginator' ] page_obj = context [ 'page_obj' ] page_suffix = context . get ( 'page_suffix' , '' ) page_range = paginator . page_range records = { 'first' : ( 1 + ( ( page_obj . number - 1 ) * paginator . per_page ) ) } records [ 'last' ] = ( ( records [ 'first' ] + paginator . per_page ) - 1 ) if ( ( records [ 'last' ] + paginator . orphans ) >= paginator . count ) : records [ 'last' ] = paginator . count first = set ( page_range [ : window ] ) last = set ( page_range [ ( - window ) : ] ) current_start = ( ( page_obj . number - 1 ) - window ) if ( current_start < 0 ) : current_start = 0 current_end = ( ( page_obj . number - 1 ) + window ) if ( current_end < 0 ) : current_end = 0 current = set ( page_range [ current_start : current_end ] ) pages = [ ] if ( len ( first . intersection ( current ) ) == 0 ) : first_list = list ( first ) first_list . sort ( ) second_list = list ( current ) second_list . sort ( ) pages . extend ( first_list ) diff = ( second_list [ 0 ] - first_list [ ( - 1 ) ] ) if ( diff == 2 ) : pages . append ( ( second_list [ 0 ] - 1 ) ) elif ( diff == 1 ) : pass else : pages . append ( None ) pages . extend ( second_list ) else : unioned = list ( first . union ( current ) ) unioned . sort ( ) pages . extend ( unioned ) if ( len ( current . intersection ( last ) ) == 0 ) : second_list = list ( last ) second_list . sort ( ) diff = ( second_list [ 0 ] - pages [ ( - 1 ) ] ) if ( diff == 2 ) : pages . append ( ( second_list [ 0 ] - 1 ) ) elif ( diff == 1 ) : pass else : pages . append ( None ) pages . extend ( second_list ) else : differenced = list ( last . difference ( current ) ) differenced . sort ( ) pages . extend ( differenced ) to_return = { 'MEDIA_URL' : settings . MEDIA_URL , 'request' : context [ 'request' ] , 'pages' : pages , 'records' : records , 'page_obj' : page_obj , 'paginator' : paginator , 'hashtag' : hashtag , 'is_paginated' : ( paginator . count > paginator . per_page ) , 'page_suffix' : page_suffix } if ( 'request' in context ) : getvars = context [ 'request' ] . GET . copy ( ) if ( ( 'page%s' % page_suffix ) in getvars ) : del getvars [ ( 'page%s' % page_suffix ) ] if ( len ( getvars . keys ( ) ) > 0 ) : to_return [ 'getvars' ] = ( '&%s' % getvars . urlencode ( ) ) else : to_return [ 'getvars' ] = '' return to_return except KeyError as AttributeError : return { }
return a paginated page for the given objects .