Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.test_object = Ticket(comments=ProxyDict(dict(comment=Comment(),
list=[1, 3, 4],
dict={1: 2, 3: 4})))
self.attribute_name = 'comments'
self.proxy_dict = getattr(self.test_object, self.attribute_name)
self.proxy_dict._clean_dirty()
self.test_object._clean_dirty()
def test_proxy_list_wraps_dict(self):
some_dict = self.proxy_dict['dict']
self.assertIsInstance(some_dict, ProxyDict)
def object_from_json(self, object_type, object_json, parent=None):
"""
Given a blob of JSON representing a Zenpy object, recursively deserialize it and
any nested objects it contains. This method also adds the deserialized object
to the relevant cache if applicable.
"""
if not isinstance(object_json, dict):
return object_json
obj = self.instantiate_object(object_type, parent)
for key, value in object_json.items():
if key not in self.skip_attrs:
key, value = self._deserialize(key, obj, value)
if isinstance(value, dict):
value = ProxyDict(value, dirty_callback=getattr(
obj, '_dirty_callback', None))
elif isinstance(value, list):
value = ProxyList(value, dirty_callback=getattr(
obj, '_dirty_callback', None))
setattr(obj, key, value)
if hasattr(obj, '_clean_dirty'):
obj._clean_dirty()
self.api.cache.add(obj)
return obj
def _wrap_element(self, element):
"""
We want to know if an item is modified that is stored in this list. If the element is a list or dict,
we wrap it in a ProxyList or ProxyDict, and if it is modified execute a callback that updates this
instance. If it is a ZenpyObject, then the callback updates the parent object.
"""
def dirty_callback():
self._set_dirty()
if isinstance(element, list):
element = ProxyList(element, dirty_callback=dirty_callback)
elif isinstance(element, dict):
element = ProxyDict(element, dirty_callback=dirty_callback)
# If it is a Zenpy object this will either return None or the previous wrapper.
elif getattr(element, '_dirty_callback', self._sentinel) is not self._sentinel:
# Don't set callback if already set.
if not callable(element._dirty_callback):
element._dirty_callback = dirty_callback
return element
def _wrap_element(self, element):
"""
We want to know if an item is modified that is stored in this dict. If the element is a list or dict,
we wrap it in a ProxyList or ProxyDict, and if it is modified execute a callback that updates this
instance. If it is a ZenpyObject, then the callback updates the parent object.
"""
def dirty_callback():
self._set_dirty()
if isinstance(element, list):
element = ProxyList(element, dirty_callback=dirty_callback)
elif isinstance(element, dict):
element = ProxyDict(element, dirty_callback=dirty_callback)
# If it is a Zenpy object this will either return None or the previous wrapper.
elif getattr(element, '_dirty_callback', self._sentinel) is not self._sentinel:
# Don't set callback if already set.
if not callable(element._dirty_callback):
element._dirty_callback = dirty_callback
return element
def json_encode(obj, serialize):
""" Handle encoding complex types. """
if hasattr(obj, 'to_dict'):
return obj.to_dict(serialize=serialize)
elif isinstance(obj, datetime):
return obj.date().isoformat()
elif isinstance(obj, date):
return obj.isoformat()
elif isinstance(obj, ProxyDict):
return dict(obj)
elif isinstance(obj, ProxyList):
return list(obj)
elif is_iterable_but_not_string(obj):
return list(obj)