o
    êË·eÀ  ã                   @   s8   d dl mZmZmZmZ d dlmZ G dd„ deƒZdS )é    )Ú	EmptyPageÚPageÚPageNotAnIntegerÚ	Paginator)Úgettextc                       sn   e Zd ZdZdZd‡ fdd„	Zdd„ Zdd	„ Zd
d„ Zdd„ Z	e
e	ƒZdd„ Ze
eƒZdd„ Ze
eƒZ‡  ZS )ÚLazyPaginatoras  
    Implement lazy pagination, preventing any count() queries.

    By default, for any valid page, the total number of pages for the paginator will be

     - `current + 1` if the number of records fetched for the current page offset is
       bigger than the number of records per page.
     - `current` if the number of records fetched is less than the number of records per page.

    The number of additional records fetched can be adjusted using `look_ahead`, which
    defaults to 1 page. If you like to provide a little more extra information on how much
    pages follow the current page, you can use a higher value.

    .. note::

        The number of records fetched for each page is `per_page * look_ahead + 1`, so increasing
        the value for `look_ahead` makes the view a bit more expensive.

    So::

        paginator = LazyPaginator(range(10000), 10)

        >>> paginator.page(1).object_list
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        >>> paginator.num_pages
        2
        >>> paginator.page(10).object_list
        [91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
        >>> paginator.num_pages
        11
        >>> paginator.page(1000).object_list
        [9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999]
        >>> paginator.num_pages
        1000

    Usage with `~.SingleTableView`::

        class UserListView(SingleTableView):
            table_class = UserTable
            table_data = User.objects.all()
            pagination_class = LazyPaginator

    Or with `~.RequestConfig`::

        RequestConfig(paginate={"paginator_class": LazyPaginator}).configure(table)

    .. versionadded :: 2.0.0
    é   Nc                    s4   d | _ d | _|d ur|| _tƒ j||fi |¤Ž d S ©N)Ú
_num_pagesÚ_final_num_pagesÚ
look_aheadÚsuperÚ__init__)ÚselfÚobject_listÚper_pager   Úkwargs©Ú	__class__© úP/var/www/ideatree/venv/lib/python3.10/site-packages/django_tables2/paginators.pyr   9   s
   zLazyPaginator.__init__c              	   C   sZ   zt |tƒr| ¡ st‚t|ƒ}W n ttfy    ttdƒƒ‚w |dk r+ttdƒƒ‚|S )z'Validate the given 1-based page number.z"That page number is not an integerr   zThat page number is less than 1)	Ú
isinstanceÚfloatÚ
is_integerÚ
ValueErrorÚintÚ	TypeErrorr   Ú_r   ©r   Únumberr   r   r   Úvalidate_numberA   s   ÿzLazyPaginator.validate_numberc                 C   sÄ   |   |pd¡}|d | j }|| j }| jd | j d }t| j||| j | … ƒ}t|ƒ}|| j| j krG||| j  | _|d | j… }n|dkrV|| jkrVtt	dƒƒ‚|| _|| _
t||| ƒS )Nr   zThat page contains no results)r    r   r   Úlistr   ÚorphansÚlenr
   r   r   r   r   )r   r   ÚbottomÚtopÚlook_ahead_itemsÚobjectsÚobjects_countr   r   r   ÚpageM   s   
zLazyPaginator.pagec                 C   s
   || j kS r	   )r   r   r   r   r   Úis_last_pagee   s   
zLazyPaginator.is_last_pagec                 C   ó   t ‚r	   ©ÚNotImplementedError©r   r   r   r   Ú
_get_counth   ó   zLazyPaginator._get_countc                 C   s   | j S r	   )r
   r.   r   r   r   Ú_get_num_pagesm   s   zLazyPaginator._get_num_pagesc                 C   r+   r	   r,   r.   r   r   r   Ú_get_page_ranger   r0   zLazyPaginator._get_page_ranger	   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r)   r*   r/   ÚpropertyÚcountr1   Ú	num_pagesr2   Ú
page_rangeÚ__classcell__r   r   r   r   r      s    1r   N)	Údjango.core.paginatorr   r   r   r   Údjango.utils.translationr   r   r   r   r   r   r   Ú<module>   s    