Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SG-37548 Add payload optimization on update method #363

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
43 changes: 28 additions & 15 deletions shotgun_api3/shotgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,31 @@ def _add_project_param(self, params, project_entity):
params["project"] = project_entity

return params

def _translate_update_params(
self, entity_type, entity_id, data, multi_entity_update_modes
):
global SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION

def optimize_value(field_value):
if SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION and isinstance(field_value, dict):
return {"type": field_value["type"], "id": field_value["id"]}
return field_value

def optimize_field(field_dict):
return {k: optimize_value(v) for k, v in field_dict.items()}

full_fields = self._dict_to_list(
data,
extra_data=self._dict_to_extra_data(
multi_entity_update_modes, "multi_entity_update_mode"
),
)
return {
"type": entity_type,
"id": entity_id,
"fields": [optimize_field(field_dict) for field_dict in full_fields],
}

def summarize(self,
entity_type,
Expand Down Expand Up @@ -1463,14 +1488,7 @@ def update(self, entity_type, entity_id, data, multi_entity_update_modes=None):
upload_filmstrip_image = data.pop("filmstrip_image")

if data:
params = {
"type": entity_type,
"id": entity_id,
"fields": self._dict_to_list(
data,
extra_data=self._dict_to_extra_data(
multi_entity_update_modes, "multi_entity_update_mode"))
}
params = self._translate_update_params(entity_type, entity_id, data, multi_entity_update_modes)
record = self._call_rpc("update", params)
result = self._parse_records(record)[0]
else:
Expand Down Expand Up @@ -4482,16 +4500,11 @@ def _translate_filters_simple(sg_filter):
if (
SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION
and condition["path"] != "id"
and condition["relation"] in ["is", "is_not"]
and condition["relation"] in ["is", "is_not", "in", "not_in"]
and isinstance(values[0], dict)
):
try:
values = [
{
"type": values[0]["type"],
"id": values[0]["id"],
}
]
values = [{"type": v["type"], "id": v["id"]} for v in values]
except KeyError:
pass

Expand Down
79 changes: 77 additions & 2 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import os
import unittest
from unittest import mock
from .mock import patch
import shotgun_api3 as api
from shotgun_api3.shotgun import _is_mimetypes_broken
Expand Down Expand Up @@ -434,7 +435,8 @@ def test_related_object(self):
result = api.shotgun._translate_filters(filters, "all")
self.assertEqual(result, expected)

def test_related_object_entity_optimization(self):
@mock.patch.dict(os.environ, {"SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION": "1"})
def test_related_object_entity_optimization_is(self):
filters = [
[
"project",
Expand All @@ -457,11 +459,84 @@ def test_related_object_entity_optimization(self):
}
],
}
os.environ["SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION"] = "1"
api.Shotgun("http://server_path", "script_name", "api_key", connect=False)
result = api.shotgun._translate_filters(filters, "all")
self.assertEqual(result, expected)

@mock.patch.dict(os.environ, {"SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION": "1"})
def test_related_object_entity_optimization_in(self):
filters = [
[
"project",
"in",
[
{"foo1": "foo1", "bar1": "bar1", "id": 999, "baz1": "baz1", "type": "Anything"},
{"foo2": "foo2", "bar2": "bar2", "id": 998, "baz2": "baz2", "type": "Anything"}
],
],
]
expected = {
"logical_operator": "and",
"conditions": [
{
"path": "project",
"relation": "in",
"values": [
{
"id": 999,
"type": "Anything",
},
{
"id": 998,
"type": "Anything",
}
],
}
],
}
api.Shotgun("http://server_path", "script_name", "api_key", connect=False)
result = api.shotgun._translate_filters(filters, "all")
self.assertEqual(result, expected)

@mock.patch.dict(os.environ, {"SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION": "1"})
def test_related_object_update_entity(self):
entity_type = "Anything"
entity_id = 999
multi_entity_update_modes = {"project": "set", "name": "set"}
data = {
"name": "test",
"project": {
"foo1": "foo1",
"bar1": "bar1",
"id": 999,
"baz1": "baz1",
"type": "Anything",
},
}
expected = {
"id": 999,
"type": "Anything",
"fields": [
{
"field_name": "name",
"value": "test",
"multi_entity_update_mode": "set",
},
{
"field_name": "project",
"multi_entity_update_mode": "set",
"value": {
# Entity is optimized with type/id fields.
"id": 999,
"type": "Anything",
},
},
],
}
sg = api.Shotgun("http://server_path", "script_name", "api_key", connect=False)
result = sg._translate_update_params(entity_type, entity_id, data, multi_entity_update_modes)
self.assertEqual(result, expected)


class TestCerts(unittest.TestCase):
# A dummy bad url provided by Amazon
Expand Down
Loading