Vote casting
This commit is contained in:
parent
8704ad0e70
commit
80786c4965
12 changed files with 152 additions and 42 deletions
1
src/trustcafeapiwrapper/jobs/vote/__init__.py
Normal file
1
src/trustcafeapiwrapper/jobs/vote/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .votecast import votecast
|
||||
12
src/trustcafeapiwrapper/jobs/vote/votecast.py
Normal file
12
src/trustcafeapiwrapper/jobs/vote/votecast.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
def votecast(API, payload: dict) -> dict:
|
||||
"""
|
||||
Casts a vote in the API.
|
||||
|
||||
Args:
|
||||
payload (dict): The data for the vote.
|
||||
|
||||
Returns:
|
||||
dict: The vote data.
|
||||
"""
|
||||
vote_data = API.make_request("POST", "content", "votecast", data=payload, authenticate=True)
|
||||
return vote_data
|
||||
|
|
@ -2,3 +2,4 @@ from .get_parent_pksk_from_path import get_parent_pksk_from_path
|
|||
from .get_post_pksk import get_post_pksk
|
||||
from .make_comment_sk import make_comment_sk
|
||||
from .make_post_sk import make_post_sk
|
||||
from .get_child_spksk_from_paths import get_child_spksk_from_paths
|
||||
34
src/trustcafeapiwrapper/utils/get_child_spksk_from_paths.py
Normal file
34
src/trustcafeapiwrapper/utils/get_child_spksk_from_paths.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from trustcafeapiwrapper.utils import get_parent_pksk_from_path, get_post_pksk, make_comment_sk, make_post_sk
|
||||
|
||||
def get_child_spksk_from_paths(parent_path: str, item_path: str):
|
||||
|
||||
'''
|
||||
|
||||
If it's a post we want to create a pk/sk like:
|
||||
'{entity}#{parent_slug}' / 'post#{post_slug}'
|
||||
|
||||
If it's a comment we want to create a pk/sk like:
|
||||
'post#{post_slug}' / 'comment#{comment_slug}'
|
||||
|
||||
|
||||
'''
|
||||
slug = item_path.split('/')[-1]
|
||||
if item_path.startswith('/post'):
|
||||
# It's a reaction on a post
|
||||
top_level_parent_pk = get_parent_pksk_from_path(parent_path)
|
||||
item_pksk = get_post_pksk(top_level_parent_pk, item_path)
|
||||
entity = 'post'
|
||||
else:
|
||||
# It's a reaction on a comment
|
||||
item_pksk = {
|
||||
'pk': make_post_sk(parent_path),
|
||||
'sk': make_comment_sk(item_path)
|
||||
}
|
||||
entity = 'comment'
|
||||
|
||||
return {
|
||||
"pk": item_pksk.get('pk', None),
|
||||
"sk": item_pksk.get('sk', None),
|
||||
"entity": entity,
|
||||
"slug": slug
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
from trustcafeapiwrapper.utils import get_parent_pksk_from_path, get_post_pksk, make_comment_sk, make_post_sk
|
||||
|
||||
from trustcafeapiwrapper.utils import get_child_spksk_from_paths
|
||||
def react(reaction_type: str, parent_path: str, item_path: str):
|
||||
"""
|
||||
React to something. ie a post or a comment.
|
||||
|
|
@ -12,40 +12,11 @@ def react(reaction_type: str, parent_path: str, item_path: str):
|
|||
dict: A dictionary containing the job name and payload for creating the post
|
||||
that will be processed by the API client wrapper function.
|
||||
"""
|
||||
|
||||
'''
|
||||
|
||||
If it's a post we want to create a pk/sk like:
|
||||
'{entity}#{parent_slug}' / 'post#{post_slug}'
|
||||
|
||||
If it's a comment we want to create a pk/sk like:
|
||||
'post#{post_slug}' / 'comment#{comment_slug}'
|
||||
|
||||
|
||||
'''
|
||||
slug = item_path.split('/')[-1]
|
||||
if item_path.startswith('/post'):
|
||||
# It's a reaction on a post
|
||||
top_level_parent_pk = get_parent_pksk_from_path(parent_path)
|
||||
item_pksk = get_post_pksk(top_level_parent_pk, item_path)
|
||||
entity = 'post'
|
||||
else:
|
||||
# It's a reaction on a comment
|
||||
item_pksk = {
|
||||
'pk': make_post_sk(parent_path),
|
||||
'sk': make_comment_sk(item_path)
|
||||
}
|
||||
entity = 'comment'
|
||||
|
||||
parent = get_child_spksk_from_paths(parent_path, item_path)
|
||||
return {
|
||||
"job_function": "reaction.reacttosomething",
|
||||
"payload": {
|
||||
"reaction": reaction_type,
|
||||
"parent": {
|
||||
"pk": item_pksk.get('pk', None),
|
||||
"sk": item_pksk.get('sk', None),
|
||||
"entity": entity,
|
||||
"slug": slug
|
||||
}
|
||||
"parent": parent
|
||||
}
|
||||
}
|
||||
|
|
|
|||
1
src/trustcafeapiwrapper/wrappers/vote/__init__.py
Normal file
1
src/trustcafeapiwrapper/wrappers/vote/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .votecast import votecast
|
||||
21
src/trustcafeapiwrapper/wrappers/vote/votecast.py
Normal file
21
src/trustcafeapiwrapper/wrappers/vote/votecast.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from trustcafeapiwrapper.utils import get_child_spksk_from_paths
|
||||
|
||||
def votecast(vote: str, parent_path: str, item_path: str):
|
||||
"""
|
||||
Creates a new vote in the API.
|
||||
|
||||
Args:
|
||||
|
||||
Returns:
|
||||
dict: A dictionary containing the job name and payload for creating the post
|
||||
that will be processed by the API client wrapper function.
|
||||
"""
|
||||
parent = get_child_spksk_from_paths(parent_path, item_path)
|
||||
|
||||
return {
|
||||
"job_function": "vote.votecast",
|
||||
"payload": {
|
||||
"vote": vote,
|
||||
"parent": parent
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue