Skip to content

Commit

Permalink
Merge pull request #352 from EvanBldy/main
Browse files Browse the repository at this point in the history
[task] add new function publish_comments_previews to publish multiple…
  • Loading branch information
EvanBldy authored Jan 7, 2025
2 parents 205d818 + a76aad0 commit a49b916
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
27 changes: 19 additions & 8 deletions gazu/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,11 @@ def get_message_from_response(
message = default_message
message_json = response.json()

for key in ["error", "message"]:
if message_json.get(key):
message = message_json[key]
break
if hasattr(message_json, "get"):
for key in ["error", "message"]:
if message_json.get(key):
message = message_json[key]
break

return message

Expand Down Expand Up @@ -663,7 +664,14 @@ def update(model_name, model_id, data, client=default_client):
)


def upload(path, file_path, data={}, extra_files=[], client=default_client):
def upload(
path,
file_path=None,
data={},
extra_files=[],
files=None,
client=default_client,
):
"""
Upload file located at *file_path* to given url *path*.
Expand All @@ -672,13 +680,15 @@ def upload(path, file_path, data={}, extra_files=[], client=default_client):
file_path (str): The file location on the hard drive.
data (dict): The data to send with the file.
extra_files (list): List of extra files to upload.
files (dict): The dictionary of files to upload.
client (KitsuClient): The client to use for the request.
Returns:
Response: Request response object.
"""
url = get_full_url(path, client)
files = _build_file_dict(file_path, extra_files)
if not files:
files = _build_file_dict(file_path, extra_files)
retry = True
while retry:
response = client.session.post(
Expand Down Expand Up @@ -714,10 +724,11 @@ def _build_file_dict(file_path, extra_files):
"""

files = {"file": open(file_path, "rb")}
i = 2
i = 0
for file_path in extra_files:
files["file-%s" % i] = open(file_path, "rb")
i += 1
files["file-%s" % i] = open(file_path, "rb")

return files


Expand Down
37 changes: 37 additions & 0 deletions gazu/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ def add_comment(
"task_status_id": task_status["id"],
"comment": comment,
"checklist": checklist,
"links": links,
}

if person is not None:
Expand Down Expand Up @@ -1014,6 +1015,7 @@ def publish_preview(
normalize_movie=True,
revision=None,
set_thumbnail=False,
links=[],
client=default,
):
"""
Expand All @@ -1035,6 +1037,7 @@ def publish_preview(
server side.
revision (int): Revision number.
set_thumbnail (bool): Set the preview as thumbnail of the entity.
links (list): List of links to add to the comment
Returns:
tuple(dict, dict): Created comment model and created preview file
model.
Expand All @@ -1047,6 +1050,7 @@ def publish_preview(
checklist=checklist,
attachments=attachments,
created_at=created_at,
links=links,
client=client,
)
preview_file = add_preview(
Expand All @@ -1063,6 +1067,39 @@ def publish_preview(
return new_comment, preview_file


def publish_comments_previews(task, comments=[], client=default):
"""
Publish a list of comments (with attachments and previews) for given task.
Each dict comments may contain a list of attachment files path and preview
files path in the keys "attachment_files" and "preview_files".
Args:
task (str / dict): The task dict or the task ID.
comments (list): List of comments to publish.
Returns:
list: List of created comments.
"""
task = normalize_model_parameter(task)

files = {}
for x, comment in enumerate(comments):
if comment.get("attachment_files"):
for y, file_path in enumerate(comment["attachment_files"]):
files["attachment_file-%i-%i" % (x, y)] = open(file_path, "rb")
if comment.get("preview_files"):
for y, file_path in enumerate(comment["preview_files"]):
files["preview_file-%i-%i" % (x, y)] = open(file_path, "rb")

files["comments"] = (None, json.dumps(comments), "application/json")
return raw.upload(
"actions/tasks/%s/add-comments-previews" % task["id"],
file_path=None,
files=files,
client=client,
)


def set_main_preview(preview_file, frame_number=None, client=default):
"""
Set given preview as thumbnail of given entity.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def test_upload_multiple_files(self):
mock,
{
"file": test_file_read,
"file-2": test_file_read,
"file-1": test_file_read,
"test": "True",
},
)
Expand Down

0 comments on commit a49b916

Please sign in to comment.