Skip to content
Open
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
19 changes: 19 additions & 0 deletions shotgun_api3/shotgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -2610,6 +2610,7 @@ def upload(
field_name: Optional[str] = None,
display_name: Optional[str] = None,
tag_list: Optional[str] = None,
created_at: Optional[datetime.datetime] = None,
) -> int:
"""
Upload a file to the specified entity.
Expand All @@ -2634,6 +2635,8 @@ def upload(
This field must be a File/Link field type.
:param str display_name: The display name to use for the file. Defaults to the file name.
:param str tag_list: comma-separated string of tags to assign to the file.
:param datetime.datetime created_at: Optional datetime value to set as the created_at
time for the Attachment entity. If ``None``, the server will use the current time.
:returns: Id of the Attachment entity that was created for the image.
:rtype: int
:raises: :class:`ShotgunError` on upload failure.
Expand Down Expand Up @@ -2662,6 +2665,13 @@ def upload(
if os.path.getsize(path) == 0:
raise ShotgunError("Path cannot be an empty file: '%s'" % path)

if created_at is not None:
if not isinstance(created_at, datetime.datetime):
raise ShotgunError(
"created_at must be a datetime.datetime instance, got %s"
% type(created_at)
)

is_thumbnail = field_name in [
"thumb_image",
"filmstrip_thumb_image",
Expand All @@ -2679,6 +2689,7 @@ def upload(
display_name,
tag_list,
is_thumbnail,
created_at,
)
else:
return self._upload_to_sg(
Expand All @@ -2689,6 +2700,7 @@ def upload(
display_name,
tag_list,
is_thumbnail,
created_at,
)

def _upload_to_storage(
Expand All @@ -2700,6 +2712,7 @@ def _upload_to_storage(
display_name: Optional[str],
tag_list: Optional[str],
is_thumbnail: bool,
created_at: Optional[datetime.datetime] = None,
) -> int:
"""
Internal function to upload a file to the Cloud storage and link it to the specified entity.
Expand All @@ -2712,6 +2725,7 @@ def _upload_to_storage(
:param str display_name: The display name to use for the file. Defaults to the file name.
:param str tag_list: comma-separated string of tags to assign to the file.
:param bool is_thumbnail: indicates if the attachment is a thumbnail.
:param datetime created_at: The datetime to set for the attachment.
:returns: Id of the Attachment entity that was created for the image.
:rtype: int
"""
Expand Down Expand Up @@ -2768,6 +2782,8 @@ def _upload_to_storage(
# None gets converted to a string and added as a tag...
if tag_list:
params["tag_list"] = tag_list
if created_at is not None:
params["created_at"] = created_at

result = self._send_form(url, params)
if not result.startswith("1"):
Expand All @@ -2790,6 +2806,7 @@ def _upload_to_sg(
display_name: Optional[str],
tag_list: Optional[str],
is_thumbnail: bool,
created_at: Optional[datetime.datetime] = None,
) -> int:
"""
Internal function to upload a file to Shotgun and link it to the specified entity.
Expand Down Expand Up @@ -2848,6 +2865,8 @@ def _upload_to_sg(
# None gets converted to a string and added as a tag...
if tag_list:
params["tag_list"] = tag_list
if created_at is not None:
params["created_at"] = created_at

params["file"] = open(path, "rb")

Expand Down
Loading