o
    2g&F                     @  s   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ d	d
lmZ ddlmZ ddlmZ ddlmZ erLddlmZ ddlZG dd deZG dd deZG dd deZG dd deZee_dS )z"Provide Collections functionality.    )annotations)TYPE_CHECKINGAny   )API_PATH)ClientException)_deprecate_args)cachedproperty   )PRAWBase   )
RedditBase)
Submission)	Subreddit)IteratorNc                      sp   e Zd ZdZd" fddZd#ddZd$ddZdd Zd$ddZd%ddZ	d&ddZ
d'ddZd(d d!Z  ZS ))CollectionModerationzClass to support moderation actions on a :class:`.Collection`.

    Obtain an instance via:

    .. code-block:: python

        reddit.subreddit("test").collections("some_uuid").mod

    redditpraw.Redditcollection_idstrc                   s   t  j|dd || _dS )zInitialize a :class:`.CollectionModeration` instance.

        :param collection_id: The ID of a :class:`.Collection`.

        N_data)super__init__r   )selfr   r   	__class__ V/home/garg/my-data/venv/lib/python3.10/site-packages/praw/models/reddit/collections.pyr   !   s   
zCollectionModeration.__init__poststr | praw.models.Submissionreturnc                 C  s   t |tr|jS t |tsdt| d}t||| jjj	d  dr(|S z	| jj
|djW S  tyA   | j
|j Y S w )zGet a post's fullname.

        :param post: A fullname, a :class:`.Submission`, a permalink, or an ID.

        :returns: The fullname of the post.

        z(Cannot get fullname from object of type .
submission_)url)
isinstancer   fullnamer   type	TypeError
startswith_redditconfigkindsr#   r   )r   r   msgr   r   r   _post_fullname*   s   

z#CollectionModeration._post_fullnamer#   praw.models.Submissionc                 C  *   |  |}| jjtd | j|dd dS )a  Add a post to the collection.

        :param submission: The post to add, a :class:`.Submission`, its permalink as a
            ``str``, its fullname as a ``str``, or its ID as a ``str``.

        Example usage:

        .. code-block:: python

            collection = reddit.subreddit("test").collections("some_uuid")
            collection.mod.add_post("bgibu9")

        .. seealso::

            :meth:`.remove_post`

        collection_add_postr   link_fullnamedataNr/   r+   r   r   r   r   r#   r4   r   r   r   add_post>   
   


zCollectionModeration.add_postc                 C  s   | j jtd d| jid dS )zDelete this collection.

        Example usage:

        .. code-block:: python

            reddit.subreddit("test").collections("some_uuid").mod.delete()

        .. seealso::

            :meth:`~.SubredditCollectionsModeration.create`

        collection_deleter   r5   Nr+   r   r   r   r   r   r   r   deleteW   s   
zCollectionModeration.deletec                 C  r1   )a  Remove a post from the collection.

        :param submission: The post to remove, a :class:`.Submission`, its permalink as
            a ``str``, its fullname as a ``str``, or its ID as a ``str``.

        Example usage:

        .. code-block:: python

            collection = reddit.subreddit("test").collections("some_uuid")
            collection.mod.remove_post("bgibu9")

        .. seealso::

            :meth:`.add_post`

        collection_remove_postr3   r5   Nr7   r8   r   r   r   remove_posti   r:   z CollectionModeration.remove_postlinks"list[str | praw.models.Submission]c                   s8   d  fdd|D } jjtd  j|dd dS )a  Reorder posts in the collection.

        :param links: A list of :class:`.Submission`\ s or a ``str`` that is either a
            fullname or an ID.

        Example usage:

        .. code-block:: python

            collection = reddit.subreddit("test").collections("some_uuid")
            current_order = collection.link_ids
            new_order = reversed(current_order)
            collection.mod.reorder(new_order)

        ,c                 3  s    | ]}  |V  qd S N)r/   ).0r   r=   r   r   	<genexpr>   s    z/CollectionModeration.reorder.<locals>.<genexpr>collection_reorder)r   link_idsr5   N)joinr+   r   r   r   )r   rA   rH   r   r=   r   reorder   s
   

zCollectionModeration.reorderdescriptionc                 C      | j jtd | j|dd dS )ah  Update the collection's description.

        :param description: The new description.

        Example usage:

        .. code-block:: python

            collection = reddit.subreddit("test").collections("some_uuid")
            collection.mod.update_description("Please enjoy these links!")

        .. seealso::

            :meth:`.update_title`

        collection_desc)r   rK   r5   Nr<   )r   rK   r   r   r   update_description      

z'CollectionModeration.update_descriptiondisplay_layoutc                 C  rL   )aW  Update the collection's display layout.

        :param display_layout: Either ``"TIMELINE"`` for events or discussions or
            ``"GALLERY"`` for images or memes. Passing ``None`` will clear the set
            layout and ``collection.display_layout`` will be ``None``, however, the
            collection will appear on Reddit as if ``display_layout`` is set to
            ``"TIMELINE"``.

        Example usage:

        .. code-block:: python

            collection = reddit.subreddit("test").collections("some_uuid")
            collection.mod.update_display_layout("GALLERY")

        collection_layout)r   rP   r5   Nr<   )r   rP   r   r   r   update_display_layout   s   
z*CollectionModeration.update_display_layouttitlec                 C  rL   )aO  Update the collection's title.

        :param title: The new title.

        Example usage:

        .. code-block:: python

            collection = reddit.subreddit("test").collections("some_uuid")
            collection.mod.update_title("Titley McTitleface")

        .. seealso::

            :meth:`.update_description`

        collection_title)r   rS   r5   Nr<   )r   rS   r   r   r   update_title   rO   z!CollectionModeration.update_title)r   r   r   r   )r   r    r!   r   )r#   r0   )rA   rB   )rK   r   )rP   r   )rS   r   )__name__
__module____qualname____doc__r   r/   r9   r>   r@   rJ   rN   rR   rU   __classcell__r   r   r   r   r      s    

	




r   c                      sB   e Zd ZdZ	dd fd	d
ZeddddddddZ  ZS )SubredditCollectionsModerationzClass to represent moderator actions on a :class:`.Subreddit`'s :class:`.Collection`\ s.

    Obtain an instance via:

    .. code-block:: python

        reddit.subreddit("test").collections.mod

    Nr   r   sub_fullnamer   r   dict[str, Any] | Nonec                      t  || || _dS )z?Initialize a :class:`.SubredditCollectionsModeration` instance.N)r   r   subreddit_fullname)r   r   r\   r   r   r   r   r         
z'SubredditCollectionsModeration.__init__rS   rK   rP   )rP   
str | Noner!   
Collectionc                C  s.   | j ||d}|r||d< | jjtd |dS )a<  Create a new :class:`.Collection`.

        The authenticated account must have appropriate moderator permissions in the
        subreddit this collection belongs to.

        :param description: The description, up to 500 characters.
        :param display_layout: Either ``"TIMELINE"`` for events or discussions or
            ``"GALLERY"`` for images or memes. Passing ``""`` or ``None`` will make the
            collection appear on Reddit as if this is set to ``"TIMELINE"`` (default:
            ``None``).
        :param title: The title of the collection, up to 300 characters.

        :returns: The newly created :class:`.Collection`.

        Example usage:

        .. code-block:: python

            my_sub = reddit.subreddit("test")
            new_collection = my_sub.collections.mod.create(title="Title", description="desc")
            new_collection.mod.add_post("bgibu9")

        To specify the display layout as ``"GALLERY"`` when creating the collection:

        .. code-block:: python

            my_sub = reddit.subreddit("test")
            new_collection = my_sub.collections.mod.create(
                title="Title", description="desc", display_layout="GALLERY"
            )
            new_collection.mod.add_post("bgibu9")

        .. seealso::

            :meth:`~.CollectionModeration.delete`

        )sr_fullnamerS   rK   rP   collection_creater5   )r_   r+   r   r   )r   rK   rP   rS   r6   r   r   r   create   s   *z%SubredditCollectionsModeration.createrD   )r   r   r\   r   r   r]   )rK   r   rP   ra   rS   r   r!   rb   )rV   rW   rX   rY   r   r   re   rZ   r   r   r   r   r[      s    

r[   c                      sL   e Zd ZdZedddZ		ddddZ	dd fddZdd Z  Z	S )SubredditCollectionszClass to represent a :class:`.Subreddit`'s :class:`.Collection`\ s.

    Obtain an instance via:

    .. code-block:: python

        reddit.subreddit("test").collections

    r!   r[   c                 C  s   t | j| jjS )a5  Get an instance of :class:`.SubredditCollectionsModeration`.

        Provides :meth:`~SubredditCollectionsModeration.create`:

        .. code-block:: python

            my_sub = reddit.subreddit("test")
            new_collection = my_sub.collections.mod.create(title="Title", description="desc")

        )r[   r+   	subredditr'   r=   r   r   r   mod4  s   zSubredditCollections.modNr   ra   	permalinkrb   c                 C  s,   |du |du krd}t |t| j||dS )al  Return the :class:`.Collection` with the specified ID.

        :param collection_id: The ID of a :class:`.Collection` (default: ``None``).
        :param permalink: The permalink of a collection (default: ``None``).

        :returns: The specified :class:`.Collection`.

        Exactly one of ``collection_id`` or ``permalink`` is required.

        Example usage:

        .. code-block:: python

            subreddit = reddit.subreddit("test")

            uuid = "847e4548-a3b5-4ad7-afb4-edbfc2ed0a6b"
            collection = subreddit.collections(uuid)
            print(collection.title)
            print(collection.description)

            permalink = "https://www.reddit.com/r/SUBREDDIT/collection/" + uuid
            collection = subreddit.collections(permalink=permalink)
            print(collection.title)
            print(collection.description)

        Nz?Exactly one of 'collection_id' or 'permalink' must be provided.)r   ri   )r)   rb   r+   )r   r   ri   r.   r   r   r   __call__B  s   zSubredditCollections.__call__r   r   rg   praw.models.Subredditr   r]   c                   r^   )z5Initialize a :class:`.SubredditCollections` instance.N)r   r   rg   )r   r   rg   r   r   r   r   r   h  r`   zSubredditCollections.__init__c                 c  s,    | j jtd d| jjid}|E dH  dS )zIterate over the :class:`.Subreddit`'s :class:`.Collection`\ s.

        Example usage:

        .. code-block:: python

            for collection in reddit.subreddit("test").collections:
                print(collection.permalink)

        collection_subredditrc   )paramsN)r+   getr   rg   r'   )r   requestr   r   r   __iter__r  s   
zSubredditCollections.__iter__)r!   r[   )NN)r   ra   ri   ra   r!   rb   rD   )r   r   rg   rk   r   r]   )
rV   rW   rX   rY   r	   rh   rj   r   rp   rZ   r   r   r   r   rf   )  s    
*
rf   c                      s   e Zd ZdZdZed'ddZed(dd	Z	
	
	
d)d* fddZd+ddZ	d,ddZ
d- fddZ fdd Zd!d" Zd#d$ Zd%d& Z  ZS ).rb   a  Class to represent a :class:`.Collection`.

    Obtain an instance via:

    .. code-block:: python

        collection = reddit.subreddit("test").collections("some_uuid")

    or

    .. code-block:: python

        collection = reddit.subreddit("test").collections(
            permalink="https://reddit.com/r/SUBREDDIT/collection/some_uuid"
        )

    .. include:: ../../typical_attributes.rst

    =================== =============================================================
    Attribute           Description
    =================== =============================================================
    ``author``          The :class:`.Redditor` who created the collection.
    ``collection_id``   The UUID of the collection.
    ``created_at_utc``  Time the collection was created, represented in `Unix Time`_.
    ``description``     The collection description.
    ``display_layout``  The collection display layout.
    ``last_update_utc`` Time the collection was last updated, represented in `Unix
                        Time`_.
    ``link_ids``        A list of :class:`.Submission` fullnames.
    ``permalink``       The collection's permalink (to view on the web).
    ``sorted_links``    An iterable listing of the posts in this collection.
    ``title``           The title of the collection.
    =================== =============================================================

    .. _unix time: https://en.wikipedia.org/wiki/Unix_time

    r   r!   r   c                 C  s   t | j| jS )a  Get an instance of :class:`.CollectionModeration`.

        Provides access to various methods, including
        :meth:`~.CollectionModeration.add_post`, :meth:`~.CollectionModeration.delete`,
        :meth:`~.CollectionModeration.reorder`, and
        :meth:`~.CollectionModeration.update_title`.

        Example usage:

        .. code-block:: python

            collection = reddit.subreddit("test").collections("some_uuid")
            collection.mod.update_title("My new title!")

        )r   r+   r   r=   r   r   r   rh     s   zCollection.modrk   c                 C  s   t | jj| jgdS )zGet the subreddit that this collection belongs to.

        For example:

        .. code-block:: python

            collection = reddit.subreddit("test").collections("some_uuid")
            subreddit = collection.subreddit

        )	fullnames)nextr+   infosubreddit_idr=   r   r   r   rg     s   zCollection.subredditNr   r   r   dict[str, Any]ra   ri   c                   s\   |||f ddkrd}t||r| |d }|r|| _t || | jdd| _dS )a>  Initialize a :class:`.Collection` instance.

        :param reddit: An instance of :class:`.Reddit`.
        :param _data: Any data associated with the :class:`.Collection`.
        :param collection_id: The ID of the :class:`.Collection`.
        :param permalink: The permalink of the :class:`.Collection`.

        Nr
   zIExactly one of '_data', 'collection_id', or 'permalink' must be provided.   T)r   include_links)countr)   
_url_partsr   r   r   _info_params)r   r   r   r   ri   r.   r   r   r   r     s   zCollection.__init__r   c                 c  s    | j E dH  dS )a;  Provide a way to iterate over the posts in this :class:`.Collection`.

        Example usage:

        .. code-block:: python

            collection = reddit.subreddit("test").collections("some_uuid")
            for submission in collection:
                print(submission.title, submission.permalink)

        N)sorted_linksr=   r   r   r   rp     s   zCollection.__iter__intc                 C  s
   t | jS )zGet the number of posts in this :class:`.Collection`.

        Example usage:

        .. code-block:: python

            collection = reddit.subreddit("test").collections("some_uuid")
            print(len(collection))

        )lenrH   r=   r   r   r   __len__  s   
zCollection.__len__	attributer   valuer   c                   s@   |dkr| j || _n|dkr| j j|}t || dS )z9Objectify author, subreddit, and sorted_links attributes.author_namer{   N)r+   redditorauthor	_objector	objectifyr   __setattr__)r   r   r   r   r   r   r   	  s
   zCollection.__setattr__c                   sp   |   }z	| jj| W n ty    d| jd}t|d w t| | j|d}| j|j t	 
  d S )Nz(Error during fetch. Check collection ID z is correct.r   )_fetch_datar+   r   check_errorr   r   r(   __dict__updater   _fetch)r   r6   r.   otherr   r   r   r     s   
zCollection._fetchc                 C  s   di | j fS )N
collection)rz   r=   r   r   r   _fetch_info   s   zCollection._fetch_infoc                 C      | j jtd | jddd dS )zFollow this :class:`.Collection`.

        Example usage:

        .. code-block:: python

            reddit.subreddit("test").collections("some_uuid").follow()

        .. seealso::

            :meth:`.unfollow`

        collection_followTr   followr5   Nr<   r=   r   r   r   r   #     

zCollection.followc                 C  r   )zUnfollow this :class:`.Collection`.

        Example usage:

        .. code-block:: python

            reddit.subreddit("test").collections("some_uuid").unfollow()

        .. seealso::

            :meth:`.follow`

        r   Fr   r5   Nr<   r=   r   r   r   unfollow6  r   zCollection.unfollow)r!   r   )r!   rk   )NNN)r   r   r   ru   r   ra   ri   ra   )r!   r   )r!   r|   )r   r   r   r   )rV   rW   rX   rY   	STR_FIELDr	   rh   rg   r   rp   r~   r   r   r   r   r   rZ   r   r   r   r   rb     s$    &
 
rb   )rY   
__future__r   typingr   r   constr   
exceptionsr   utilr   
util.cacher	   baser   r   r#   r   rg   r   collections.abcr   praw.modelsprawr   r[   rf   rb   _subreddit_collections_classr   r   r   r   <module>   s*     IK[ 
G