Allow comments to be passed a key instead of path

This commit is contained in:
simonwt 2026-04-13 22:31:11 +01:00
parent deb19d78ae
commit 8453c10efe
2 changed files with 36 additions and 6 deletions

View file

@ -1,7 +1,7 @@
from trustcafeapiwrapper.utils.get_parent_pksk_from_path import get_parent_pksk_from_path from trustcafeapiwrapper.utils.get_parent_pksk_from_path import get_parent_pksk_from_path
from trustcafeapiwrapper.utils.get_post_pksk import get_post_pksk from trustcafeapiwrapper.utils.get_post_pksk import get_post_pksk
def create_comment(comment_text, post_slug, parent_path, blur_label=None, version=3): def create_comment(comment_text, post_slug=None, parent_path=None, post_key=None, blur_label=None, version=3):
""" """
Creates a new comment. Creates a new comment.
@ -15,9 +15,20 @@ def create_comment(comment_text, post_slug, parent_path, blur_label=None, versio
dict: A dictionary containing the job name and payload for creating the comment dict: A dictionary containing the job name and payload for creating the comment
that will be processed by the API client wrapper function. that will be processed by the API client wrapper function.
""" """
if post_key is not None:
parent_pksk = get_parent_pksk_from_path(parent_path) post_pksk = {
post_pksk = get_post_pksk(parent_pksk, post_slug) "pk": post_key.get('pk'),
"sk": post_key.get('sk')
}
elif parent_path is not None and post_slug is not None:
parent_pksk = get_parent_pksk_from_path(parent_path)
post_pksk = get_post_pksk(parent_pksk, post_slug)
else:
raise ValueError("Either post_key or both parent_path and post_slug must be provided.")
if comment_text is None or comment_text.strip() == "":
raise ValueError("Comment text cannot be empty.")
return { return {
"job_function": "comment.create", "job_function": "comment.create",
"payload": { "payload": {
@ -26,7 +37,7 @@ def create_comment(comment_text, post_slug, parent_path, blur_label=None, versio
"parent": { "parent": {
"pk": post_pksk['pk'], "pk": post_pksk['pk'],
"sk": post_pksk['sk'], "sk": post_pksk['sk'],
"slug": parent_path.split('/')[-1] "slug": post_pksk.get('sk', '').replace('post#', ''),
}, },
"topLevel": { "topLevel": {
"pk": post_pksk['pk'], "pk": post_pksk['pk'],

View file

@ -19,4 +19,23 @@ class TestCreateComment(unittest.TestCase):
self.assertEqual(result["payload"]["commentText"], self.comment_text) self.assertEqual(result["payload"]["commentText"], self.comment_text)
self.assertEqual(result["payload"]["blurLabel"], self.blur_label) self.assertEqual(result["payload"]["blurLabel"], self.blur_label)
self.assertEqual(result["payload"]["parent"]["pk"], "maintrunk#maintrunk") self.assertEqual(result["payload"]["parent"]["pk"], "maintrunk#maintrunk")
self.assertEqual(result["payload"]["parent"]["sk"], "post#1774951384-98fe38df") self.assertEqual(result["payload"]["parent"]["sk"], "post#1774951384-98fe38df")
def test_create_comment_with_post_key(self):
post_key = {
"pk": "maintrunk#maintrunk",
"sk": "post#1774951384-98fe38df"
}
result = create_comment(
comment_text=self.comment_text,
post_key=post_key,
blur_label=self.blur_label
)
self.assertIsInstance(result, dict)
self.assertIn("job_function", result)
self.assertIn("payload", result)
self.assertEqual(result["job_function"], "comment.create")
self.assertEqual(result["payload"]["commentText"], self.comment_text)
self.assertEqual(result["payload"]["blurLabel"], self.blur_label)
self.assertEqual(result["payload"]["parent"]["pk"], post_key["pk"])
self.assertEqual(result["payload"]["parent"]["sk"], post_key["sk"])