Esempio n. 1
0
 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
Esempio n. 2
0
 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()
Esempio n. 3
0
 def object_from_json(self, object_type, object_json):
     """
     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
     ZenpyClass = self.class_for_type(object_type)
     obj = ZenpyClass(api=self.api)
     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)
         elif isinstance(value, list):
             value = ProxyList(value)
         setattr(obj, key, value)
     if hasattr(obj, '_clean_dirty'):
         obj._clean_dirty()
     add_to_cache(obj)
     return obj
Esempio n. 4
0
    def object_from_json(self, object_type, object_json):
        """
        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
        ZenpyClass = self.class_for_type(object_type)
        obj = ZenpyClass(api=self.api)
        for key, value in object_json.items():
            if isinstance(value, dict):
                key = self.format_key(key, parent=obj)
                if key in self.class_mapping:
                    value = self.object_from_json(key, value)
                elif as_singular(key) in self.class_mapping:
                    value = self.object_from_json(as_singular(key), value)
            elif isinstance(value, list) and self.format_key(
                    as_singular(key), parent=obj) in self.class_mapping:
                zenpy_objects = list()
                for item in value:
                    zenpy_objects.append(
                        self.object_from_json(
                            self.format_key(as_singular(key), parent=obj),
                            item))
                value = zenpy_objects

            if isinstance(obj, dict):
                value = ProxyDict(obj)
            elif isinstance(value, list):
                value = ProxyList(value)
            setattr(obj, key, value)

        if hasattr(obj, '_clean_dirty'):
            obj._clean_dirty()
        add_to_cache(obj)
        return obj