Пример #1
0
    def post(self, *args, **kwargs):
        if is_json_request(self.request):
            body = json.loads(self.request.body)
            body = body.get('data', None)
        else:
            body = self.get_argument('data', None)
            body = escape.json_decode(body) if body else {}
            # self.send_error(reason="仅支持Content-type:application/json")
            # return

        db = self.settings['mongo_client'][self.db]
        for row in body:
            id = row.get('id', None)
            if row.get('_state', None) == 'removed':
                if self.is_object_id(id):
                    yield db[self.cname].remove({"_id": ObjectId(id)})

            if id and self.is_object_id(id):  # update
                row['update_time'] = format_datetime(datetime.now())
                row['update_user'] = self.current_user.get('userid', '')
                yield db[self.cname].update({"_id": ObjectId(id) if self.is_object_id(id) else id}, {
                    "$set": clone_dict(row, without=[])
                })

            else:
                obj = clone_dict(row)
                obj['id'] = ObjectId()
                obj['_id'] = obj['id']

                obj['create_time'] = format_datetime(datetime.now())
                obj['create_user'] = self.current_user.get('userid', '')
                yield db[self.cname].insert(obj)
        # self.write(generate_response(message="保存成功"))
        self.send_message("保存成功")
Пример #2
0
def to_list(tree,parentid,children_field,id_field,parent_id_field,current_user):
    """
    将树形结构的json数组处理成一个常规数组
    :param tree
    :param parentid
    :param children_field
    :param id_field
    :param parent_id_field
    :return []
    """
    result = []
    for node in tree:
        item = clone_dict(node,without=['children'])
        item[parent_id_field] = parentid
        id=item.get('id',None)
        if not id:
            item['id']=ObjectId()
            item['_id']=item['id']
            item['create_user']=current_user
            item['create_time']=datetime.datetime.now()
        else:
            item['id']=ObjectId(id)
            item['_id']=item['id']
            item['update_user']=current_user
            item['update_time']=datetime.datetime.now()
        result.append(item)
        childrenes = node.get(children_field)
        if childrenes:
            id = item['id'] or ""
            result.extend(to_list(childrenes, id, children_field, id_field, parent_id_field,current_user))
    return result
Пример #3
0
    def post(self, *args, **kwargs):
        body = escape.json_decode(self.request.body)
        role_id = body.get("role_id", None)
        users = body.get("users", [])
        if role_id and users:
            db = self.settings["db"]
            role = yield db.roles.find_one({"_id": ObjectId(role_id)})
            for item in users:
                if item["_state"] == "removed":
                    yield db.users.update(
                        {"_id": ObjectId(item["id"])},
                        {
                            "$pull": {"roles": role["code"]},
                            "$set": {
                                "update_time": format_datetime(datetime.now()),
                                "update_user": self.current_user.get("username", ""),
                            },
                        },
                    )
                elif item["_state"] == "added" and not item.get("id", None):
                    user = clone_dict(item)
                    if hasattr(user, "roles"):
                        user["roles"].append(role["code"])
                    else:
                        user["roles"] = [role["code"]]
                    user["id"] = ObjectId()
                    user["_id"] = user["id"]

                    user["create_time"] = format_datetime(datetime.now())
                    user["create_user"] = self.current_user.get("username", "")
                    yield db.users.insert(user)
                else:
                    user = clone_dict(item)
                    user["update_time"] = format_datetime(datetime.now())
                    user["update_user"] = self.current_user.get("username", "")
                    # if user.get('roles',None):
                    user["roles"].append(role["code"])
                    # yield db.users.update({'_id': ObjectId(item['id'])}, {"$push":{"roles":role['code']}})
                    yield db.users.update({"_id": ObjectId(item["id"])}, {"$set": user})
Пример #4
0
    def put(self, *args, **kwargs):
        """接受用户的请求对文档进行更新
        :param args url路径的参数 """
        if is_json_request(self.request):
            body = json.loads(self.request.body)
        else:
            self.send_error(reason="仅支持Content-type:application/json")

        db = self.settings['mongo_client'][self.db]
        id = body.get('_id', None)
        if id :  # update
            body['update_time'] = format_datetime(datetime.now())
            body['update_user'] = self.current_user.get('userid', '')

            yield db[self.cname].update({"_id": ObjectId(id) if self.is_object_id(id) else id}, {
                "$set": clone_dict(body)
            })
        self.send_message("保存成功")