Acccept pk sk as well as path when updating post
This commit is contained in:
parent
921b6f5d95
commit
0a76b6e7f8
3 changed files with 98 additions and 13 deletions
|
|
@ -25,6 +25,11 @@ potentially misleading.
|
|||
1. Make more jobs
|
||||
2. Make more wrappers
|
||||
3. Write more documentation
|
||||
4. Wrappers should also accept actual keys for when you know them
|
||||
5. Add utility to manage the token easily to encourage reuse over
|
||||
new tokens every time
|
||||
|
||||
|
||||
|
||||
Trust
|
||||
Follow
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from trustcafeapiwrapper.utils import get_post_pksk, get_parent_pksk_from_path
|
||||
|
||||
def update_post(parent_path, post_path, post_text, blur_label=None, card_url=None, collaborative=False):
|
||||
def update_post( post_text, post_path=None, parent_path='/', post_key=None, blur_label=None, card_url=None, collaborative=False):
|
||||
"""
|
||||
Updates an existing post.
|
||||
|
||||
|
|
@ -15,21 +15,38 @@ def update_post(parent_path, post_path, post_text, blur_label=None, card_url=Non
|
|||
Returns:
|
||||
dict: The updated post data.
|
||||
"""
|
||||
parent_pksk = get_parent_pksk_from_path(parent_path)
|
||||
post_pksk = get_post_pksk(parent_pksk, post_path)
|
||||
if post_key is not None:
|
||||
post_pksk = {
|
||||
'pk': post_key.get('pk', None),
|
||||
'sk': post_key.get('sk', None)
|
||||
}
|
||||
elif post_path is not None and parent_path is not None:
|
||||
parent_pksk = get_parent_pksk_from_path(parent_path)
|
||||
post_pksk = get_post_pksk(parent_pksk, post_path)
|
||||
else:
|
||||
raise ValueError("Either post_path and parent_path or post_key must be provided.")
|
||||
|
||||
|
||||
if post_text is None:
|
||||
raise ValueError("post_text is required.")
|
||||
|
||||
|
||||
payload = {
|
||||
"key": {
|
||||
"pk": post_pksk.get('pk', None),
|
||||
"sk": post_pksk.get('sk', None)
|
||||
},
|
||||
"postSlug": post_path.strip('/post/'),
|
||||
"blurLabel": blur_label,
|
||||
"cardUrl": card_url,
|
||||
"postText": post_text,
|
||||
"collaborative": collaborative,
|
||||
|
||||
"postSlug": post_pksk.get('sk', '').replace('post#', ''),
|
||||
"postText": post_text
|
||||
}
|
||||
if collaborative is not None:
|
||||
payload["collaborative"] = collaborative
|
||||
|
||||
if blur_label is not None:
|
||||
payload["blurLabel"] = blur_label
|
||||
|
||||
if card_url is not None:
|
||||
payload["cardUrl"] = card_url
|
||||
|
||||
return {
|
||||
"job_function": "post.update",
|
||||
|
|
|
|||
|
|
@ -21,10 +21,73 @@ class TestUpdatePost(unittest.TestCase):
|
|||
self.assertIn("payload", result)
|
||||
self.assertEqual(result["job_function"], "post.update")
|
||||
self.assertEqual(result["payload"]["postText"], self.post_text)
|
||||
self.assertEqual(result["payload"]["blurLabel"], self.blur_label)
|
||||
self.assertEqual(result["payload"]["cardUrl"], self.card_url)
|
||||
self.assertEqual(result["payload"]["collaborative"], self.collaborative)
|
||||
self.assertEqual(result["payload"]["postSlug"], "1235-abcv")
|
||||
self.assertEqual(result["payload"]["key"]["pk"], "maintrunk#maintrunk")
|
||||
self.assertEqual(result["payload"]["key"]["sk"], "post#1235-abcv")
|
||||
self.assertNotIn("slug", result["payload"]["key"])
|
||||
|
||||
def test_update_post_with_post_key(self):
|
||||
post_key = {
|
||||
"pk": "maintrunk#maintrunk",
|
||||
"sk": "post#1235-abcv"
|
||||
}
|
||||
result = update_post(
|
||||
post_key=post_key,
|
||||
post_text=self.post_text,
|
||||
blur_label=self.blur_label,
|
||||
card_url=self.card_url,
|
||||
collaborative=self.collaborative,
|
||||
)
|
||||
self.assertIsInstance(result, dict)
|
||||
self.assertIn("job_function", result)
|
||||
self.assertIn("payload", result)
|
||||
self.assertEqual(result["job_function"], "post.update")
|
||||
self.assertEqual(result["payload"]["postText"], self.post_text)
|
||||
self.assertEqual(result["payload"]["postSlug"], "1235-abcv")
|
||||
self.assertEqual(result["payload"]["key"]["pk"], post_key["pk"])
|
||||
self.assertEqual(result["payload"]["key"]["sk"], post_key["sk"])
|
||||
self.assertNotIn("slug", result["payload"]["key"])
|
||||
|
||||
def test_update_collaborative_post(self):
|
||||
collaborative = True
|
||||
result = update_post(
|
||||
parent_path='/',
|
||||
post_path='/post/1235-abcv',
|
||||
post_text=self.post_text,
|
||||
blur_label=self.blur_label,
|
||||
card_url=self.card_url,
|
||||
collaborative=collaborative
|
||||
)
|
||||
self.assertIsInstance(result, dict)
|
||||
self.assertIn("job_function", result)
|
||||
self.assertIn("payload", result)
|
||||
self.assertEqual(result["job_function"], "post.update")
|
||||
self.assertEqual(result["payload"]["postText"], self.post_text)
|
||||
self.assertEqual(result["payload"]["postSlug"], "1235-abcv")
|
||||
self.assertEqual(result["payload"]["key"]["pk"], "maintrunk#maintrunk")
|
||||
self.assertEqual(result["payload"]["key"]["sk"], "post#1235-abcv")
|
||||
self.assertNotIn("slug", result["payload"]["key"])
|
||||
self.assertTrue(result["payload"]["collaborative"])
|
||||
|
||||
def test_update_blur_label_and_card_url(self):
|
||||
blur_label = "Sensitive Content"
|
||||
card_url = "https://example.com/card"
|
||||
result = update_post(
|
||||
parent_path='/',
|
||||
post_path='/post/1235-abcv',
|
||||
post_text=self.post_text,
|
||||
blur_label=blur_label,
|
||||
card_url=card_url,
|
||||
collaborative=self.collaborative
|
||||
)
|
||||
self.assertIsInstance(result, dict)
|
||||
self.assertIn("job_function", result)
|
||||
self.assertIn("payload", result)
|
||||
self.assertEqual(result["job_function"], "post.update")
|
||||
self.assertEqual(result["payload"]["postText"], self.post_text)
|
||||
self.assertEqual(result["payload"]["postSlug"], "1235-abcv")
|
||||
self.assertEqual(result["payload"]["key"]["pk"], "maintrunk#maintrunk")
|
||||
self.assertEqual(result["payload"]["key"]["sk"], "post#1235-abcv")
|
||||
self.assertNotIn("slug", result["payload"]["key"])
|
||||
self.assertEqual(result["payload"]["blurLabel"], blur_label)
|
||||
self.assertEqual(result["payload"]["cardUrl"], card_url)
|
||||
Loading…
Add table
Add a link
Reference in a new issue