From 61d92832f96a5815ea4d16cf144df3eacf5ca996 Mon Sep 17 00:00:00 2001 From: Brandon Ashworth Date: Thu, 31 May 2018 10:40:07 -0700 Subject: [PATCH 1/3] For SG-4335 - Added support for querying a subset of site preferences. --- shotgun_api3/shotgun.py | 21 ++++++++++++++++ tests/test_api.py | 56 ++++++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index a3e8c1d2..6509673f 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -3031,6 +3031,27 @@ def get_session_token(self): return session_token + def preferences_read(self, prefs=None): + """ + Get a subset of the site preferences. + + >>> sg.preferences_read() + { + "pref_name": "pref value" + } + :param list prefs: An optional list of preference names to return. + :returns: Dictionary of preferences and their values. + :rtype: dict + """ + if self.server_caps.version and self.server_caps.version < (7, 10, 0): + raise ShotgunError("preferences_read requires server version 7.10.0 or "\ + "higher, server is %s" % (self.server_caps.version,)) + + if not prefs: + prefs = [] + + return self._call_rpc("preferences_read", { "prefs": prefs }) + def _build_opener(self, handler): """ Build urllib2 opener with appropriate proxy handler. diff --git a/tests/test_api.py b/tests/test_api.py index e83674d4..82192022 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -650,6 +650,44 @@ def test_work_schedule(self): work_schedule['2012-01-04'] = {"reason": "USER_EXCEPTION", "working": False, "description": "Artist Holiday"} self.assertEqual(work_schedule, resp) + def test_preferences_read(self): + # all prefs + resp = self.sg.preferences_read() + + expected = { + 'date_component_order': 'month_day', + 'format_currency_fields_decimal_options': '$1,000.99', + 'format_currency_fields_display_dollar_sign': False, + 'format_currency_fields_negative_options': '- $1,000', + 'format_date_fields': '08/04/22 OR 04/08/22 (depending on the Month order preference)', + 'format_float_fields': '9,999.99', + 'format_float_fields_rounding': '9.999999', + 'format_footage_fields': '10-05', + 'format_number_fields': '1,000', + 'format_time_hour_fields': '12 hour', + 'support_local_storage': False + } + self.assertEqual(expected, resp) + + # all filtered + resp = self.sg.preferences_read(['date_component_order', 'support_local_storage']) + + expected = { + 'date_component_order': 'month_day', + 'support_local_storage': False + } + self.assertEqual(expected, resp) + + # all filtered with invalid pref + resp = self.sg.preferences_read(['date_component_order', 'support_local_storage', 'email_notifications']) + + expected = { + 'date_component_order': 'month_day', + 'support_local_storage': False + } + self.assertEqual(expected, resp) + + class TestDataTypes(base.LiveTestBase): '''Test fields representing the different data types mapped on the server side. @@ -1543,18 +1581,18 @@ def test_following(self): [["id","is",self.task["id"]]], ["project.Project.id"])["project.Project.id"] project_count = 2 if shot_project_id == task_project_id else 1 - result = self.sg.following(self.human_user, + result = self.sg.following(self.human_user, project={"type":"Project", "id":shot_project_id}) self.assertEqual( project_count, len(result) ) - result = self.sg.following(self.human_user, + result = self.sg.following(self.human_user, project={"type":"Project", "id":task_project_id}) self.assertEqual( project_count, len(result) ) - result = self.sg.following(self.human_user, - project={"type":"Project", "id":shot_project_id}, + result = self.sg.following(self.human_user, + project={"type":"Project", "id":shot_project_id}, entity_type="Shot") self.assertEqual( 1, len(result) ) - result = self.sg.following(self.human_user, - project={"type":"Project", "id":task_project_id}, + result = self.sg.following(self.human_user, + project={"type":"Project", "id":task_project_id}, entity_type="Task") self.assertEqual( 1, len(result) ) @@ -1701,7 +1739,7 @@ def test_upload_empty_file(self): path = os.path.abspath(os.path.expanduser(os.path.join(this_dir,"empty.txt"))) self.assertRaises(shotgun_api3.ShotgunError, self.sg.upload, 'Version', 123, path) self.assertRaises(shotgun_api3.ShotgunError, self.sg.upload_thumbnail, 'Version', 123, path) - self.assertRaises(shotgun_api3.ShotgunError, self.sg.upload_filmstrip_thumbnail, 'Version', + self.assertRaises(shotgun_api3.ShotgunError, self.sg.upload_filmstrip_thumbnail, 'Version', 123, path) def test_upload_missing_file(self): @@ -1711,7 +1749,7 @@ def test_upload_missing_file(self): path = "/path/to/nowhere/foo.txt" self.assertRaises(shotgun_api3.ShotgunError, self.sg.upload, 'Version', 123, path) self.assertRaises(shotgun_api3.ShotgunError, self.sg.upload_thumbnail, 'Version', 123, path) - self.assertRaises(shotgun_api3.ShotgunError, self.sg.upload_filmstrip_thumbnail, 'Version', + self.assertRaises(shotgun_api3.ShotgunError, self.sg.upload_filmstrip_thumbnail, 'Version', 123, path) # def test_malformed_response(self): @@ -2525,7 +2563,7 @@ def _has_unicode(data): def _get_path(url): """Returns path component of a url without the sheme, host, query, anchor, or any other - additional elements. + additional elements. For example, the url "https://foo.shotgunstudio.com/page/2128#Shot_1190_sr10101_034" returns "/page/2128" """ From 428e5a50f7a140aa84997e211c4be506f1ae9e55 Mon Sep 17 00:00:00 2001 From: Brandon Ashworth Date: Thu, 31 May 2018 10:54:57 -0700 Subject: [PATCH 2/3] For SG-4335 - Fixed test to only run on the correct versions of Shotgun. --- tests/test_api.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_api.py b/tests/test_api.py index 82192022..a5408412 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -651,6 +651,10 @@ def test_work_schedule(self): self.assertEqual(work_schedule, resp) def test_preferences_read(self): + # Only run the tests on a server with the feature. + if not self.sg.server_caps.version or self.sg.server_caps.version < (7, 10, 0): + return + # all prefs resp = self.sg.preferences_read() From f7db5712075a0894e92a247b74040c6458f6b892 Mon Sep 17 00:00:00 2001 From: Brandon Ashworth Date: Mon, 4 Jun 2018 15:38:59 -0700 Subject: [PATCH 3/3] Addressing code review note --- shotgun_api3/shotgun.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 6509673f..579227b2 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -3047,8 +3047,7 @@ def preferences_read(self, prefs=None): raise ShotgunError("preferences_read requires server version 7.10.0 or "\ "higher, server is %s" % (self.server_caps.version,)) - if not prefs: - prefs = [] + prefs = prefs or [] return self._call_rpc("preferences_read", { "prefs": prefs })