def pages_to_show(paginator, page, page_labels=None):
    """Generate a dictionary of pages to show around the current page. Show
    3 numbers on either side of the specified page, or more if close to end or
    beginning of available pages.

    :param paginator: django :class:`~django.core.paginator.Paginator`,
        populated with objects
    :param page: number of the current page
    :param page_labels: optional dictionary of page labels, keyed on page number
    :rtype: dictionary; keys are page numbers, values are page labels
    """
    show_pages = {}  # FIXME; do we need OrderedDict here ?
    if page_labels is None:
        page_labels = {}

    def get_page_label(index):
        if index in page_labels:
            return page_labels[index]
        else:
            return unicode(index)

    if page != 1:
        before = 3      # default number of pages to show before the current page
        if page >= (paginator.num_pages - 3):   # current page is within 3 of end
            # increase number to show before current page based on distance to end
            before += (3 - (paginator.num_pages - page))
        for i in range(before, 0, -1):    # add pages from before away up to current page
            if (page - i) >= 1:
                # if there is a page label available, use that as dictionary value
                show_pages[page - i] = get_page_label(page - i)

    # show up to 3 to 7 numbers after the current number, depending on
    # how many we already have
    for i in range(7 - len(show_pages)):
        if (page + i) <= paginator.num_pages:
            # if there is a page label available, use that as dictionary value
            show_pages[page + i] = get_page_label(page + i)

    return show_pages
Generate a dictionary of pages to show around the current page. Show
    3 numbers on either side of the specified page, or more if close to end or
    beginning of available pages.

    :param paginator: django :class:`~django.core.paginator.Paginator`,
        populated with objects
    :param page: number of the current page
    :param page_labels: optional dictionary of page labels, keyed on page number
    :rtype: dictionary; keys are page numbers, values are page labels