Ejemplo n.º 1
0
def decode(key, value):
    if isinstance(value, get_dumpable_types()):
        return value

    if isinstance(value, (tuple, list)):
        return [decode(key, x) for x in value]

    if not isinstance(value, dict):
        return value

    if '__type' not in value:
        return dict([(k, decode(k, v)) for k, v in iteritems(value)])

    _type = value['__type']

    if _type == 'Pointer':
        value = copy.deepcopy(value)
        class_name = value['className']
        pointer = leancloud.Object.create(class_name)
        if 'createdAt' in value:
            value.pop('__type')
            value.pop('className')
            pointer._update_data(value)
        else:
            pointer._update_data({'objectId': value['objectId']})
        return pointer

    if _type == 'Object':
        value = copy.deepcopy(value)
        class_name = value['className']
        value.pop('__type')
        value.pop('className')
        obj = leancloud.Object.create(class_name)
        obj._update_data(value)
        return obj

    if _type == 'Date':
        return arrow.get(iso8601.parse_date(value['iso'])).to('local').datetime

    if _type == 'GeoPoint':
        return leancloud.GeoPoint(latitude=value['latitude'], longitude=value['longitude'])

    if key == 'ACL':
        if isinstance(value, leancloud.ACL):
            return value
        return leancloud.ACL(value)

    if _type == 'Relation':
        relation = leancloud.Relation(None, key)
        relation.target_class_name = value['className']
        return relation

    if _type == 'File':
        f = leancloud.File(value['name'])
        meta_data = value.get('metaData')
        if meta_data:
            f._metadata = meta_data
        f._url = value['url']
        f.id = value['objectId']
        return f
Ejemplo n.º 2
0
 def _apply(self, old, obj=None, key=None):
     if old is None:
         return leancloud.Relation(obj, key)
     elif isinstance(old, leancloud.Relation):
         if self._target_class_name:
             if old.target_class_name:
                 if old.target_class_name != self._target_class_name:
                     raise TypeError(
                         "related object must be class of {0}".format(
                             old.target_class_name))
                 else:
                     old.target_class_name = self._target_class_name
             return old
         else:
             raise TypeError("invalid op")
Ejemplo n.º 3
0
    def relation(self, attr):
        """
        返回对象上相应字段的 Relation

        :param attr: 字段名
        :type attr: basestring
        :return: Relation
        :rtype: leancloud.Relation
        """
        value = self.get(attr)
        if value is not None:
            if not isinstance(value, leancloud.Relation):
                raise TypeError('field %s is not Relation'.format(attr))
            value._ensure_parent_and_key(self, attr)
            return value
        return leancloud.Relation(self, attr)
Ejemplo n.º 4
0
def decode(key, value):
    if isinstance(value, get_dumpable_types()):
        return value

    if isinstance(value, (tuple, list)):
        return [decode(key, x) for x in value]

    if not isinstance(value, dict):
        return value

    if key == "ACL":
        if isinstance(value, leancloud.ACL):
            return value
        return leancloud.ACL(value)

    if "__type" not in value:
        return dict([(k, decode(k, v)) for k, v in six.iteritems(value)])

    _type = value["__type"]

    if _type == "Pointer":
        value = copy.deepcopy(value)
        class_name = value["className"]
        pointer = leancloud.Object.create(class_name)
        if "createdAt" in value:
            value.pop("__type")
            value.pop("className")
            pointer._update_data(value)
        else:
            pointer._update_data({"objectId": value["objectId"]})
        return pointer

    if _type == "Object":
        value = copy.deepcopy(value)
        class_name = value["className"]
        value.pop("__type")
        value.pop("className")
        obj = leancloud.Object.create(class_name)
        obj._update_data(value)
        return obj

    if _type == "Date":
        return arrow.get(iso8601.parse_date(value["iso"])).to("local").datetime

    if _type == "GeoPoint":
        return leancloud.GeoPoint(
            latitude=value["latitude"], longitude=value["longitude"]
        )

    if _type == "Relation":
        relation = leancloud.Relation(None, key)
        relation.target_class_name = value["className"]
        return relation

    if _type == "File":
        f = leancloud.File(value["name"])
        meta_data = value.get("metaData")
        if meta_data:
            f._metadata = meta_data
        f._url = value["url"]
        f.id = value["objectId"]
        return f