From e406d33eac1fbda91b5a97306132981f06dbb539 Mon Sep 17 00:00:00 2001 From: Philip Blyth Date: Thu, 12 Mar 2015 17:04:17 -0400 Subject: [PATCH 1/3] For #28441: refactored support checks to handle future additions --- shotgun_api3/shotgun.py | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 38515c22..a70bd66c 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -137,23 +137,35 @@ def __init__(self, host, meta): self._ensure_json_supported() - def _ensure_json_supported(self): - """Checks the server version supports the JSON api, raises an + def _ensure_support(self, feature): + """Checks the server version supports a given feature, raises an exception if it does not. - :raises ShotgunError: The current server version does not support json + :param feature: dict supported version and human label { 'version': (int, int, int), 'label': str } + + :raises ShotgunError: The current server version does not [feature] """ - if not self.version or self.version < (2, 4, 0): - raise ShotgunError("JSON API requires server version 2.4 or "\ - "higher, server is %s" % (self.version,)) + + if not self.version or self.version < feature['version']: + raise ShotgunError( + "%s requires server version %s or higher, "\ + "server is %s" % (feature['label'], _version_str(feature['version']), _version_str(self.version)) + ) + + + def _ensure_json_supported(self): + """Wrapper for ensure_support""" + self._ensure_support({ + 'version': (2, 4, 0), + 'label': 'JSON API' + }) def ensure_include_archived_projects(self): - """Checks the server version support include_archived_projects parameter - to find. - """ - if not self.version or self.version < (5, 3, 14): - raise ShotgunError("The include_archived_projects flag requires server version 5.3.14 or "\ - "higher, server is %s" % (self.version,)) + """Wrapper for ensure_support""" + self._ensure_support({ + 'version': (5, 3, 14), + 'label': 'include_archived_projects parameter' + }) def __str__(self): @@ -2208,4 +2220,6 @@ def _translate_filters_simple(sg_filter): return condition - +def _version_str(version): + """Converts a tuple of int's to a '.' separated str""" + return '.'.join(map(str, version)) From 5f590f671895b20a721fcc2c149621900c1cdc55 Mon Sep 17 00:00:00 2001 From: Philip Blyth Date: Thu, 19 Mar 2015 12:19:48 -0400 Subject: [PATCH 2/3] For #28441: added `raise_hell` to `_ensure_support()` --- shotgun_api3/shotgun.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index a70bd66c..3f8fbbba 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -137,7 +137,7 @@ def __init__(self, host, meta): self._ensure_json_supported() - def _ensure_support(self, feature): + def _ensure_support(self, feature, raise_hell=True): """Checks the server version supports a given feature, raises an exception if it does not. @@ -147,10 +147,14 @@ def _ensure_support(self, feature): """ if not self.version or self.version < feature['version']: - raise ShotgunError( - "%s requires server version %s or higher, "\ - "server is %s" % (feature['label'], _version_str(feature['version']), _version_str(self.version)) - ) + if raise_hell: + raise ShotgunError( + "%s requires server version %s or higher, "\ + "server is %s" % (feature['label'], _version_str(feature['version']), _version_str(self.version)) + ) + return False + else: + return True def _ensure_json_supported(self): From 7963a06652178cd10b1d25f4f2616ae70f61a181 Mon Sep 17 00:00:00 2001 From: Philip Blyth Date: Tue, 24 Mar 2015 19:07:31 -0400 Subject: [PATCH 3/3] For #28817: FIXED failing tests in older sites due to new features --- shotgun_api3/shotgun.py | 42 +++++++++++----------- tests/test_api.py | 78 ++++++++++++++++++++++------------------- tests/test_api_long.py | 23 ++++++------ 3 files changed, 77 insertions(+), 66 deletions(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 3f8fbbba..0be6cd3e 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -171,6 +171,13 @@ def ensure_include_archived_projects(self): 'label': 'include_archived_projects parameter' }) + def ensure_per_project_customization(self): + """Wrapper for ensure_support""" + return self._ensure_support({ + 'version': (5, 4, 4), + 'label': 'project parameter' + }, True) + def __str__(self): return "ServerCapabilities: host %s, version %s, is_dev %s"\ @@ -643,6 +650,15 @@ def _construct_read_parameters(self, params['sorts'] = sort_list return params + + def _add_project_param(self, params, project_entity): + + if project_entity and self.server_caps.ensure_per_project_customization(): + params["project"] = project_entity + + return params + + def summarize(self, entity_type, filters, @@ -1021,12 +1037,7 @@ def schema_entity_read(self, project_entity=None): params = {} - if project_entity: - if not self.server_caps.version or self.server_caps.version < (5, 4, 4): - raise ShotgunError("Per project schema operations require server "\ - "version 5.4.4 or higher, server is %s" % (self.server_caps.version,)) - else: - params["project"] = project_entity + params = self._add_project_param(params, project_entity) if params: return self._call_rpc("schema_entity_read", params) @@ -1045,13 +1056,8 @@ def schema_read(self, project_entity=None): params = {} - if project_entity: - if not self.server_caps.version or self.server_caps.version < (5, 4, 4): - raise ShotgunError("Per project schema operations require server "\ - "version 5.4.4 or higher, server is %s" % (self.server_caps.version,)) - else: - params["project"] = project_entity - + params = self._add_project_param(params, project_entity) + if params: return self._call_rpc("schema_read", params) else: @@ -1078,15 +1084,11 @@ def schema_field_read(self, entity_type, field_name=None, project_entity=None): params = { "type": entity_type, } + if field_name: params["field_name"] = field_name - - if project_entity: - if not self.server_caps.version or self.server_caps.version < (5, 4, 4): - raise ShotgunError("Per project schema operations require server "\ - "version 5.4.4 or higher, server is %s" % (self.server_caps.version,)) - else: - params["project"] = project_entity + + params = self._add_project_param(params, project_entity) return self._call_rpc("schema_field_read", params) diff --git a/tests/test_api.py b/tests/test_api.py index 935e67ff..f4a004c5 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1567,57 +1567,63 @@ class TestSessionTokenAuth(base.SessionTokenAuthLiveTestBase): """ Testing the session token based authentication method """ - + def test_humanuser_find(self): """Called find, find_one for known entities as session token based user""" - filters = [] - filters.append(['project', 'is', self.project]) - filters.append(['id', 'is', self.version['id']]) - fields = ['id'] + if self.sg.server_caps.version >= (5, 4, 1): - versions = self.sg.find("Version", filters, fields=fields) + filters = [] + filters.append(['project', 'is', self.project]) + filters.append(['id', 'is', self.version['id']]) - self.assertTrue(isinstance(versions, list)) - version = versions[0] - self.assertEqual("Version", version["type"]) - self.assertEqual(self.version['id'], version["id"]) + fields = ['id'] - version = self.sg.find_one("Version", filters, fields=fields) - self.assertEqual("Version", version["type"]) - self.assertEqual(self.version['id'], version["id"]) + versions = self.sg.find("Version", filters, fields=fields) + + self.assertTrue(isinstance(versions, list)) + version = versions[0] + self.assertEqual("Version", version["type"]) + self.assertEqual(self.version['id'], version["id"]) + + version = self.sg.find_one("Version", filters, fields=fields) + self.assertEqual("Version", version["type"]) + self.assertEqual(self.version['id'], version["id"]) def test_humanuser_upload_thumbnail_for_version(self): """simple upload thumbnail for version test as session based token user.""" - this_dir, _ = os.path.split(__file__) - path = os.path.abspath(os.path.expanduser( - os.path.join(this_dir,"sg_logo.jpg"))) - size = os.stat(path).st_size - # upload thumbnail - thumb_id = self.sg.upload_thumbnail("Version", - self.version['id'], path) - self.assertTrue(isinstance(thumb_id, int)) + if self.sg.server_caps.version >= (5, 4, 1): - # check result on version - version_with_thumbnail = self.sg.find_one('Version', - [['id', 'is', self.version['id']]], - fields=['image']) + this_dir, _ = os.path.split(__file__) + path = os.path.abspath(os.path.expanduser( + os.path.join(this_dir,"sg_logo.jpg"))) + size = os.stat(path).st_size - self.assertEqual(version_with_thumbnail.get('type'), 'Version') - self.assertEqual(version_with_thumbnail.get('id'), self.version['id']) + # upload thumbnail + thumb_id = self.sg.upload_thumbnail("Version", + self.version['id'], path) + self.assertTrue(isinstance(thumb_id, int)) + # check result on version + version_with_thumbnail = self.sg.find_one('Version', + [['id', 'is', self.version['id']]], + fields=['image']) - h = Http(".cache") - thumb_resp, content = h.request(version_with_thumbnail.get('image'), "GET") - self.assertEqual(thumb_resp['status'], '200') - self.assertEqual(thumb_resp['content-type'], 'image/jpeg') + self.assertEqual(version_with_thumbnail.get('type'), 'Version') + self.assertEqual(version_with_thumbnail.get('id'), self.version['id']) - # clear thumbnail - response_clear_thumbnail = self.sg.update("Version", - self.version['id'], {'image':None}) - expected_clear_thumbnail = {'id': self.version['id'], 'image': None, 'type': 'Version'} - self.assertEqual(expected_clear_thumbnail, response_clear_thumbnail) + + h = Http(".cache") + thumb_resp, content = h.request(version_with_thumbnail.get('image'), "GET") + self.assertEqual(thumb_resp['status'], '200') + self.assertEqual(thumb_resp['content-type'], 'image/jpeg') + + # clear thumbnail + response_clear_thumbnail = self.sg.update("Version", + self.version['id'], {'image':None}) + expected_clear_thumbnail = {'id': self.version['id'], 'image': None, 'type': 'Version'} + self.assertEqual(expected_clear_thumbnail, response_clear_thumbnail) class TestProjectLastAccessedByCurrentUser(base.LiveTestBase): diff --git a/tests/test_api_long.py b/tests/test_api_long.py index a437c317..a5fb21f8 100644 --- a/tests/test_api_long.py +++ b/tests/test_api_long.py @@ -105,35 +105,38 @@ def test_schema(self): self.assertTrue(ret_val) def test_schema_with_project(self): - """Called schema functions""" + """Called schema functions with project""" + + project_entity = {'type': 'Project', 'id': 0} if not self.sg.server_caps.version or self.sg.server_caps.version < (5, 4, 4): + # server does not support this! - self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_entity_read, {'type': 'Project', 'id': 0}) - self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_read, {'type': 'Project', 'id': 0}) - self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', None, {'type': 'Project', 'id': 0}) - self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', 'user', {'type': 'Project', 'id': 0}) - + self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_entity_read, project_entity) + self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_read, project_entity) + self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', None, project_entity) + self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', 'user', project_entity) + else: - project_entity = {'type': 'Project', 'id': 0} + schema = self.sg.schema_entity_read(project_entity) self.assertTrue(schema, dict) self.assertTrue(len(schema) > 0) self.assertTrue('Project' in schema) self.assertTrue('visible' in schema['Project']) - + schema = self.sg.schema_read(project_entity) self.assertTrue(schema, dict) self.assertTrue(len(schema) > 0) self.assertTrue('Version' in schema) self.assertFalse('visible' in schema.keys()) - + schema = self.sg.schema_field_read('Version', project_entity=project_entity) self.assertTrue(schema, dict) self.assertTrue(len(schema) > 0) self.assertTrue('user' in schema) self.assertTrue('visible' in schema['user']) - + schema = self.sg.schema_field_read('Version', 'user', project_entity) self.assertTrue(schema, dict) self.assertTrue(len(schema) > 0)