Skip to content

Commit 2e1cd50

Browse files
SG-34551 validation for python3, so display_name now is only decoded … (#337)
* SG-34551 Old code for python2 was deleted for upload_sg, so the fix was covered with this deletion
1 parent b61ed09 commit 2e1cd50

File tree

3 files changed

+64
-21
lines changed

3 files changed

+64
-21
lines changed

shotgun_api3/shotgun.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,30 +2569,12 @@ def _upload_to_sg(self, entity_type, entity_id, path, field_name, display_name,
25692569

25702570
params.update(self._auth_params())
25712571

2572-
# If we ended up with a unicode string path, we need to encode it
2573-
# as a utf-8 string. If we don't, there's a chance that there will
2574-
# will be an attempt later on to encode it as an ascii string, and
2575-
# that will fail ungracefully if the path contains any non-ascii
2576-
# characters.
2577-
#
2578-
# On Windows, if the path contains non-ascii characters, the calls
2579-
# to open later in this method will fail to find the file if given
2580-
# a non-ascii-encoded string path. In that case, we're going to have
2581-
# to call open on the unicode path, but we'll use the encoded string
2582-
# for everything else.
2583-
path_to_open = path
2584-
if isinstance(path, six.text_type):
2585-
path = path.encode("utf-8")
2586-
if sys.platform != "win32":
2587-
path_to_open = path
2588-
25892572
if is_thumbnail:
25902573
url = urllib.parse.urlunparse((self.config.scheme, self.config.server,
25912574
"/upload/publish_thumbnail", None, None, None))
2592-
params["thumb_image"] = open(path_to_open, "rb")
2575+
params["thumb_image"] = open(path, "rb")
25932576
if field_name == "filmstrip_thumb_image" or field_name == "filmstrip_image":
25942577
params["filmstrip"] = True
2595-
25962578
else:
25972579
url = urllib.parse.urlunparse((self.config.scheme, self.config.server,
25982580
"/upload/upload_file", None, None, None))
@@ -2606,7 +2588,7 @@ def _upload_to_sg(self, entity_type, entity_id, path, field_name, display_name,
26062588
if tag_list:
26072589
params["tag_list"] = tag_list
26082590

2609-
params["file"] = open(path_to_open, "rb")
2591+
params["file"] = open(path, "rb")
26102592

26112593
result = self._send_form(url, params)
26122594

File renamed without changes.

tests/test_api.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def test_upload_download(self):
243243
# test upload of non-ascii, unicode path
244244
u_path = os.path.abspath(
245245
os.path.expanduser(
246-
glob.glob(os.path.join(six.text_type(this_dir), u'No*l.jpg'))[0]
246+
glob.glob(os.path.join(six.text_type(this_dir), "Noëlご.jpg"))[0]
247247
)
248248
)
249249

@@ -310,6 +310,67 @@ def test_upload_download(self):
310310
# cleanup
311311
os.remove(file_path)
312312

313+
@patch('shotgun_api3.Shotgun._send_form')
314+
def test_upload_to_sg(self, mock_send_form):
315+
"""
316+
Upload an attachment tests for _upload_to_sg()
317+
"""
318+
if "localhost" in self.server_url:
319+
self.skipTest("upload / down tests skipped for localhost")
320+
321+
self.sg.server_info["s3_direct_uploads_enabled"] = False
322+
mock_send_form.method.assert_called_once()
323+
mock_send_form.return_value = "1\n:123\nasd"
324+
this_dir, _ = os.path.split(__file__)
325+
u_path = os.path.abspath(
326+
os.path.expanduser(
327+
glob.glob(os.path.join(six.text_type(this_dir), "Noëlご.jpg"))[0]
328+
)
329+
)
330+
upload_id = self.sg.upload(
331+
"Ticket",
332+
self.ticket['id'],
333+
u_path,
334+
'attachments',
335+
tag_list="monkeys, everywhere, send, help"
336+
)
337+
mock_send_form_args, _ = mock_send_form.call_args
338+
display_name_to_send = mock_send_form_args[1].get("display_name", "")
339+
self.assertTrue(isinstance(upload_id, int))
340+
self.assertFalse(
341+
display_name_to_send.startswith("b'") and
342+
display_name_to_send.endswith("'")
343+
)
344+
345+
upload_id = self.sg.upload(
346+
"Ticket",
347+
self.ticket['id'],
348+
u_path,
349+
'filmstrip_image',
350+
tag_list="monkeys, everywhere, send, help",
351+
)
352+
self.assertTrue(isinstance(upload_id, int))
353+
mock_send_form_args, _ = mock_send_form.call_args
354+
display_name_to_send = mock_send_form_args[1].get("display_name", "")
355+
self.assertTrue(isinstance(upload_id, int))
356+
self.assertFalse(
357+
display_name_to_send.startswith("b'") and
358+
display_name_to_send.endswith("'")
359+
)
360+
361+
mock_send_form.method.assert_called_once()
362+
mock_send_form.return_value = "2\nIt can't be upload"
363+
self.assertRaises(
364+
shotgun_api3.ShotgunError,
365+
self.sg.upload,
366+
"Ticket",
367+
self.ticket['id'],
368+
u_path,
369+
'attachments',
370+
tag_list="monkeys, everywhere, send, help"
371+
)
372+
self.sg.server_info["s3_direct_uploads_enabled"] = True
373+
313374
def test_upload_thumbnail_in_create(self):
314375
"""Upload a thumbnail via the create method"""
315376
this_dir, _ = os.path.split(__file__)

0 commit comments

Comments
 (0)