Skip to content

Commit

Permalink
Merge pull request #254 from praw-dev/flair_reorder
Browse files Browse the repository at this point in the history
Added support for reordering flairs through reddit.flair.reorder
  • Loading branch information
LilSpazJoekp authored Oct 13, 2023
2 parents 2c71fd7 + baadf37 commit acc25c1
Show file tree
Hide file tree
Showing 7 changed files with 1,063 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ Async PRAW follows `semantic versioning <https://semver.org/>`_.
Unreleased
----------

**Added**

- :meth:`~.SubredditLinkFlairTemplates.reorder` to reorder a subreddit's link flair
templates.
- :meth:`~.SubredditRedditorFlairTemplates.reorder` to reorder a subreddit's redditor
flair templates.

**Fixed**

- XML parsing error when media uploads fail.
Expand Down
1 change: 1 addition & 0 deletions asyncpraw/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"flairtemplate_v2": "r/{subreddit}/api/flairtemplate_v2",
"flairtemplateclear": "r/{subreddit}/api/clearflairtemplates/",
"flairtemplatedelete": "r/{subreddit}/api/deleteflairtemplate/",
"flairtemplatereorder": "r/{subreddit}/api/flair_template_order",
"friend": "r/{subreddit}/api/friend/",
"friend_v1": "api/v1/me/friends/{user}",
"friends": "api/v1/me/friends/",
Expand Down
44 changes: 44 additions & 0 deletions asyncpraw/models/reddit/subreddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2371,6 +2371,17 @@ async def delete(self, template_id: str):
url = API_PATH["flairtemplatedelete"].format(subreddit=self.subreddit)
await self.subreddit._reddit.post(url, data={"flair_template_id": template_id})

async def _reorder(self, flair_list: list, *, is_link: Optional[bool] = None):
url = API_PATH["flairtemplatereorder"].format(subreddit=self.subreddit)
await self.subreddit._reddit.patch(
url,
params={
"flair_type": self.flair_type(is_link),
"subreddit": self.subreddit.display_name,
},
json=flair_list,
)

@_deprecate_args(
"template_id",
"text",
Expand Down Expand Up @@ -4400,6 +4411,22 @@ async def clear(self):
"""
await self._clear(is_link=True)

async def reorder(self, flair_list: List[str]):
"""Reorder a list of flairs.
:param flair_list: A list of flair IDs.
For example, to reverse the order of the link flair list try:
.. code-block:: python
subreddit = await reddit.subreddit("test")
flairs = [flair["id"] async for flair in subreddit.flair.link_templates]
await subreddit.flair.link_templates.reorder(list(reversed(flairs)))
"""
await self._reorder(flair_list, is_link=True)

async def user_selectable(
self,
) -> AsyncGenerator[Dict[str, Union[str, bool]], None]:
Expand Down Expand Up @@ -4520,3 +4547,20 @@ async def clear(self):
"""
await self._clear(is_link=False)

async def reorder(self, flair_list: List[str]):
"""Reorder a list of flairs.
:param flair_list: A list of flair IDs.
For example, to reverse the order of the :class:`.Redditor` flair templates list
try:
.. code-block:: python
subreddit = reddit.subreddit("test")
flairs = [flair["id"] for flair in subreddit.flair.templates]
subreddit.flair.templates.reorder(list(reversed(flairs)))
"""
await self._reorder(flair_list, is_link=False)
17 changes: 10 additions & 7 deletions asyncpraw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
AsyncGenerator,
Dict,
Iterable,
List,
Optional,
Type,
Union,
Expand Down Expand Up @@ -543,7 +544,7 @@ async def _objectify_request(
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
files: Optional[Dict[str, IO]] = None,
json: Optional[Dict[Any, Any]] = None,
json: Optional[Union[Dict[Any, Any], List[Any]]] = None,
method: str = "",
params: Optional[Union[str, Dict[str, str]]] = None,
path: str = "",
Expand Down Expand Up @@ -743,7 +744,7 @@ async def delete(
path: str,
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
json: Optional[Dict[Any, Any]] = None,
json: Optional[Union[Dict[Any, Any], List[Any]]] = None,
params: Optional[Union[str, Dict[str, str]]] = None,
) -> Any:
"""Return parsed objects returned from a DELETE request to ``path``.
Expand Down Expand Up @@ -870,7 +871,8 @@ async def patch(
path: str,
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
json: Optional[Dict[Any, Any]] = None,
json: Optional[Union[Dict[Any, Any], List[Any]]] = None,
params: Optional[Union[str, Dict[str, str]]] = None,
) -> Any:
"""Return parsed objects returned from a PATCH request to ``path``.
Expand All @@ -880,10 +882,11 @@ async def patch(
:param json: JSON-serializable object to send in the body of the request with a
Content-Type header of application/json (default: ``None``). If ``json`` is
provided, ``data`` should not be.
:param params: The query parameters to add to the request (default: ``None``).
"""
return await self._objectify_request(
data=data, json=json, method="PATCH", path=path
data=data, json=json, method="PATCH", params=params, path=path
)

@_deprecate_args("path", "data", "files", "params", "json")
Expand All @@ -893,7 +896,7 @@ async def post(
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
files: Optional[Dict[str, IO]] = None,
json: Optional[Dict[Any, Any]] = None,
json: Optional[Union[Dict[Any, Any], List[Any]]] = None,
params: Optional[Union[str, Dict[str, str]]] = None,
) -> Any:
"""Return parsed objects returned from a POST request to ``path``.
Expand Down Expand Up @@ -941,7 +944,7 @@ async def put(
path: str,
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
json: Optional[Dict[Any, Any]] = None,
json: Optional[Union[Dict[Any, Any], List[Any]]] = None,
):
"""Return parsed objects returned from a PUT request to ``path``.
Expand Down Expand Up @@ -1006,7 +1009,7 @@ async def request(
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
files: Optional[Dict[str, IO]] = None,
json: Optional[Dict[Any, Any]] = None,
json: Optional[Union[Dict[Any, Any], List[Any]]] = None,
method: str,
params: Optional[Union[str, Dict[str, Union[str, int]]]] = None,
path: str,
Expand Down
Loading

0 comments on commit acc25c1

Please sign in to comment.