Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for reordering flairs through reddit.flair.reorder #1956

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ PRAW follows `semantic versioning <http://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.

7.7.1 (2023/07/11)
------------------

Expand Down
1 change: 1 addition & 0 deletions praw/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
51 changes: 50 additions & 1 deletion praw/models/reddit/subreddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,11 @@ def _fetch(self):
self.__dict__.update(other.__dict__)
self._fetched = True

def _fetch_data(self) -> dict:
name, fields, params = self._fetch_info()
path = API_PATH[name].format(**fields)
return self._reddit.request(method="GET", params=params, path=path)

def _fetch_info(self):
return "subreddit_about", {"subreddit": self}, None

Expand Down Expand Up @@ -2225,6 +2230,17 @@ def delete(self, template_id: str):
url = API_PATH["flairtemplatedelete"].format(subreddit=self.subreddit)
self.subreddit._reddit.post(url, data={"flair_template_id": template_id})

def _reorder(self, flair_list: list, *, is_link: Optional[bool] = None):
url = API_PATH["flairtemplatereorder"].format(subreddit=self.subreddit)
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 @@ -2728,7 +2744,7 @@ def update(
:param submit_text_label: Custom label for submit text post button (``None`` for
default).
:param subreddit_type: One of ``"archived"``, ``"employees_only"``,
``"gold_only"``, ``"gold_restricted"``, ``"private"``, ``"public"``, or
``"gold_only"``, ``gold_restricted``, ``"private"``, ``"public"``, or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rebase on master?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry... How do I do this? As you can tell, I don't use git all that often! In fact I think this is only my first or maybe second pr that has been taken seriously :)

``"restricted"``.
:param suggested_comment_sort: All comment threads will use this sorting method
by default. Leave ``None``, or choose one of ``"confidence"``,
Expand Down Expand Up @@ -4130,6 +4146,22 @@ def clear(self):
"""
self._clear(is_link=True)

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 = reddit.subreddit("test")
flairs = [flair["id"] for flair in subreddit.flair.link_templates]
subreddit.flair.link_templates.reorder(list(reversed(flairs)))

"""
self._reorder(flair_list, is_link=True)

def user_selectable(
self,
) -> Generator[Dict[str, Union[str, bool]], None, None]:
Expand Down Expand Up @@ -4245,3 +4277,20 @@ def clear(self):

"""
self._clear(is_link=False)

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)))

"""
self._reorder(flair_list, is_link=False)
19 changes: 12 additions & 7 deletions praw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Dict,
Generator,
Iterable,
List,
Optional,
Type,
Union,
Expand Down Expand Up @@ -493,7 +494,7 @@ 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 @@ -670,7 +671,7 @@ 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 @@ -794,7 +795,8 @@ 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 @@ -804,9 +806,12 @@ 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 self._objectify_request(data=data, json=json, method="PATCH", path=path)
return self._objectify_request(
data=data, json=json, method="PATCH", params=params, path=path
)

@_deprecate_args("path", "data", "files", "params", "json")
def post(
Expand All @@ -815,7 +820,7 @@ 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 @@ -863,7 +868,7 @@ 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 @@ -913,7 +918,7 @@ 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
Loading