Skip to content

Commit

Permalink
Add support to update the display layout of posts in collections
Browse files Browse the repository at this point in the history
  • Loading branch information
jsk56143 authored and LilSpazJoekp committed Dec 24, 2021
1 parent d3d3e00 commit f49d373
Show file tree
Hide file tree
Showing 11 changed files with 2,049 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -77,4 +77,5 @@ Source Contributors
- Yash Chhabria `@Cyatos <https://github.com/Cyatos>`_
- Justin Krejcha <justin@justinkrejcha.com> `@jkrejcha <https://github.com/jkrejcha>`_
- Dio Brando `@isFakeAccount <https://github.com/isFakeAccount>`_
- Josh Kim `@jsk56143 <https://github.com/jsk56143>`_
- Add "Name <email (optional)> and github profile link" above this line.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -9,6 +9,8 @@ Unreleased
**Added**

- :meth:`.pin` to manage pinned submissions on the authenticated user's profile.
- :meth:`.update_display_layout` to update the display layout of posts in a
:class:`.Collection`.

**Changed**

Expand Down
1 change: 1 addition & 0 deletions praw/endpoints.py
Expand Up @@ -26,6 +26,7 @@
"collection_create": "api/v1/collections/create_collection",
"collection_delete": "api/v1/collections/delete_collection",
"collection_desc": "api/v1/collections/update_collection_description",
"collection_layout": "api/v1/collections/update_collection_display_layout",
"collection_follow": "api/v1/collections/follow_collection",
"collection_remove_post": "api/v1/collections/remove_post_in_collection",
"collection_reorder": "api/v1/collections/reorder_collection",
Expand Down
26 changes: 26 additions & 0 deletions praw/models/reddit/collections.py
Expand Up @@ -165,6 +165,31 @@ def update_description(self, description: str):
data={"collection_id": self.collection_id, "description": description},
)

def update_display_layout(self, display_layout: str):
"""Update the collection's display layout.
:param display_layout: Either ``"TIMELINE"`` for events or discussions or
``"GALLERY"`` for images or memes. Passing ``""`` or ``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")
"""
self._reddit.post(
API_PATH["collection_layout"],
data={
"collection_id": self.collection_id,
"display_layout": display_layout,
},
)

def update_title(self, title: str):
"""Update the collection's title.
Expand Down Expand Up @@ -220,6 +245,7 @@ class Collection(RedditBase):
``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.
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

64 changes: 63 additions & 1 deletion tests/integration/models/reddit/test_collections.py
Expand Up @@ -4,7 +4,7 @@

import pytest

from praw.exceptions import ClientException
from praw.exceptions import ClientException, RedditAPIException
from praw.models import Submission

from ... import IntegrationTest
Expand Down Expand Up @@ -138,6 +138,68 @@ def test_update_description(self, _):
collection.mod.update_description(new_description)
assert new_description == collection.description

@mock.patch("time.sleep", return_value=None)
def test_update_display_layout__empty_string(self, _):
self.reddit.read_only = False
uuid = "accd53cf-6f76-49fd-8ca5-5ad2036b4693"
empty_string = ""
with self.use_cassette():
collection = self.subreddit.collections(uuid)
collection.mod.update_display_layout(empty_string)
assert empty_string != collection.display_layout
assert collection.display_layout is None

@mock.patch("time.sleep", return_value=None)
def test_update_display_layout__gallery(self, _):
self.reddit.read_only = False
uuid = "accd53cf-6f76-49fd-8ca5-5ad2036b4693"
gallery_layout = "GALLERY"
with self.use_cassette():
collection = self.subreddit.collections(uuid)
collection.mod.update_display_layout(gallery_layout)
assert gallery_layout == collection.display_layout

@mock.patch("time.sleep", return_value=None)
def test_update_display_layout__invalid_layout(self, _):
self.reddit.read_only = False
uuid = "accd53cf-6f76-49fd-8ca5-5ad2036b4693"
invalid_layout = "colossal atom cake"
with self.use_cassette():
collection = self.subreddit.collections(uuid)
with pytest.raises(RedditAPIException):
collection.mod.update_display_layout(invalid_layout)
assert collection.display_layout is None

@mock.patch("time.sleep", return_value=None)
def test_update_display_layout__lowercase(self, _):
self.reddit.read_only = False
uuid = "accd53cf-6f76-49fd-8ca5-5ad2036b4693"
lowercase_gallery_layout = "gallery"
with self.use_cassette():
collection = self.subreddit.collections(uuid)
with pytest.raises(RedditAPIException):
collection.mod.update_display_layout(lowercase_gallery_layout)
assert collection.display_layout is None

@mock.patch("time.sleep", return_value=None)
def test_update_display_layout__none(self, _):
self.reddit.read_only = False
uuid = "accd53cf-6f76-49fd-8ca5-5ad2036b4693"
with self.use_cassette():
collection = self.subreddit.collections(uuid)
collection.mod.update_display_layout(None)
assert collection.display_layout is None

@mock.patch("time.sleep", return_value=None)
def test_update_display_layout__timeline(self, _):
self.reddit.read_only = False
uuid = "accd53cf-6f76-49fd-8ca5-5ad2036b4693"
timeline_layout = "TIMELINE"
with self.use_cassette():
collection = self.subreddit.collections(uuid)
collection.mod.update_display_layout(timeline_layout)
assert timeline_layout == collection.display_layout

@mock.patch("time.sleep", return_value=None)
def test_update_title(self, _):
self.reddit.read_only = False
Expand Down

0 comments on commit f49d373

Please sign in to comment.