Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[files] add functions to upload avatar for projects and persons #319

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")},
)
Loading