o
    D©Ìh0  ã                   @   s\   d dl mZ d dlmZmZmZmZmZmZ dZ	dZ
dZG dd„ deƒZG dd	„ d	eƒZd
S )é    )ÚOperationError)ÚCASCADEÚDENYÚ
DO_NOTHINGÚNULLIFYÚPULLÚBaseQuerySet)ÚQuerySetÚQuerySetNoCacher   r   r   r   r   é   éd   c                       s^   e Zd ZdZdZdZdZdd„ Zdd„ Zdd	„ Z	d
d„ Z
dd„ Zd‡ fdd„	Zdd„ Z‡  ZS )r	   zÈThe default queryset, that builds queries and handles a set of results
    returned from a query.

    Wraps a MongoDB cursor, providing :class:`~mongoengine.Document` objects as
    the results.
    TNc                 C   s   d| _ | jr
|  ¡ S t| jƒS )zñIteration utilises a results cache which iterates the cursor
        in batches of ``ITER_CHUNK_SIZE``.

        If ``self._has_more`` the cursor hasn't been exhausted so cache then
        batch. Otherwise iterate the result_cache.
        T)Ú_iterÚ	_has_moreÚ_iter_resultsÚiterÚ_result_cache©Úself© r   úl/var/www/html/Moveonwheels/move_on_wheels/venv/lib/python3.10/site-packages/mongoengine/queryset/queryset.pyÚ__iter__&   s   
zQuerySet.__iter__c                 C   s4   | j dur| j S | jrt|  ¡ ƒ t| jƒ| _ | j S )z�Since __len__ is called quite frequently (for example, as part of
        list(qs)), we populate the result cache and cache the length.
        N)Ú_lenr   Úlistr   Úlenr   r   r   r   r   Ú__len__5   s   
zQuerySet.__len__c                 C   s@   | j rdS |  ¡  | jdtd … }t|ƒtkrd|d< t|ƒS )z/Provide a string representation of the QuerySetú.. queryset mid-iteration ..Né   ú$...(remaining elements truncated)...éÿÿÿÿ)r   Ú_populate_cacher   ÚREPR_OUTPUT_SIZEr   Úrepr)r   Údatar   r   r   Ú__repr__D   s   zQuerySet.__repr__c                 c   sj   � | j du r	g | _ d}	 |t| j ƒk r$| j | V  |d7 }|t| j ƒk s| js)dS t| j ƒ|kr4|  ¡  q)zÅA generator for iterating over the result cache.

        Also populates the cache if there are more possible results to
        yield. Raises StopIteration when there are no more results.
        Nr   Tr   )r   r   r   r   )r   Úposr   r   r   r   O   s   €
	þìzQuerySet._iter_resultsc                 C   sZ   | j du rg | _ | jsdS zttƒD ]
}| j  t| ƒ¡ qW dS  ty,   d| _Y dS w )z{
        Populates the result cache with ``ITER_CHUNK_SIZE`` more entries
        (until the cursor is exhausted).
        NF)r   r   ÚrangeÚITER_CHUNK_SIZEÚappendÚnextÚStopIteration)r   Ú_r   r   r   r   o   s   
ÿüzQuerySet._populate_cacheFc                    s2   |du r
t ƒ  |¡S | jdu rt ƒ  |¡| _| jS )zêCount the selected elements in the query.

        :param with_limit_and_skip (optional): take any :meth:`limit` or
            :meth:`skip` that has been applied to this cursor into account when
            getting the count
        FN)ÚsuperÚcountr   )r   Úwith_limit_and_skip©Ú	__class__r   r   r,   ‡   s
   
zQuerySet.countc                 C   s&   | j dur	tdƒ‚|  t| j| jƒ¡S )z!Convert to a non-caching querysetNzQuerySet already cached)r   r   Ú_clone_intor
   Ú	_documentÚ_collectionr   r   r   r   Úno_cache—   s   
zQuerySet.no_cache)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r#   r   r   r,   r3   Ú__classcell__r   r   r.   r   r	      s     r	   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r
   zA non caching QuerySetc                 C   s   |   t| j| jƒ¡S )zConvert to a caching queryset)r0   r	   r1   r2   r   r   r   r   Úcache¢   s   zQuerySetNoCache.cachec              	   C   sj   | j rdS g }ttd ƒD ]}z	| t| ƒ¡ W q ty"   Y  nw t|ƒtkr-d|d< |  ¡  t|ƒS )z2Provides the string representation of the QuerySetr   r   r   r   )	r   r%   r    r'   r(   r)   r   Úrewindr!   )r   r"   r*   r   r   r   r#   ¦   s   ÿzQuerySetNoCache.__repr__c                 C   s   | }|j r	|  ¡ }| ¡  |S )N)r   Úcloner:   )r   Úquerysetr   r   r   r   ¸   s
   zQuerySetNoCache.__iter__N)r4   r5   r6   r7   r9   r#   r   r   r   r   r   r
   Ÿ   s
    r
   N)Úmongoengine.errorsr   Úmongoengine.queryset.baser   r   r   r   r   r   Ú__all__r    r&   r	   r
   r   r   r   r   Ú<module>   s     	 