How to use the zenpy.lib.proxy.ProxyList function in zenpy

To help you get started, we’ve selected a few zenpy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github facetoe / zenpy / tests / test_api / test_proxy.py View on Github external
def test_list_access_wrapped(self):
        self.proxy_list.append([])
        item = self.proxy_list[-1]
        self.assertIsInstance(item, ProxyList)
github facetoe / zenpy / tests / test_api / test_proxy.py View on Github external
def setUp(self):
        self.test_element = Comment()
        self.test_object = Ticket(comments=ProxyList([self.test_element]))
        self.attribute_name = 'comments'
        self.proxy_list = getattr(self.test_object, self.attribute_name)
        self.test_object._clean_dirty()
        self.proxy_list._clean_dirty()
github facetoe / zenpy / zenpy / lib / proxy.py View on Github external
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
github facetoe / zenpy / zenpy / lib / proxy.py View on Github external
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
github facetoe / zenpy / zenpy / lib / mapping.py View on Github external
"""
        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
github facetoe / zenpy / zenpy / lib / util.py View on Github external
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)