-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
editable.py
67 lines (49 loc) · 1.87 KB
/
editable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Provide the EditableMixin class."""
from typing import TYPE_CHECKING, Union
from ....const import API_PATH
if TYPE_CHECKING: # pragma: no cover
import asyncpraw
class EditableMixin:
"""Interface for classes that can be edited and deleted."""
async def delete(self):
"""Delete the object.
Example usage:
.. code-block:: python
comment = await reddit.comment("dkk4qjd", fetch=False)
await comment.delete()
submission = await reddit.submission("8dmv8z", fetch=False)
await submission.delete()
"""
await self._reddit.post(API_PATH["del"], data={"id": self.fullname})
async def edit(
self, body: str
) -> Union["asyncpraw.models.Comment", "asyncpraw.models.Submission"]:
"""Replace the body of the object with ``body``.
:param body: The Markdown formatted content for the updated object.
:returns: The current instance after updating its attributes.
Example usage:
.. code-block:: python
comment = await reddit.comment("dkk4qjd")
# construct the text of an edited comment
# by appending to the old body:
edited_body = comment.body + "Edit: thanks for the gold!"
await comment.edit(edited_body)
"""
data = {
"text": body,
"thing_id": self.fullname,
"validate_on_submit": self._reddit.validate_on_submit,
}
response = await self._reddit.post(API_PATH["edit"], data=data)
updated = response[0]
for attribute in [
"_fetched",
"_reddit",
"_submission",
"replies",
"subreddit",
]:
if attribute in updated.__dict__:
delattr(updated, attribute)
self.__dict__.update(updated.__dict__)
return self