From a42a3b65d9cd567d7fcb129f0039ff8eeec734b0 Mon Sep 17 00:00:00 2001 From: simonwt Date: Tue, 7 Apr 2026 23:37:55 +0100 Subject: [PATCH] Post update --- src/trustcafeapiwrapper/jobs/post/__init__.py | 3 +- src/trustcafeapiwrapper/jobs/post/update.py | 12 ++++++ .../wrappers/post/__init__.py | 3 +- .../wrappers/post/update_post.py | 37 +++++++++++++++++++ testing.py | 33 +++++++++++++---- tests/utils/get_child_spksk_from_paths.py | 4 +- tests/wrappers/react.py | 10 ++--- tests/wrappers/update_post.py | 30 +++++++++++++++ unittests.py | 1 + 9 files changed, 116 insertions(+), 17 deletions(-) create mode 100644 src/trustcafeapiwrapper/jobs/post/update.py create mode 100644 src/trustcafeapiwrapper/wrappers/post/update_post.py create mode 100644 tests/wrappers/update_post.py diff --git a/src/trustcafeapiwrapper/jobs/post/__init__.py b/src/trustcafeapiwrapper/jobs/post/__init__.py index 6621076..d72a28d 100644 --- a/src/trustcafeapiwrapper/jobs/post/__init__.py +++ b/src/trustcafeapiwrapper/jobs/post/__init__.py @@ -1,7 +1,8 @@ +from .update import update +from .create import create from .get import get from .listbybranch import listbybranch from .listbyuserprofile import listbyuserprofile -from .create import create from .listall import listall from .listpublic import listpublic from .listremoved import listremoved \ No newline at end of file diff --git a/src/trustcafeapiwrapper/jobs/post/update.py b/src/trustcafeapiwrapper/jobs/post/update.py new file mode 100644 index 0000000..80f558d --- /dev/null +++ b/src/trustcafeapiwrapper/jobs/post/update.py @@ -0,0 +1,12 @@ +def update(API, payload: dict) -> dict: + """ + Updates an existing post in the API. + + Args: + payload (dict): The data for the post update. + + Returns: + dict: The post data. + """ + post_data = API.make_request("PUT", "content", "post/update", data=payload, authenticate=True) + return post_data \ No newline at end of file diff --git a/src/trustcafeapiwrapper/wrappers/post/__init__.py b/src/trustcafeapiwrapper/wrappers/post/__init__.py index 61a7e0b..eafe74e 100644 --- a/src/trustcafeapiwrapper/wrappers/post/__init__.py +++ b/src/trustcafeapiwrapper/wrappers/post/__init__.py @@ -1 +1,2 @@ -from .create_post import create_post \ No newline at end of file +from .create_post import create_post +from .update_post import update_post \ No newline at end of file diff --git a/src/trustcafeapiwrapper/wrappers/post/update_post.py b/src/trustcafeapiwrapper/wrappers/post/update_post.py new file mode 100644 index 0000000..d302aab --- /dev/null +++ b/src/trustcafeapiwrapper/wrappers/post/update_post.py @@ -0,0 +1,37 @@ +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): + """ + Updates an existing post. + + Args: + post_slug (str): The slug of the post to update. + post_text (str): The new text for the post. + parent_path (str, optional): The parent path for the post. Defaults to '/'. + blur_label (str, optional): The blur label for the post. Defaults to None. + card_url (str, optional): The card URL for the post. Defaults to None. + collaborative (bool, optional): Whether the post is collaborative. Defaults to False. + + Returns: + dict: The updated post data. + """ + parent_pksk = get_parent_pksk_from_path(parent_path) + post_pksk = get_post_pksk(parent_pksk, post_path) + + 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, + + } + + return { + "job_function": "post.update", + "payload": payload + } \ No newline at end of file diff --git a/testing.py b/testing.py index 5ede80d..ee63098 100644 --- a/testing.py +++ b/testing.py @@ -61,9 +61,9 @@ def save_response(response): # print("-----------------------------") # feed = API.run_job('post.listbybranch', "music") # print(feed) -print("-----------------------------") -feed = save_response(API.run_job('post.listremoved')) -print(feed) +# print("-----------------------------") +# feed = save_response(API.run_job('post.listremoved')) +# print(feed) # print("----------------------------z # branchlist = API.run_job('branch.listbyname') # print(branchlist) @@ -80,6 +80,16 @@ print(feed) # print(feed) # print("-----------------------------") +# save_response(API.run_job('post.create', { +# "blurLabel": None, +# "cardUrl": None, +# "postText": "This is a test post created via the API wrapper.", +# "collaborative": False, +# "parent": { +# "pk": "maintrunk#maintrunk", +# "sk": "maintrunk#maintrunk" +# } +# })) # save_response(API.run_job('post.create', { # "blurLabel": None, # "cardUrl": None, @@ -183,8 +193,15 @@ print(feed) # users = API.run_job('trust.listbyuserhas', "simon-little ") # print(users) -from trustcafeapiwrapper.wrappers.trust import trust -save_response(API.wrapped(trust( - 100, - "/user/bossman" -))) \ No newline at end of file +# from trustcafeapiwrapper.wrappers.trust import trust +# save_response(API.wrapped(trust( +# 100, +# "/user/bossman" +# ))) + +from trustcafeapiwrapper.wrappers.post.update_post import update_post +save_response(API.wrapped(update_post( + post_text="This is an updated version of the test post created via the create_post wrapper function.", + post_path="/post/1775143460-ef45186a", + parent_path="/", +))) diff --git a/tests/utils/get_child_spksk_from_paths.py b/tests/utils/get_child_spksk_from_paths.py index b639c6d..f45526b 100644 --- a/tests/utils/get_child_spksk_from_paths.py +++ b/tests/utils/get_child_spksk_from_paths.py @@ -13,10 +13,10 @@ class TestGetChildSpkskFromPaths(unittest.TestCase): self.assertEqual(get_child_spksk_from_paths(parent_path, item_path), expected_output) def test_comment_reaction(self): - parent_path = '/post/12345' + parent_path = '/post/12345-abcv' item_path = '/comment/67890' expected_output = { - "pk": "post#12345", + "pk": "post#12345-abcv", "sk": "comment#67890", "entity": "comment", "slug": "67890" diff --git a/tests/wrappers/react.py b/tests/wrappers/react.py index 52598f3..641a535 100644 --- a/tests/wrappers/react.py +++ b/tests/wrappers/react.py @@ -5,7 +5,7 @@ class TestReact(unittest.TestCase): def test_react_to_post(self): reaction_type = 'like' parent_path = '/' - item_path = '/post/12345' + item_path = '/post/12345-abcv' result = react(reaction_type, parent_path, item_path) self.assertIsInstance(result, dict) @@ -14,13 +14,13 @@ class TestReact(unittest.TestCase): self.assertEqual(result["job_function"], "reaction.reacttosomething") self.assertEqual(result["payload"]["reaction"], reaction_type) self.assertEqual(result["payload"]["parent"]["pk"], "maintrunk#maintrunk") - self.assertEqual(result["payload"]["parent"]["sk"], "post#12345") + self.assertEqual(result["payload"]["parent"]["sk"], "post#12345-abcv") self.assertEqual(result["payload"]["parent"]["entity"], "post") - self.assertEqual(result["payload"]["parent"]["slug"], "12345") + self.assertEqual(result["payload"]["parent"]["slug"], "12345-abcv") def test_react_to_comment(self): reaction_type = 'like' - parent_path = '/post/12345' + parent_path = '/post/12345-abcv' item_path = '/comment/67890' result = react(reaction_type, parent_path, item_path) @@ -29,7 +29,7 @@ class TestReact(unittest.TestCase): self.assertIn("payload", result) self.assertEqual(result["job_function"], "reaction.reacttosomething") self.assertEqual(result["payload"]["reaction"], reaction_type) - self.assertEqual(result["payload"]["parent"]["pk"], "post#12345") + self.assertEqual(result["payload"]["parent"]["pk"], "post#12345-abcv") self.assertEqual(result["payload"]["parent"]["sk"], "comment#67890") self.assertEqual(result["payload"]["parent"]["entity"], "comment") self.assertEqual(result["payload"]["parent"]["slug"], "67890") \ No newline at end of file diff --git a/tests/wrappers/update_post.py b/tests/wrappers/update_post.py new file mode 100644 index 0000000..03c0d36 --- /dev/null +++ b/tests/wrappers/update_post.py @@ -0,0 +1,30 @@ +import unittest +from trustcafeapiwrapper.wrappers.post.update_post import update_post +class TestUpdatePost(unittest.TestCase): + def setUp(self): + self.post_text = "This is an updated test post created via the update_post wrapper function." + self.blur_label = None + self.card_url = None + self.collaborative = False + + def test_update_post(self): + 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=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"]["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"]) \ No newline at end of file diff --git a/unittests.py b/unittests.py index f787956..d0dc1b9 100644 --- a/unittests.py +++ b/unittests.py @@ -11,6 +11,7 @@ from tests.utils.get_user_slug_from_path import TestGetUserSlugFromPath from tests.utils.get_userprofile_pksk_from_slug import TestGetUserprofilePkskFromSlug from tests.wrappers.create_post import TestCreatePost +from tests.wrappers.update_post import TestUpdatePost from tests.wrappers.create_comment import TestCreateComment from tests.wrappers.react import TestReact from tests.wrappers.vote import TestVoteCast