Skip to content

Commit

Permalink
Merge pull request #319 from EvanBldy/master
Browse files Browse the repository at this point in the history
[files] add functions to upload avatar for projects and persons
  • Loading branch information
EvanBldy authored Mar 14, 2024
2 parents 32b44d9 + 85a2e86 commit 9d0db82
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
33 changes: 31 additions & 2 deletions gazu/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,8 +1109,7 @@ def upload_working_file(working_file, file_path, client=default):
"""
working_file = normalize_model_parameter(working_file)
url_path = "/data/working-files/%s/file" % working_file["id"]
raw.upload(url_path, file_path, client=client)
return working_file
return raw.upload(url_path, file_path, client=client)


def download_working_file(working_file, file_path=None, client=default):
Expand Down Expand Up @@ -1243,6 +1242,21 @@ def download_person_avatar(person, file_path, client=default):
)


def upload_person_avatar(person, file_path, client=default):
"""
Upload given file as person avatar.
Args:
person (str / dict): The person dict or the person ID.
file_path (str): Path of the file to upload as avatar.
"""
path = (
"/pictures/thumbnails/persons/%s"
% normalize_model_parameter(person)["id"]
)
return raw.upload(path, file_path, client=client)


def download_project_avatar(project, file_path, client=default):
"""
Download given project's avatar and save it at given location.
Expand All @@ -1258,6 +1272,21 @@ def download_project_avatar(project, file_path, client=default):
)


def upload_project_avatar(project, file_path, client=default):
"""
Upload given file as project avatar.
Args:
project (str / dict): The project dict or the project ID.
file_path (str): Path of the file to upload as avatar.
"""
path = (
"/pictures/thumbnails/projects/%s"
% normalize_model_parameter(project)["id"]
)
return raw.upload(path, file_path, client=client)


def update_preview(preview_file, data, client=default):
"""
Update the data of given preview file.
Expand Down
42 changes: 41 additions & 1 deletion tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import gazu.client
import gazu.files

from utils import fakeid, mock_route
from utils import fakeid, mock_route, add_verify_file_callback


class FilesTestCase(unittest.TestCase):
Expand Down Expand Up @@ -1170,3 +1170,43 @@ def test_get_output_file_by_path(self):
self.assertEqual(
gazu.files.get_output_file_by_path("testpath"), text[0]
)

def test_upload_person_avatar(self):
with open("./tests/fixtures/v1.png", "rb") as test_file:
with requests_mock.Mocker() as mock:
mock_route(
mock,
"POST",
"pictures/thumbnails/persons/%s" % fakeid("person-1"),
text={"id": fakeid("person-1")},
)

add_verify_file_callback(mock, {"file": test_file.read()})

self.assertEqual(
gazu.files.upload_person_avatar(
fakeid("person-1"),
"./tests/fixtures/v1.png",
),
{"id": fakeid("person-1")},
)

def test_upload_project_avatar(self):
with open("./tests/fixtures/v1.png", "rb") as test_file:
with requests_mock.Mocker() as mock:
mock_route(
mock,
"POST",
"pictures/thumbnails/projects/%s" % fakeid("project-1"),
text={"id": fakeid("project-1")},
)

add_verify_file_callback(mock, {"file": test_file.read()})

self.assertEqual(
gazu.files.upload_project_avatar(
fakeid("project-1"),
"./tests/fixtures/v1.png",
),
{"id": fakeid("project-1")},
)

0 comments on commit 9d0db82

Please sign in to comment.