Skip to content

Commit 6a6e973

Browse files
author
Alex
committed
Implement schema_field_create/update/delete
We implement the create and update using a new internal helper called `_set_property`. This new method will recursively search the passed current property dictionary and attempt to find all keys matching the key you pass in and update it's value to the value you have passed in. If it cannot find the value in the current dictionary but it does find a "properties" dictionary, it will add a new entry in the "properties" dictionary. This "properties" dictionary appears to only exist on the root of the properties dictionary. A `schema_field_update` does a deep copy of the properties before updating them whereas the `schema_field_create` method starts from a base schema_field dictionary and updates it with what you pass in. This does not deal with setting "editable" values in each of these keys. We are assuming, in Mockgun, that you are allowed to change anything in the schema. Perhaps someone from the team at Shotgun will have some clear logic that will allow us to implement that.
1 parent 0d306b9 commit 6a6e973

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

shotgun_api3/lib/mockgun/mockgun.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
115115
"""
116116

117+
import copy
117118
import datetime
118119

119120
from ... import ShotgunError
@@ -227,13 +228,48 @@ def schema_read(self):
227228
return self._schema
228229

229230
def schema_field_create(self, entity_type, data_type, display_name, properties=None):
230-
raise NotImplementedError
231+
_properties = {
232+
"data_type": {"editable": True, "value": None},
233+
"description": {"editable": True, "value": ""},
234+
"editable": {"editable": True, "value": True},
235+
"entity_type": {"editable": True, "value": None},
236+
"mandatory": {"editable": True, "value": False},
237+
"name": {"editable": True, "value": None},
238+
"properties": {
239+
"default_value": {"editable": True, "value": None},
240+
"regex_validation": {"editable": True, "value": ""},
241+
"regex_validation_enabled": {"editable": True, "value": None},
242+
"summary_default": {"editable": True, "value": "none"}
243+
},
244+
"ui_value_displayable": {"editable": True, "value": True},
245+
"unique": {"editable": True, "value": True},
246+
"visible": {"editable": True, "value": True}
247+
}
248+
_properties["entity_type"]["value"] = entity_type
249+
_properties["entity_type"]["editable"] = False
250+
251+
_properties["data_type"]["value"] = data_type
252+
_properties["data_type"]["editable"] = False
253+
254+
_properties["name"]["value"] = display_name
255+
_properties["name"]["editable"] = False
256+
257+
if isinstance(properties, dict) and properties:
258+
for prop in properties:
259+
self._set_property(_properties, prop, properties[prop])
260+
261+
self._schema[entity_type][display_name] = _properties
231262

232263
def schema_field_update(self, entity_type, field_name, properties):
233-
raise NotImplementedError
264+
_properties = copy.deepcopy(self._schema[entity_type][field_name])
265+
for key in properties:
266+
self._set_property(_properties, key, properties[key])
267+
self._schema[entity_type][field_name] = _properties
234268

235269
def schema_field_delete(self, entity_type, field_name):
236-
raise NotImplementedError
270+
if field_name not in self._schema[entity_type]:
271+
return
272+
del self._schema[entity_type][field_name]
237273

238274
def schema_entity_read(self):
239275
return self._schema_entity
@@ -422,8 +458,29 @@ def upload(self, entity_type, entity_id, path, field_name=None, display_name=Non
422458
def upload_thumbnail(self, entity_type, entity_id, path, **kwargs):
423459
pass
424460

461+
def dump_schemas(self):
462+
schema_path, schema_entity_path = self.get_schema_paths()
463+
SchemaFactory.set_schemas(self.schema_read(), schema_path, self.schema_entity_read(), schema_entity_path)
464+
425465
###################################################################################################
426466
# internal methods and members
467+
@classmethod
468+
def _set_property(cls, property_data, property_key, property_value):
469+
result = False
470+
for key in property_data:
471+
if key == property_key:
472+
property_data[key]["value"] = property_value
473+
result = True
474+
break
475+
elif isinstance(property_data[key], dict):
476+
_result = cls._set_property(property_data[key], property_key, property_value)
477+
if _result is True:
478+
result = _result
479+
break
480+
if result is False and "properties" in property_data:
481+
property_data["properties"][property_key] = {"editable": True, "value": property_value}
482+
result = True
483+
return result
427484

428485
def _validate_entity_type(self, entity_type):
429486
if entity_type not in self._schema:

0 commit comments

Comments
 (0)