Example #1
0
 def _update_data(self, server_data):
     self.id = server_data.pop('objectId')
     self.created_at = utils.decode('createdAt',
                                    server_data.pop('createdAt'))
     self.updated_at = utils.decode('updatedAt',
                                    server_data.pop('updatedAt'))
     self._data = utils.decode(None, server_data)
Example #2
0
def _rpc_in_local(_cloud_rpc_name, **params):
    if not leanengine.root_engine:
        return
    result = leanengine.dispatch_cloud_func(
        leanengine.root_engine.app.cloud_codes, {}, _cloud_rpc_name, True,
        params)
    return utils.decode(None, result)
Example #3
0
 def _finish_save(self, server_data):
     saved_changes = self._op_set_queue[0]
     self._op_set_queue = self._op_set_queue[1:]
     self._apply_op_set(saved_changes, self._server_data)
     self._merge_magic_field(server_data)
     for key, value in server_data.iteritems():
         self._server_data[key] = utils.decode(key, value)
     self._rebuild_attributes()
Example #4
0
 def _finish_fetch(self, server_data, existed):
     self._op_set_queue = [{}]
     self._merge_magic_field(server_data)
     for key, value in server_data.iteritems():
         self._server_data[key] = utils.decode(key, value)
     self._rebuild_attributes()
     self._op_set_queue = [{}]
     self._existed = existed
Example #5
0
def test_decode():
    p = utils.decode('test_key', {
        '__type': 'GeoPoint',
        'longitude': 0,
        'latitude': 0,
    })
    assert isinstance(p, GeoPoint)
    assert p.latitude == 0
    assert p.longitude == 0
Example #6
0
def run(_cloud_func_name, **params):
    """
    调用 LeanEngine 上的远程代码
    :param name: 需要调用的远程 Cloud Code 的名称
    :type name: string_types
    :param params: 调用参数
    :return: 调用结果
    """
    response = leancloud.client.post('/functions/{0}'.format(_cloud_func_name), params=params)
    content = response.json()
    return utils.decode(None, content)['result']
Example #7
0
def run(name, **params):
    """
    调用 LeanEngine 上的远程代码

    :param name: 需要调用的远程 Cloud Code 的名称
    :type name: basestring
    :param params: 调用参数
    :return: 调用结果
    """
    response = leancloud.client.post('/functions/{0}'.format(name), params=params)
    content = utils.response_to_json(response)
    return utils.decode(None, content)['result']
Example #8
0
    def _finish_fetch(self, server_data, has_data):
        self._op_set_queue = [{}]

        self._merge_magic_field(server_data)

        for key, value in server_data.iteritems():
            self._server_data[key] = utils.decode(key, value)

        self._rebuild_all_estimated_data()

        self._op_set_queue = [{}]

        self._has_data = has_data
Example #9
0
 def _merge_metadata(self, server_data):
     for key in ("objectId", "createdAt", "updatedAt"):
         if server_data.get(key) is None:
             continue
         if key == "objectId":
             self.id = server_data[key]
         else:
             if isinstance(server_data[key], six.string_types):
                 dt = utils.decode(key, {
                     "__type": "Date",
                     "iso": server_data[key]
                 })
             elif server_data[key]["__type"] == "Date":
                 dt = utils.decode(key, server_data[key])
             else:
                 raise TypeError("Invalid date type")
             server_data[key] = dt
             if key == "createdAt":
                 self.created_at = dt
             elif key == "updatedAt":
                 self.updated_at = dt
             else:
                 raise TypeError
Example #10
0
 def _merge_metadata(self, server_data):
     for key in ('objectId', 'createdAt', 'updatedAt'):
         if server_data.get(key) is None:
             continue
         if key == 'objectId':
             self.id = server_data[key]
         else:
             if isinstance(server_data[key], six.string_types):
                 dt = utils.decode(key, {
                     '__type': 'Date',
                     'iso': server_data[key]
                 })
             elif server_data[key]['__type'] == 'Date':
                 dt = utils.decode(key, server_data)
             else:
                 raise TypeError('Invalid date type')
             server_data[key] = dt
             if key == 'createdAt':
                 self.created_at = dt
             elif key == 'updatedAt':
                 self.updated_at = dt
             else:
                 raise TypeError
Example #11
0
    def send(self, query):
        current_user = User.get_current()
        if not current_user:
            raise ValueError('Please sign in an user')

        params = {
            'inboxType': self.inbox_type,
            'data': self._data,
            'query': query.dump(),
            'source': self._data.get('source') or current_user._to_pointer(),
        }
        params['query']['className'] = query._query_class._class_name

        content = client.post('/statuses', params=params).json()
        self.id = content['objectId']
        self.created_at = utils.decode('createdAt', content['createdAt'])
Example #12
0
    def send(self, query):
        current_user = User.get_current()
        if not current_user:
            raise ValueError('Please sign in an user')

        params = {
            'inboxType': self.inbox_type,
            'data': self._data,
            'query': query.dump(),
            'source': self._data.get('source') or current_user._to_pointer(),
        }
        params['query']['className'] = query._query_class._class_name

        content = client.post('/statuses', params=params).json()
        self.id = content['objectId']
        self.created_at = utils.decode('createdAt', content['createdAt'])
Example #13
0
    def send(self, query):
        current_user = User.get_current()
        if not current_user:
            raise ValueError("Please sign in an user")

        params = {
            "inboxType": self.inbox_type,
            "data": self._data,
            "query": query.dump(),
            "source": self._data.get("source") or current_user._to_pointer(),
        }
        params["query"]["className"] = query._query_class._class_name

        content = client.post("/statuses", params=params).json()
        self.id = content["objectId"]
        self.created_at = utils.decode("createdAt", content["createdAt"])
Example #14
0
def rpc(_cloud_rpc_name, **params):
    """
    调用 LeanEngine 上的远程代码
    与 cloud.run 类似,但是允许传入 leancloud.Object 作为参数,也允许传入 leancloud.Object 作为结果
    :param name: 需要调用的远程 Cloud Code 的名称
    :type name: basestring
    :param params: 调用参数
    :return: 调用结果
    """
    encoded_params = {}
    for key, value in params.items():
        if isinstance(params, leancloud.Object):
            encoded_params[key] = utils.encode(value._dump())
        else:
            encoded_params[key] = utils.encode(value)
    response = leancloud.client.post('/call/{}'.format(_cloud_rpc_name), params=encoded_params)
    content = response.json()
    return utils.decode(None, content['result'])
Example #15
0
    def set(self, key_or_attrs, value=None, unset=False):
        """
        在当前对象此字段上赋值

        :param key_or_attrs: 字段名,或者一个包含 字段名 / 值的 dict
        :type key_or_attrs: string_types or dict
        :param value: 字段值
        :param unset:
        :return: 当前对象,供链式调用
        """
        if isinstance(key_or_attrs, dict) and value is None:
            attrs = key_or_attrs
            keys = attrs.keys()
            for k in keys:
                if isinstance(attrs[k], LocalProxy):
                    attrs[k] = attrs[k]._get_current_object()
        else:
            key = key_or_attrs
            if isinstance(value, LocalProxy):
                value = value._get_current_object()
            attrs = {key: utils.decode(key, value)}

        if unset:
            for k in attrs.keys():
                attrs[k] = operation.Unset()

        self.validate(attrs)

        self._merge_metadata(attrs)

        keys = list(attrs.keys())
        for k in keys:
            v = attrs[k]
            # TODO: Relation

            if not isinstance(v, operation.BaseOp):
                v = operation.Set(v)

            self._attributes[k] = v._apply(self._attributes.get(k), self, k)
            if self._attributes[k] == operation._UNSET:
                del self._attributes[k]
            self._changes[k] = v._merge(self._changes.get(k))

        return self
Example #16
0
    def set(self, key_or_attrs, value=None, unset=False):
        """
        在当前对象此字段上赋值

        :param key_or_attrs: 字段名,或者一个包含 字段名 / 值的 dict
        :type key_or_attrs: basestring or dict
        :param value: 字段值
        :param unset:
        :return: 当前对象,供链式调用
        """
        if isinstance(key_or_attrs, dict) and value is None:
            attrs = key_or_attrs
            keys = attrs.keys()
            for k in keys:
                if isinstance(attrs[k], LocalProxy):
                    attrs[k] = attrs[k]._get_current_object()
        else:
            key = key_or_attrs
            if isinstance(value, LocalProxy):
                value = value._get_current_object()
            attrs = {key: utils.decode(key, value)}

        if unset:
            for k in attrs.keys():
                attrs[k] = operation.Unset()

        self.validate(attrs)

        self._merge_magic_field(attrs)

        keys = attrs.keys()
        for k in keys:
            v = attrs[k]
            # TODO: Relation

            if not isinstance(v, operation.BaseOp):
                v = operation.Set(v)

            current_changes = self._op_set_queue[-1]
            current_changes[k] = v._merge(current_changes.get(k))
            self._rebuild_attribute(k)

        return self
Example #17
0
def get_server_time():
    response = requests.get('https://leancloud.cn/1.1/date')
    content = json.loads(response.content)
    return utils.decode('iso', content)
Example #18
0
def _rpc_in_local(_cloud_rpc_name, **params):
    result = leancloud.engine.leanengine.dispatch_cloud_func(_cloud_rpc_name, True, params)
    return utils.decode(None, result)
Example #19
0
 def _update_data(self, server_data):
     self._merge_metadata(server_data)
     for key, value in six.iteritems(server_data):
         self._attributes[key] = utils.decode(key, value)
     self._changes = {}
Example #20
0
def get_server_time():
    response = requests.get(get_base_url() + '/date')
    content = json.loads(response.text)
    return utils.decode('iso', content)
Example #21
0
def get_server_time():
    response = requests.get(get_base_url() + '/date')
    content = json.loads(response.content)
    return utils.decode('iso', content)
def test_decode():  # type: () -> None
    p = utils.decode("test_key", {"__type": "GeoPoint", "longitude": 0, "latitude": 0})
    assert isinstance(p, GeoPoint)
    assert p.latitude == 0
    assert p.longitude == 0
Example #23
0
def get_server_time():
    response = check_error(session.get)(get_url('/date'),
                                        timeout=TIMEOUT_SECONDS)
    return utils.decode('iso', response.json())
Example #24
0
 def _update_data(self, server_data):
     self.id = server_data.pop("objectId")
     self.created_at = utils.decode("createdAt", server_data.pop("createdAt"))
     self.updated_at = utils.decode("updatedAt", server_data.pop("updatedAt"))
     self._data = utils.decode(None, server_data)
Example #25
0
def _rpc_in_local(_cloud_rpc_name, **params):
    result = leancloud.engine.leanengine.dispatch_cloud_func(_cloud_rpc_name, True, params)
    return utils.decode(None, result)
Example #26
0
def _rpc_in_local(_cloud_rpc_name, **params):
    if not leanengine.root_engine:
        return
    result = leanengine.dispatch_cloud_func(leanengine.root_engine.app.cloud_codes, {}, _cloud_rpc_name, True, params)
    return utils.decode(None, result)
Example #27
0
 def _update_data(self, server_data):
     self.id = server_data.pop('objectId')
     self.created_at = utils.decode('createdAt', server_data.pop('createdAt'))
     self.updated_at = utils.decode('updatedAt', server_data.pop('updatedAt'))
     self._data = utils.decode(None, server_data)