Ejemplo n.º 1
0
def paginate(collection, page=None, per_page=10, item_count=None, *args, **options):
    """Paginate a collection of data
    
    If the collection is a list, it will return the slice of the list along
    with the Paginator object. If the collection is given using an ORM, the
    collection argument must be a partial representing the function to be
    used that will generate the proper query and extend properly for the
    limit/offset.
    
    Example::
    
        # In this case, Person is a SQLObject class, or it could be a list/tuple
        person_paginator, person_set = paginate(Person, page=1)
        
        set_count = int(person_paginator.current)
        total_pages = len(person_paginator)
    
    Current ORM support is limited to SQLObject and SQLAlchemy. You can use any ORM
    you'd like with the Paginator as it will give you the offset/limit data necessary
    to make your own query.
    
    **WARNING:** Unless you pass in an item_count, a count will be performed on the
    collection every time paginate is called. If using an ORM, it's suggested that
    you count the items yourself and/or cache them.
    
    """
    collection = get_wrapper(collection, *args, **options)
    if not item_count:
        item_count = len(collection)
    paginator = Paginator(item_count, per_page, page)
    subset = collection[paginator.current.first_item:paginator.current.last_item]
    
    return paginator, subset
Ejemplo n.º 2
0
def paginate(collection,
             page=None,
             per_page=10,
             item_count=None,
             *args,
             **options):
    """Paginate a collection of data
    
    If the collection is a list, it will return the slice of the list along
    with the Paginator object. If the collection is given using an ORM, the
    collection argument must be a partial representing the function to be
    used that will generate the proper query and extend properly for the
    limit/offset.
    
    Example::
    
        # In this case, Person is a SQLObject class, or it could be a list/tuple
        person_paginator, person_set = paginate(Person, page=1)
        
        set_count = int(person_paginator.current)
        total_pages = len(person_paginator)
    
    Current ORM support is limited to SQLObject and SQLAlchemy. You can use any ORM
    you'd like with the Paginator as it will give you the offset/limit data necessary
    to make your own query.
    
    **WARNING:** Unless you pass in an item_count, a count will be performed on the
    collection every time paginate is called. If using an ORM, it's suggested that
    you count the items yourself and/or cache them.
    
    """
    collection = get_wrapper(collection, *args, **options)
    if not item_count:
        item_count = len(collection)
    paginator = Paginator(item_count, per_page, page)
    subset = collection[paginator.current.first_item:paginator.current.
                        last_item]

    return paginator, subset
Ejemplo n.º 3
0
def paginate(collection, page=None, per_page=10, item_count=None, 
             query_args=None, **options):
    """Paginate a collection of data
    
    If the collection is a list, it will return the slice of the list along
    with the Paginator object. If the collection is given using an ORM, the
    collection argument must be a partial representing the function to be
    used that will generate the proper query and extend properly for the
    limit/offset.
    
    query_args will be passed to the partial and is for use in generating
    limiting conditions that your collection object may take, the remaining
    unused keyword arguments will also be passed into the collection object.
    
    Example::
    
        # In this case, Person is a SQLObject class, or it could be a 
        # list/tuple
        person_paginator, person_set = paginate(Person, page=1)
        
        set_count = int(person_paginator.current)
        total_pages = len(person_paginator)
    
    Current ORM support is limited to SQLObject and SQLAlchemy. You can use any
    ORM you'd like with the Paginator as it will give you the offset/limit 
    data necessary to make your own query.
    
    If you fail to pass in a page value, paginate will attempt to find a page
    value in the QUERY_STRING from environ, or the Routes match dict. This 
    feature only works if routes was used to resolve the URL.
    
    Example::
        
        # Using an SQLAlchemy object with assign_mapper under Pylons
        # with an order_by passed in
        c.paginator, c.people = paginate(model.Person,
                                         order_by=[model.Person.c.date])
    
    **WARNING:** Unless you pass in an item_count, a count will be performed 
    on the collection every time paginate is called. If using an ORM, it's 
    suggested that you count the items yourself and/or cache them.
    """
    # If our page wasn't passed in, attempt to pull out either a page arg from
    # the routes route path, or try the environ GET.
    if page is None:
        config = request_config()
        if hasattr(config, 'mapper_dict'):
            page = config.mapper_dict.get('page')
        if page is not None:
            if re.match(r'\d+', page):
                page = int(page)
            else:
                page = 0
        elif page is None and hasattr(config, 'environ'):
            page_match = re.match(find_page, 
                                  config.environ.get('QUERY_STRING', ''))
            if page_match:
                page = int(page_match.groups()[0])
        
        # If environ is set, and no page has been we will assume they wanted to
        # find a page value but didn't so we default to 0 now.
        if page is None:
            page = 0
    
    if query_args is None:
        query_args = []
    
    collection = get_wrapper(collection, *query_args, **options)
    if not item_count:
        item_count = len(collection)
    paginator = Paginator(item_count, per_page, page)
    if page < 0 or page >= len(paginator):
        subset = []
    else:
        subset = collection[paginator.current.first_item:paginator.current.last_item]
    
    return paginator, subset