Skip to content

Commit

Permalink
Merge pull request #162 from praw-dev/collection-layout
Browse files Browse the repository at this point in the history
Add support to update the display layout of posts in collections
  • Loading branch information
LilSpazJoekp committed Dec 24, 2021
2 parents 7922def + ac2dea6 commit 4cbbff9
Show file tree
Hide file tree
Showing 11 changed files with 1,949 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -84,4 +84,5 @@ Source Contributors
<https://github.com/GerardRodes>`_
- cmays90 `@cmays90 <https://github.com/cmays90>`_
- 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 asyncpraw/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
27 changes: 27 additions & 0 deletions asyncpraw/models/reddit/collections.py
Expand Up @@ -175,6 +175,32 @@ async def update_description(self, description: str):
data={"collection_id": self.collection_id, "description": description},
)

async 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
subreddit = await reddit.subreddit("test")
collection = await subreddit.collections("some_uuid")
await collection.mod.update_display_layout("GALLERY")
"""
await self._reddit.post(
API_PATH["collection_layout"],
data={
"collection_id": self.collection_id,
"display_layout": display_layout,
},
)

async def update_title(self, title: str):
"""Update the collection's title.
Expand Down Expand Up @@ -233,6 +259,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.

76 changes: 75 additions & 1 deletion tests/integration/models/reddit/test_collections.py
Expand Up @@ -3,7 +3,7 @@
import pytest
from asynctest import mock

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

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

@mock.patch("asyncio.sleep", return_value=None)
async def test_update_display_layout__empty_string(self, _):
self.reddit.read_only = False
uuid = "3aa31024-711b-46b2-9514-3fd50619f6e8"
empty_string = ""
with self.use_cassette():
subreddit = await self.reddit.subreddit(pytest.placeholders.test_subreddit)
collection = await subreddit.collections(uuid, fetch=False)
await collection.mod.update_display_layout(empty_string)
await collection.load()
assert empty_string != collection.display_layout
assert collection.display_layout is None

@mock.patch("asyncio.sleep", return_value=None)
async def test_update_display_layout__gallery(self, _):
self.reddit.read_only = False
uuid = "3aa31024-711b-46b2-9514-3fd50619f6e8"
gallery_layout = "GALLERY"
with self.use_cassette():
subreddit = await self.reddit.subreddit(pytest.placeholders.test_subreddit)
collection = await subreddit.collections(uuid, fetch=False)
await collection.mod.update_display_layout(gallery_layout)
await collection.load()
assert gallery_layout == collection.display_layout

@mock.patch("asyncio.sleep", return_value=None)
async def test_update_display_layout__invalid_layout(self, _):
self.reddit.read_only = False
uuid = "3aa31024-711b-46b2-9514-3fd50619f6e8"
invalid_layout = "colossal atom cake"
with self.use_cassette():
subreddit = await self.reddit.subreddit(pytest.placeholders.test_subreddit)
collection = await subreddit.collections(uuid, fetch=False)
with pytest.raises(RedditAPIException):
await collection.mod.update_display_layout(invalid_layout)
await collection.load()
assert collection.display_layout is None

@mock.patch("asyncio.sleep", return_value=None)
async def test_update_display_layout__lowercase(self, _):
self.reddit.read_only = False
uuid = "3aa31024-711b-46b2-9514-3fd50619f6e8"
lowercase_gallery_layout = "gallery"
with self.use_cassette():
subreddit = await self.reddit.subreddit(pytest.placeholders.test_subreddit)
collection = await subreddit.collections(uuid, fetch=False)
with pytest.raises(RedditAPIException):
await collection.mod.update_display_layout(lowercase_gallery_layout)
await collection.load()
assert collection.display_layout is None

@mock.patch("asyncio.sleep", return_value=None)
async def test_update_display_layout__none(self, _):
self.reddit.read_only = False
uuid = "3aa31024-711b-46b2-9514-3fd50619f6e8"
with self.use_cassette():
subreddit = await self.reddit.subreddit(pytest.placeholders.test_subreddit)
collection = await subreddit.collections(uuid, fetch=False)
await collection.mod.update_display_layout(None)
await collection.load()
assert collection.display_layout is None

@mock.patch("asyncio.sleep", return_value=None)
async def test_update_display_layout__timeline(self, _):
self.reddit.read_only = False
uuid = "3aa31024-711b-46b2-9514-3fd50619f6e8"
timeline_layout = "TIMELINE"
with self.use_cassette():
subreddit = await self.reddit.subreddit(pytest.placeholders.test_subreddit)
collection = await subreddit.collections(uuid, fetch=False)
await collection.mod.update_display_layout(timeline_layout)
await collection.load()
assert timeline_layout == collection.display_layout

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

0 comments on commit 4cbbff9

Please sign in to comment.