def get(self, *args, **kwargs):
        project_code = self.get_argument('project_code',
                                         default=None,
                                         strip=True)
        environment = self.get_argument('environment',
                                        default=None,
                                        strip=True)
        service = self.get_argument('service', default=None, strip=True)
        filename = self.get_argument('filename', default=None, strip=True)
        if not project_code or not environment or not service or not filename:
            return self.write(dict(code=-1, msg='关键参数不能为空'))

        the_pro_env_list, the_pro_per_dict = check_permissions(
            self.get_current_nickname())
        if not self.is_superuser and not environment == 'public':
            if not the_pro_per_dict.get(project_code):
                if "{}/{}".format(project_code,
                                  environment) not in the_pro_env_list:
                    return self.write(dict(code=-2, msg='没有权限'))

        with DBContext('r') as session:
            config_key = "/{}/{}/{}/{}".format(project_code, environment,
                                               service, filename)
            conf_info = session.query(KerriganPublish).filter(
                KerriganPublish.config == config_key).all()

        if not conf_info:
            return self.write(dict(code=-3, msg='没有数据'))

        new_data = {}
        for msg in conf_info:
            data_dict = model_to_dict(msg)
            new_data[data_dict['config']] = data_dict['content']

        self.write(dict(code=0, msg='获取成功', data=new_data))
    def get(self, *args, **kwargs):
        key = self.get_argument('key', default=None, strip=True)
        limit = self.get_argument('limit', default=50, strip=True)
        nickname = self.get_current_nickname()
        project_list = []
        the_project_list = []
        with DBContext('r') as session:
            if key:
                project_info = session.query(KerriganProject).filter(
                    or_(KerriganProject.project_name.like('%{}%'.format(key)),
                        KerriganProject.project_code.like(
                            '%{}%'.format(key)))).all()
            else:
                project_info = session.query(KerriganProject).limit(int(limit))

        the_pro_env_list, the_pro_per_dict = check_permissions(nickname)
        for p in the_pro_env_list:
            the_project_list.append(p.split('/')[0])

        for msg in project_info:
            data_dict = model_to_dict(msg)
            data_dict['create_time'] = str(data_dict['create_time'])

            if not self.is_superuser:
                if data_dict[
                        'project_code'] in the_project_list or nickname == data_dict[
                            'create_user']:
                    project_list.append(data_dict)
            else:
                project_list.append(data_dict)

        self.write(dict(code=0, msg='获取成功', data=project_list))
    def get(self, *args, **kwargs):
        project_code = self.get_argument('project_code',
                                         default=None,
                                         strip=True)
        environment = self.get_argument('environment',
                                        default=None,
                                        strip=True)
        service = self.get_argument('service', default=None, strip=True)
        filename = self.get_argument('filename', default=None, strip=True)
        if not project_code or not environment or not service or not filename:
            return self.write(dict(code=-1, msg='关键参数不能为空'))

        ### 鉴权
        the_pro_env_list, the_pro_per_dict = check_permissions(
            self.get_current_nickname())
        if not self.is_superuser:
            if not the_pro_per_dict.get(project_code):
                if "{}/{}".format(project_code,
                                  environment) not in the_pro_env_list:
                    return self.write(dict(code=-2, msg='没有权限'))

        history_list = []
        config_key = "/{}/{}/{}/{}".format(project_code, environment, service,
                                           filename)
        with DBContext('r') as session:
            conf_info = session.query(KerriganHistory).filter(
                KerriganHistory.config == config_key).order_by(
                    -KerriganHistory.id).limit(50)

        for msg in conf_info:
            data_dict = model_to_dict(msg)
            data_dict['create_time'] = str(data_dict['create_time'])
            history_list.append(data_dict)
        return self.write(dict(code=0, msg='获取历史成功', data=history_list))
def check_permissions(nickname):
    the_project_list = []
    the_pro_env_list = []
    is_admin = False
    the_pro_per_dict = {}
    with DBContext('r') as session:
        the_project = session.query(KerriganPermissions).filter(
            KerriganPermissions.nickname == nickname).all()

    for msg in the_project:
        data_dict = model_to_dict(msg)
        the_project_list.append(data_dict['project_code'])
        the_pro_env_list.append("{}/{}".format(data_dict['project_code'],
                                               data_dict['environment']))
        is_admin = data_dict['is_admin'] if data_dict['is_admin'] else is_admin
        the_pro_per_dict[data_dict['project_code']] = data_dict['is_admin']
    return the_pro_env_list, the_pro_per_dict
    def get(self, *args, **kwargs):
        project_code = self.get_argument('project_code',
                                         default=None,
                                         strip=True)
        nickname = self.get_current_nickname()
        if not project_code:
            return self.write(dict(code=-1, msg='关键参数不能为空'))

        config_list = []

        with DBContext('r') as session:
            config_info = session.query(KerriganConfig).filter(
                KerriganConfig.project_code == project_code,
                KerriganConfig.is_deleted == False).all()
            project_info = session.query(KerriganProject.project_name).filter(
                KerriganProject.project_code == project_code).first()

        the_pro_env_list, the_pro_per_dict = check_permissions(nickname)

        if not project_info:
            project_name = project_code
        else:
            project_name = project_info[0]

        for m in config_info:
            data_dict = model_to_dict(m)
            data_dict.pop('create_time')
            ###如果是超级管理  或者是此项目的管理员
            if self.is_superuser or the_pro_per_dict.get(project_code):
                config_list.append(data_dict)
            ###
            elif "{}/{}".format(data_dict['project_code'],
                                data_dict['environment']) in the_pro_env_list:
                config_list.append(data_dict)

        _tree = [{
            "expand": True,
            "title": project_code,
            "children": [],
            "data_type": 'project',
            "display_name": "%s | %s" % (project_code, project_name)
        }]

        if config_list:
            tmp_tree = {
                "environ": {},
                "service": {},
                "filename": {},
            }

            for t in config_list:
                filename, service, environ = t["filename"], t['service'], t[
                    "environment"]

                # 因为是第一层所以没有parent
                tmp_tree["environ"][environ] = {
                    "expand": True,
                    "title": environ,
                    "parent": "root",
                    "children": [],
                    "data_type": 'env'
                }

                # 父节点是对应的environ
                tmp_tree["service"][environ + "|" + service] = {
                    "expand": True,
                    "title": service,
                    "parent": environ,
                    "children": [],
                    "data_type": 'service'
                }

                # 最后一层没有children
                tmp_tree["filename"][environ + "|" + service + "|" +
                                     filename] = {
                                         "expand": True,
                                         "id": t['id'],
                                         "title": filename,
                                         "parent": environ + "|" + service,
                                         "env": environ,
                                         "service": service,
                                         "data_type": 'file'
                                     }

            for tmpFilename in tmp_tree["filename"].values():
                tmp_tree["service"][tmpFilename["parent"]]["children"].append(
                    tmpFilename)

            # service的数据插入到environ的children中
            for tmpService in tmp_tree["service"].values():
                tmp_tree["environ"][tmpService["parent"]]["children"].append(
                    tmpService)

            for tmpEnviron in tmp_tree["environ"].values():
                if tmpEnviron not in ["all", "all_env"]:
                    _tree[0]["children"].append(tmpEnviron)

            return self.write(dict(code=0, msg='获取项目Tree成功', data=_tree))
        else:
            return self.write(dict(code=0, msg='获取项目Tree失败', data=_tree))