django-endless-pagination-de / endless_pagination / utils.py

from endless_pagination.settings import (PAGE_LABEL, 
    DEFAULT_CALLABLE_EXTREMES, DEFAULT_CALLABLE_AROUNDS)
from endless_pagination import exceptions

def get_page_number_from_request(request, page_label=PAGE_LABEL):
    """
    Get page number from *GET* or *POST* data.
    If the page dows not exists in *request*, or is not a number
    then 1 is returned.
    """
    try:
        return int(request.REQUEST[PAGE_LABEL])
    except (KeyError, TypeError, ValueError):
        return 1
        
def get_page_from_context(context):
    """
    Get the django paginator page object from a *context* (a dict like object).
    If the context key "endless_page" is not found, a PaginationError
    is raised.
    """
    try:
        return context["endless_page"]
    except KeyError:
        raise exceptions.PaginationError("Cannot find endless page in context.")

def get_querystring_for_page(request, page_number, prefix="?"):
    """
    Return a querystring pointing to *page_number*.
    The querystring is prefixed by *prefix* (e.g.: "?page=2").
    """
    querydict = request.GET.copy()
    querydict[PAGE_LABEL] = page_number
    # for page number 1 there is no need for querystring
    if page_number == 1:
        del querydict[PAGE_LABEL]
    if querydict:
        return "%s%s" % (prefix, querydict.urlencode())
    return ""
    
def get_page_numbers(current_page, num_pages, 
    extremes=DEFAULT_CALLABLE_EXTREMES, arounds=DEFAULT_CALLABLE_AROUNDS):
    """
    Default callable for page listing.
    Produces a digg-style pagination.
    """
    page_range = range(1, num_pages+1)
    pages = ["previous"]
    
    # get first and last pages (extremes)
    first = page_range[:extremes]
    pages.extend(first)
    last = page_range[-extremes:]
    
    # get current pages (arounds)
    current_start = current_page - 1 - arounds
    if current_start < 0:
        current_start = 0
    current_end = current_page + arounds
    if current_end > num_pages:
        current_end = num_pages
    current = page_range[current_start:current_end]
    
    # mix first with current pages
    diff = current[0] - first[-1]
    to_add = current
    if diff > 1:
        pages.append(None)
    elif diff < 1:
        to_add = current[abs(diff)+1:]
    pages.extend(to_add)
    
    # mix current with last pages
    diff = last[0] - current[-1]
    to_add = last
    if diff > 1:
        pages.append(None)
    elif diff < 1:
        to_add = last[abs(diff)+1:]
    pages.extend(to_add)
    
    pages.append("next")
    return pages
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.