o
    ˷eD                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ d
ZG dd deZdefddZdefddZdS )zHClasses for representing collections for the Google Cloud Firestore API.    N)retry)_helpers)DocumentReference)
AnyAsyncGenerator	Coroutine	GeneratorAsyncIteratorIteratorIterableNoReturnTupleUnion)DocumentSnapshot)	BaseQuery)Transaction>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789c                   @   s  e Zd ZdZdEddZdd Zedd	 Zed
d Zde	fddZ
dFdedefddZdeeef fddZ			dGdededejdedeeef f
ddZ			dGdededejdedeeeef eeeeeef f f f
ddZ			dGdedejdedeeef fddZ			dGdedejdedeeeeef eeef f fddZdHd d!Zd"ee de	fd#d$Z d%ed&ede	fd'd(Z!d%ede	fd)d*Z"d+ede	fd,d-Z#d+efd.d/Z$d0ede	fd1d2Z%d3ee&ee'e(f de	fd4d5Z)d3ee&ee'e(f de	fd6d7Z*d3ee&ee'e(f de	fd8d9Z+d3ee&ee'e(f de	fd:d;Z,		dIdejdedeeef fd<d=Z-			dGd>e.dejdedeee&eef ee&ef f fd?d@Z/			dGd>e.dejdedee0e& e1e& f fdAdBZ2de3fdCdDZ4dS )JBaseCollectionReferencea  A reference to a collection in a Firestore database.

    The collection may already exist or this class can facilitate creation
    of documents within the collection.

    Args:
        path (Tuple[str, ...]): The components in the collection path.
            This is a series of strings representing each collection and
            sub-collection ID, as well as the document IDs for any documents
            that contain a sub-collection.
        kwargs (dict): The keyword arguments for the constructor. The only
            supported keyword is ``client`` and it must be a
            :class:`~google.cloud.firestore_v1.client.Client` if provided. It
            represents the client that created this collection reference.

    Raises:
        ValueError: if

            * the ``path`` is empty
            * there are an even number of elements
            * a collection ID in ``path`` is not a string
            * a document ID in ``path`` is not a string
        TypeError: If a keyword other than ``client`` is used.
    returnNc                 O   s6   t j|dd || _|dd | _|rtd|dd S )NT)is_collectionclientzReceived unexpected argumentszOnly `client` is supported)r   verify_path_pathpop_client	TypeError)selfpathkwargs r   `/var/www/ideatree/venv/lib/python3.10/site-packages/google/cloud/firestore_v1/base_collection.py__init__E   s   z BaseCollectionReference.__init__c                 C   s(   t || jstS | j|jko| j|jkS N)
isinstance	__class__NotImplementedr   r   )r   otherr   r   r    __eq__N   s   zBaseCollectionReference.__eq__c                 C   s
   | j d S )zfThe collection identifier.

        Returns:
            str: The last component of the path.
        )r   r   r   r   r    idS   s   
zBaseCollectionReference.idc                 C   s,   t | jdkr	dS | jdd }| jj| S )a  Document that owns the current collection.

        Returns:
            Optional[:class:`~google.cloud.firestore_v1.document.DocumentReference`]:
            The parent document, if the current collection is not a
            top-level collection.
           Nr(   )lenr   r   document)r   parent_pathr   r   r    parent\   s   	zBaseCollectionReference.parentc                 C      t r"   NotImplementedErrorr)   r   r   r    _queryk      zBaseCollectionReference._querydocument_idc                 C   s6   |du rt  }| jd r| j|f n|f}| jj| S )a  Create a sub-document underneath the current collection.

        Args:
            document_id (Optional[str]): The document identifier
                within the current collection. If not provided, will default
                to a random 20 character string composed of digits,
                uppercase and lowercase and letters.

        Returns:
            :class:`~google.cloud.firestore_v1.document.DocumentReference`:
            The child document.
        Nr   )_auto_idr   r   r-   )r   r5   
child_pathr   r   r    r-   n   s   z BaseCollectionReference.documentc                 C   sD   | j }|du rtj| jjdf}n|j}tj|| jf}||fS )ar  Get fully-qualified parent path and prefix for this collection.

        Returns:
            Tuple[str, str]: Pair of

            * the fully-qualified (with database and project) path to the
              parent of this collection (will either be the database path
              or a document path).
            * the prefix to a document in this collection.
        N	documents)r/   r   DOCUMENT_PATH_DELIMITERjoinr   _database_string_document_pathr*   )r   
parent_docr.   expected_prefixr   r   r    _parent_info   s   
z$BaseCollectionReference._parent_infodocument_datar   timeoutc                 C   s,   |du rt  }| |}t||}||fS )z+Shared setup for async / sync :method:`add`N)r6   r-   r   make_retry_timeout_kwargs)r   r@   r5   r   rA   document_refr   r   r   r    	_prep_add   s
   
z!BaseCollectionReference._prep_addc                 C   r0   r"   r1   )r   r@   r5   r   rA   r   r   r    add   s   zBaseCollectionReference.add	page_sizec                 C   s6   |   \}}|| j|dddid}t||}||fS )z6Shared setup for async / sync :method:`list_documents`Tfield_pathsN)r/   collection_idrF   show_missingmask)r?   r*   r   rB   )r   rF   r   rA   r/   _requestr   r   r   r    _prep_list_documents   s   
z,BaseCollectionReference._prep_list_documentsc                 C   r0   r"   r1   )r   rF   r   rA   r   r   r    list_documents      z&BaseCollectionReference.list_documentsr   c                 C   s   |    S r"   )r3   	recursiver)   r   r   r    rP      s   z!BaseCollectionReference.recursiverG   c                 C      |   }||S )a  Create a "select" query with this collection as parent.

        See
        :meth:`~google.cloud.firestore_v1.query.Query.select` for
        more information on this method.

        Args:
            field_paths (Iterable[str, ...]): An iterable of field paths
                (``.``-delimited list of field names) to use as a projection
                of document fields in the query results.

        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`:
            A "projected" query.
        )r3   select)r   rG   queryr   r   r    rR      s   
zBaseCollectionReference.select
field_path	op_stringc                 C   sV   |dkr |dkr g }|D ]}t |tr| |}|| q|}|  }||||S )az  Create a "where" query with this collection as parent.

        See
        :meth:`~google.cloud.firestore_v1.query.Query.where` for
        more information on this method.

        Args:
            field_path (str): A field path (``.``-delimited list of
                field names) for the field to filter on.
            op_string (str): A comparison operation in the form of a string.
                Acceptable values are ``<``, ``<=``, ``==``, ``>=``, ``>``,
                and ``in``.
            value (Any): The value to compare the field against in the filter.
                If ``value`` is :data:`None` or a NaN, then ``==`` is the only
                allowed operation.  If ``op_string`` is ``in``, ``value``
                must be a sequence of values.

        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`:
            A filtered query.
        __name__in)r#   strr-   appendr3   where)r   rT   rU   valuewrapped_namesnamerS   r   r   r    rZ      s   

zBaseCollectionReference.wherec                 K   s   |   }|j|fi |S )a  Create an "order by" query with this collection as parent.

        See
        :meth:`~google.cloud.firestore_v1.query.Query.order_by` for
        more information on this method.

        Args:
            field_path (str): A field path (``.``-delimited list of
                field names) on which to order the query results.
            kwargs (Dict[str, Any]): The keyword arguments to pass along
                to the query. The only supported keyword is ``direction``,
                see :meth:`~google.cloud.firestore_v1.query.Query.order_by`
                for more information.

        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`:
            An "order by" query.
        )r3   order_by)r   rT   r   rS   r   r   r    r^     s   z BaseCollectionReference.order_bycountc                 C   rQ   )a-  Create a limited query with this collection as parent.

        .. note::
           `limit` and `limit_to_last` are mutually exclusive.
           Setting `limit` will drop previously set `limit_to_last`.

        See
        :meth:`~google.cloud.firestore_v1.query.Query.limit` for
        more information on this method.

        Args:
            count (int): Maximum number of documents to return that match
                the query.

        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`:
            A limited query.
        )r3   limitr   r_   rS   r   r   r    r`   $  s   
zBaseCollectionReference.limitc                 C   rQ   )aD  Create a limited to last query with this collection as parent.

        .. note::
           `limit` and `limit_to_last` are mutually exclusive.
           Setting `limit_to_last` will drop previously set `limit`.

        See
        :meth:`~google.cloud.firestore_v1.query.Query.limit_to_last`
        for more information on this method.

        Args:
            count (int): Maximum number of documents to return that
                match the query.
        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`:
            A limited to last query.
        )r3   limit_to_lastra   r   r   r    rb   :  s   
z%BaseCollectionReference.limit_to_lastnum_to_skipc                 C   rQ   )a  Skip to an offset in a query with this collection as parent.

        See
        :meth:`~google.cloud.firestore_v1.query.Query.offset` for
        more information on this method.

        Args:
            num_to_skip (int): The number of results to skip at the beginning
                of query results. (Must be non-negative.)

        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`:
            An offset query.
        )r3   offset)r   rc   rS   r   r   r    rd   O  s   
zBaseCollectionReference.offsetdocument_fieldsc                 C   rQ   )a  Start query at a cursor with this collection as parent.

        See
        :meth:`~google.cloud.firestore_v1.query.Query.start_at` for
        more information on this method.

        Args:
            document_fields (Union[:class:`~google.cloud.firestore_v1.                document.DocumentSnapshot`, dict, list, tuple]):
                A document snapshot or a dictionary/list/tuple of fields
                representing a query results cursor. A cursor is a collection
                of values that represent a position in a query result set.

        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`:
            A query with cursor.
        )r3   start_atr   re   rS   r   r   r    rf   a     
z BaseCollectionReference.start_atc                 C   rQ   )a  Start query after a cursor with this collection as parent.

        See
        :meth:`~google.cloud.firestore_v1.query.Query.start_after` for
        more information on this method.

        Args:
            document_fields (Union[:class:`~google.cloud.firestore_v1.                document.DocumentSnapshot`, dict, list, tuple]):
                A document snapshot or a dictionary/list/tuple of fields
                representing a query results cursor. A cursor is a collection
                of values that represent a position in a query result set.

        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`:
            A query with cursor.
        )r3   start_afterrg   r   r   r    ri   x  rh   z#BaseCollectionReference.start_afterc                 C   rQ   )a  End query before a cursor with this collection as parent.

        See
        :meth:`~google.cloud.firestore_v1.query.Query.end_before` for
        more information on this method.

        Args:
            document_fields (Union[:class:`~google.cloud.firestore_v1.                document.DocumentSnapshot`, dict, list, tuple]):
                A document snapshot or a dictionary/list/tuple of fields
                representing a query results cursor. A cursor is a collection
                of values that represent a position in a query result set.

        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`:
            A query with cursor.
        )r3   
end_beforerg   r   r   r    rj     rh   z"BaseCollectionReference.end_beforec                 C   rQ   )a  End query at a cursor with this collection as parent.

        See
        :meth:`~google.cloud.firestore_v1.query.Query.end_at` for
        more information on this method.

        Args:
            document_fields (Union[:class:`~google.cloud.firestore_v1.                document.DocumentSnapshot`, dict, list, tuple]):
                A document snapshot or a dictionary/list/tuple of fields
                representing a query results cursor. A cursor is a collection
                of values that represent a position in a query result set.

        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`:
            A query with cursor.
        )r3   end_atrg   r   r   r    rk     rh   zBaseCollectionReference.end_atc                 C   s   |   }t||}||fS )z:Shared setup for async / sync :meth:`get` / :meth:`stream`)r3   r   rB   )r   r   rA   rS   r   r   r   r    _prep_get_or_stream  s   z+BaseCollectionReference._prep_get_or_streamtransactionc                 C   r0   r"   r1   r   rm   r   rA   r   r   r    get  rO   zBaseCollectionReference.getc                 C   r0   r"   r1   rn   r   r   r    stream  s   zBaseCollectionReference.streamc                 C   r0   r"   r1   )r   callbackr   r   r    on_snapshot  r4   z#BaseCollectionReference.on_snapshot)r   Nr"   )NNN)r   r   )NN)5rV   
__module____qualname____doc__r!   r'   propertyr*   r/   r   r3   rX   r   r-   r   r   r?   dictretriesRetryfloatrD   r   r   rE   intrM   r   r   rN   rP   r   rR   rZ   r^   r`   rb   rd   r   listtuplerf   ri   rj   rk   rl   r   ro   r
   r	   rp   r   rr   r   r   r   r    r   +   s    
	



$





%







r   r   c                   C   s   d dd tdD S )zGenerate a "random" automatically generated ID.

    Returns:
        str: A 20 character string composed of digits, uppercase and
        lowercase and letters.
     c                 s   s    | ]}t tV  qd S r"   )randomchoice_AUTO_ID_CHARS).0rK   r   r   r    	<genexpr>  s    z_auto_id.<locals>.<genexpr>   )r:   ranger   r   r   r    r6     s   r6   c                 C   s   |j tjd }| |S )zConvert Document resource to document ref.

    Args:
        collection_reference (google.api_core.page_iterator.GRPCIterator):
            iterator response
        item (dict): document resource
    r(   )r]   splitr   r9   r-   )collection_referenceitemr5   r   r   r    _item_to_document_ref  s   
r   ) ru   r   google.api_corer   rx   google.cloud.firestore_v1r   "google.cloud.firestore_v1.documentr   typingr   r   r   r   r	   r
   r   r   r   r   'google.cloud.firestore_v1.base_documentr   $google.cloud.firestore_v1.base_queryr   %google.cloud.firestore_v1.transactionr   r   objectr   rX   r6   r   r   r   r   r    <module>   s    0   6