Skip to content

Commit 55b6303

Browse files
authored
Merge pull request #94 from opengisch/QF-7324-add-project-thumbnail-upload-command
feat: add upload_project_thumbnail command to sdk and cli
2 parents 52e7c35 + 8e4501a commit 55b6303

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

qfieldcloud_sdk/cli.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,26 @@ def delete_project(ctx: Context, project_id):
365365
log(f'Deleted project "{project_id}".')
366366

367367

368+
@cli.command()
369+
@click.argument("project_id")
370+
@click.argument(
371+
"local_filename",
372+
type=click.Path(exists=True, file_okay=True, dir_okay=False, readable=True),
373+
)
374+
@click.pass_context
375+
def upload_project_thumbnail(
376+
ctx: Context, project_id: str, local_filename: str
377+
) -> None:
378+
"""Upload a project thumbnail."""
379+
380+
response = ctx.obj["client"].upload_project_thumbnail(project_id, local_filename)
381+
382+
if ctx.obj["format_json"]:
383+
print_json(response)
384+
else:
385+
log(f'Uploaded thumbnail "{local_filename}" to project "{project_id}".')
386+
387+
368388
@cli.command()
369389
@click.argument("project_id")
370390
@click.argument("project_path")

qfieldcloud_sdk/sdk.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,41 @@ def patch_project(
603603

604604
return resp.json()
605605

606+
def upload_project_thumbnail(
607+
self,
608+
project_id: str,
609+
local_filename: str,
610+
) -> requests.Response:
611+
"""Upload a project thumbnail.
612+
613+
Args:
614+
project_id: Project ID.
615+
local_filename: Path to the thumbnail image file.
616+
617+
Raises:
618+
pathvalidate.ValidationError: Raised when the uploaded file does not have a valid filename.
619+
620+
Returns:
621+
The response object from the upload request.
622+
623+
Example:
624+
```python
625+
client.upload_project_thumbnail(
626+
project_id="123e4567-e89b-12d3-a456-426614174000",
627+
local_filename="thumbnail.png"
628+
)
629+
```
630+
"""
631+
# if the filepath is invalid, it will throw a new error `pathvalidate.ValidationError`
632+
is_valid_filepath(local_filename)
633+
634+
with open(local_filename, "rb") as thumbnail:
635+
return self._request(
636+
"POST",
637+
f"projects/{project_id}/thumbnail",
638+
files={"thumbnail": thumbnail},
639+
)
640+
606641
def upload_files(
607642
self,
608643
project_id: str,

0 commit comments

Comments
 (0)