Skip to content

Commit 0259a4c

Browse files
authored
SG-10151: set field visibility via API (#209)
Allows to modify field visibility on a per-project basis with the API.
1 parent 0988a4a commit 0259a4c

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

shotgun_api3/shotgun.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,7 @@ def schema_field_create(self, entity_type, data_type, display_name, properties=N
20062006

20072007
return self._call_rpc("schema_field_create", params)
20082008

2009-
def schema_field_update(self, entity_type, field_name, properties):
2009+
def schema_field_update(self, entity_type, field_name, properties, project_entity=None):
20102010
"""
20112011
Update the properties for the specified field on an entity.
20122012
@@ -2024,7 +2024,16 @@ def schema_field_update(self, entity_type, field_name, properties):
20242024
:param field_name: Internal Shotgun name of the field to update.
20252025
:param properties: Dictionary with key/value pairs where the key is the property to be
20262026
updated and the value is the new value.
2027+
:param dict project_entity: Optional Project entity specifying which project to modify the
2028+
``visible`` property for. If the ``visible`` is present in ``properties`` and
2029+
``project_entity`` is not set, an exception will be raised. Example:
2030+
``{'type': 'Project', 'id': 3}``
20272031
:returns: ``True`` if the field was updated.
2032+
2033+
.. note::
2034+
The ``project_entity`` parameter can only affect the state of the ``visible`` property
2035+
and has no impact on other properties.
2036+
20282037
:rtype: bool
20292038
"""
20302039

@@ -2036,7 +2045,7 @@ def schema_field_update(self, entity_type, field_name, properties):
20362045
for k, v in six.iteritems((properties or {}))
20372046
]
20382047
}
2039-
2048+
params = self._add_project_param(params, project_entity)
20402049
return self._call_rpc("schema_field_update", params)
20412050

20422051
def schema_field_delete(self, entity_type, field_name):

tests/test_api.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,72 @@ def test_multiple_latest_filters(self):
26782678
self.sg.find,
26792679
"Version", filters, fields=fields, additional_filter_presets=additional_filters)
26802680

2681+
def test_modify_visibility(self):
2682+
"""
2683+
Ensure the visibility of a field can be edited via the API.
2684+
"""
2685+
# If the version of Shotgun is too old, do not run this test.
2686+
# TODO: Update this with the real version number once the feature is released.
2687+
if self.sg_version < (8, 5, 0):
2688+
warnings.warn("Test bypassed because SG server used does not support this feature.", FutureWarning)
2689+
return
2690+
2691+
field_display_name = "Project Visibility Test"
2692+
field_name = "sg_{0}".format(field_display_name.lower().replace(" ", "_"))
2693+
2694+
schema = self.sg.schema_field_read("Asset")
2695+
# Ensure the custom field exists.
2696+
if field_name not in schema:
2697+
self.sg.schema_field_create("Asset", "text", "Project Visibility Test")
2698+
2699+
# Grab any two projects that we can use for toggling the visible property with.
2700+
projects = self.sg.find("Project", [], order=[{"field_name": "id", "direction": "asc"}])
2701+
project_1 = projects[0]
2702+
project_2 = projects[1]
2703+
2704+
# First, reset the field visibility in a known state, i.e. visible for both projects,
2705+
# in case the last test run failed midway through.
2706+
self.sg.schema_field_update("Asset", field_name, {"visible": True}, project_1)
2707+
self.assertEqual(
2708+
{"value": True, "editable": True},
2709+
self.sg.schema_field_read("Asset", field_name, project_1)[field_name]["visible"]
2710+
)
2711+
self.sg.schema_field_update("Asset", field_name, {"visible": True}, project_2)
2712+
self.assertEqual(
2713+
{"value": True, "editable": True},
2714+
self.sg.schema_field_read("Asset", field_name, project_2)[field_name]["visible"]
2715+
)
2716+
2717+
# Built-in fields should remain not editable.
2718+
self.assertFalse(self.sg.schema_field_read("Asset", "code")["code"]["visible"]["editable"])
2719+
2720+
# Custom fields should be editable
2721+
self.assertEqual(
2722+
{"value": True, "editable": True},
2723+
self.sg.schema_field_read("Asset", field_name)[field_name]["visible"]
2724+
)
2725+
2726+
# Hide the field on project 1
2727+
self.sg.schema_field_update("Asset", field_name, {"visible": False}, project_1)
2728+
# It should not be visible anymore.
2729+
self.assertEqual(
2730+
{"value": False, "editable": True},
2731+
self.sg.schema_field_read("Asset", field_name, project_1)[field_name]["visible"]
2732+
)
2733+
2734+
# The field should be visible on the second project.
2735+
self.assertEqual(
2736+
{"value": True, "editable": True},
2737+
self.sg.schema_field_read("Asset", field_name, project_2)[field_name]["visible"]
2738+
)
2739+
2740+
# Restore the visibility on the field.
2741+
self.sg.schema_field_update("Asset", field_name, {"visible": True}, project_1)
2742+
self.assertEqual(
2743+
{"value": True, "editable": True},
2744+
self.sg.schema_field_read("Asset", field_name, project_1)[field_name]["visible"]
2745+
)
2746+
26812747

26822748
def _has_unicode(data):
26832749
for k, v in data.items():

0 commit comments

Comments
 (0)