o
    go+                     @  s~   d dl mZ d dlZd dlmZ d dlZd dlm	Z
 d dlmZ d dlmZ G dd dZG dd	 d	eZG d
d deZdS )    )annotationsN)ceil)abort)requestc                   @  s  e Zd ZdZ					d?d@ddZeddddddAddZedBddZdCddZ	dBddZ
edBddZedBd d!ZedBd"d#ZedDd$d%ZedEd&d'Zd(d)dFd*d+ZedDd,d-ZedEd.d/Zd(d)dFd0d1Zd2d2d3d2d4dGd:d;ZdHd=d>ZdS )I
PaginationaU  Apply an offset and limit to the query based on the current page and number of
    items per page.

    Don't create pagination objects manually. They are created by
    :meth:`.SQLAlchemy.paginate` and :meth:`.Query.paginate`.

    This is a base class, a subclass must implement :meth:`_query_items` and
    :meth:`_query_count`. Those methods will use arguments passed as ``kwargs`` to
    perform the queries.

    :param page: The current page, used to calculate the offset. Defaults to the
        ``page`` query arg during a request, or 1 otherwise.
    :param per_page: The maximum number of items on a page, used to calculate the
        offset and limit. Defaults to the ``per_page`` query arg during a request,
        or 20 otherwise.
    :param max_per_page: The maximum allowed value for ``per_page``, to limit a
        user-provided value. Use ``None`` for no limit. Defaults to 100.
    :param error_out: Abort with a ``404 Not Found`` error if no items are returned
        and ``page`` is not 1, or if ``page`` or ``per_page`` is less than 1, or if
        either are not ints.
    :param count: Calculate the total number of values by issuing an extra count
        query. For very complex queries this may be inaccurate or slow, so it can be
        disabled and set manually if necessary.
    :param kwargs: Information about the query to paginate. Different subclasses will
        require different arguments.

    .. versionchanged:: 3.0
        Iterating over a pagination object iterates over its items.

    .. versionchanged:: 3.0
        Creating instances manually is not a public API.
    Nd   Tpage
int | Noneper_pagemax_per_page	error_outboolcountkwargst.AnyreturnNonec           	      K  sx   || _ | j||||d\}}|| _	 || _	 || _	 |  }|s*|dkr*|r*td || _	 |r5|  }nd }|| _	d S )Nr   r
   r   r        )
_query_args_prepare_page_argsr   r
   r   _query_itemsr   items_query_counttotal)	selfr   r
   r   r   r   r   r   r    r   ]/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/flask_sqlalchemy/pagination.py__init__.   s.   	

zPagination.__init__r   tuple[int, int]c              	   C  s   t rI| d u r%ztt jdd} W n ttfy$   |r td d} Y nw |d u rHztt jdd}W n  ttfyG   |rCtd d}Y nw n| d u rOd} |d u rUd}|d ur^t||}| dk rk|ritd nd} |dk r{|rytd | |fS d}| |fS )Nr   r   r   r
      )r   intargsget	TypeError
ValueErrorr   minr   r   r   r   r   Z   sD   	

zPagination._prepare_page_argsr"   c                 C  s   | j d | j S )z}The index of the first item to query, passed to ``offset()``.

        :meta private:

        .. versionadded:: 3.0
        r   )r   r
   r   r   r   r   _query_offset   s   zPagination._query_offsetlist[t.Any]c                 C     t )zExecute the query to get the items on the current page.

        Uses init arguments stored in :attr:`_query_args`.

        :meta private:

        .. versionadded:: 3.0
        NotImplementedErrorr(   r   r   r   r         	zPagination._query_itemsc                 C  r+   )zExecute the query to get the total number of items.

        Uses init arguments stored in :attr:`_query_args`.

        :meta private:

        .. versionadded:: 3.0
        r,   r(   r   r   r   r      r.   zPagination._query_countc                 C  s&   t | jdkr	dS | jd | j d S )zThe number of the first item on the page, starting from 1, or 0 if there are
        no items.

        .. versionadded:: 3.0
        r   r   )lenr   r   r
   r(   r   r   r   first   s   zPagination.firstc                 C  s   | j }t||t| j d S )zThe number of the last item on the page, starting from 1, inclusive, or 0 if
        there are no items.

        .. versionadded:: 3.0
        r   )r0   maxr/   r   )r   r0   r   r   r   last   s   zPagination.lastc                 C  s(   | j dks
| j du rdS t| j | j S )zThe total number of pages.r   N)r   r   r
   r(   r   r   r   pages   s   zPagination.pagesc                 C  s
   | j dkS )z'``True`` if this is not the first page.r   )r   r(   r   r   r   has_prev   s   
zPagination.has_prevc                 C  s   | j sdS | jd S )z@The previous page number, or ``None`` if this is the first page.Nr   )r4   r   r(   r   r   r   prev_num      
zPagination.prev_numF)r   c                C  s2   t | d| jd | j|dd| j}| j|_|S )a  Query the :class:`Pagination` object for the previous page.

        :param error_out: Abort with a ``404 Not Found`` error if no items are returned
            and ``page`` is not 1, or if ``page`` or ``per_page`` is less than 1, or if
            either are not ints.
        r   F)r   r
   r   r   Nr   )typer   r
   r   r   r   r   pr   r   r   prev   s   zPagination.prevc                 C  s   | j | jk S )z&``True`` if this is not the last page.)r   r3   r(   r   r   r   has_next   s   zPagination.has_nextc                 C  s   | j sdS | jd S )z;The next page number, or ``None`` if this is the last page.Nr   )r;   r   r(   r   r   r   next_num   r6   zPagination.next_numc                C  s6   t | d| jd | j| j|dd| j}| j|_|S )a  Query the :class:`Pagination` object for the next page.

        :param error_out: Abort with a ``404 Not Found`` error if no items are returned
            and ``page`` is not 1, or if ``page`` or ``per_page`` is less than 1, or if
            either are not ints.
        r   F)r   r
   r   r   r   Nr   )r7   r   r
   r   r   r   r8   r   r   r   next   s   zPagination.next      )	left_edgeleft_currentright_current
right_edger@   rA   rB   rC   t.Iterator[int | None]c          
      c  s    | j d }|dkrdS td| |}td|E dH  ||kr!dS t|| j| }t| j| d |}|| dkr<dV  t||E dH  ||krJdS t||| }	|	| dkrZdV  t|	|E dH  dS )a  Yield page numbers for a pagination widget. Skipped pages between the edges
        and middle are represented by a ``None``.

        For example, if there are 20 pages and the current page is 7, the following
        values are yielded.

        .. code-block:: python

            1, 2, None, 5, 6, 7, 8, 9, 10, 11, None, 19, 20

        :param left_edge: How many pages to show from the first page.
        :param left_current: How many pages to show left of the current page.
        :param right_current: How many pages to show right of the current page.
        :param right_edge: How many pages to show from the last page.

        .. versionchanged:: 3.0
            Improved efficiency of calculating what to yield.

        .. versionchanged:: 3.0
            ``right_current`` boundary is inclusive.

        .. versionchanged:: 3.0
            All parameters are keyword-only.
        r   Nr   )r3   r'   ranger1   r   )
r   r@   rA   rB   rC   	pages_endleft_end	mid_startmid_endright_startr   r   r   
iter_pages  s&   
 zPagination.iter_pagest.Iterator[t.Any]c                 c  s    | j E d H  d S )N)r   r(   r   r   r   __iter__D  s   zPagination.__iter__)NNr   TT)r   r	   r
   r	   r   r	   r   r   r   r   r   r   r   r   )
r   r	   r
   r	   r   r	   r   r   r   r    r   r"   r   r*   )r   r   )r   r	   )r   r   r   r   )
r@   r"   rA   r"   rB   r"   rC   r"   r   rD   )r   rL   )__name__
__module____qualname____doc__r   staticmethodr   propertyr)   r   r   r0   r2   r3   r4   r5   r:   r;   r<   r=   rK   rM   r   r   r   r   r      sP    #,1
	
	=r   c                   @  $   e Zd ZdZd
ddZdddZd	S )SelectPaginationzReturned by :meth:`.SQLAlchemy.paginate`. Takes ``select`` and ``session``
    arguments in addition to the :class:`Pagination` arguments.

    .. versionadded:: 3.0
    r   r*   c                 C  s>   | j d }|| j| j}| j d }t||  S )Nselectsession)	r   limitr
   offsetr)   listexecuteuniquescalars)r   rX   rY   r   r   r   r   O  s   

zSelectPagination._query_itemsr"   c                 C  sR   | j d }|tdd  }| j d }|ttj	
 | }|S )NrX   *rY   )r   optionssa_ormlazyloadorder_bysubqueryr]   sarX   funcr   select_fromscalar)r   rX   subrY   outr   r   r   r   U  s
   

 zSelectPagination._query_countNrO   rN   rP   rQ   rR   rS   r   r   r   r   r   r   rW   H  s    
rW   c                   @  rV   )QueryPaginationzReturned by :meth:`.Query.paginate`. Takes a ``query`` argument in addition to
    the :class:`Pagination` arguments.

    .. versionadded:: 3.0
    r   r*   c                 C  s&   | j d }|| j| j }|S Nquery)r   rZ   r
   r[   r)   all)r   ro   rk   r   r   r   r   d  s   
zQueryPagination._query_itemsr"   c                 C  s   | j d d  }|S rn   )r   rd   r   )r   rk   r   r   r   r   i  s   zQueryPagination._query_countNrO   rN   rl   r   r   r   r   rm   ]  s    
rm   )
__future__r   typingtmathr   
sqlalchemyrf   sqlalchemy.ormormrb   flaskr   r   r   rW   rm   r   r   r   r   <module>   s      >