def delete(self, org_id, staff_id): """退出组织/删除雇员 --- tags: - 组织部门雇员 parameters: - name: org_id in: url type: string required: true description: 组织id - name: staff_id in: url type: string required: true description: 雇员id responses: 200: examples: response: {"data": null, "message": "退出成功"} """ user = get_current_user() current_org_staff = db.session.query(OrgStaff.id).filter( OrgStaff.user_id == user.id, OrgStaff.org_id == org_id).first() if not current_org_staff: return json_response(message="您不属于该组织", status=403) org_staff = OrgStaff.query.filter_by(id=staff_id, org_id=org_id).first_or_404() if org_staff.job_title == "组织创建人": return json_response(message="您是管理员,无法直接退出组织", status=403) else: db.session.delete(org_staff) db.session.commit() return json_response(message="退出成功")
def post(self, **kwargs): """获取手机验证码 --- tags: - 登录、注册 parameters: - name: phone in: body type: string required: true responses: 200: description: A list of colors (may be filtered by palette) examples: response: {"data": null, "message": "发送成功"} """ phone = kwargs.get("phone") user = User.query.filter_by(phone=phone).first() user_phone_code = None if user: user_phone_code = UserPhoneCode.query.filter_by( user_id=user.id).first() if user_phone_code and get_timestamp( ) - user_phone_code.update_time <= 300: return json_response(message="验证码请求频繁, 请稍后重试", status=403) else: user_id = generate_uuid() user = User( id=user_id, phone=phone, create_time=get_timestamp(), nickname=Conf.get("USER_DISABLE_NICKNAME_PREFIX", 'arrplat_') + phone[-4:]) user_info = UserInfo(user_id=user_id, head_url=Conf.get("DEFAULT_HEAD_IMAGE", ""), signature="这个人很懒,什么都没留下") db.session.add_all([user, user_info]) db.session.commit() # TODO 验证码先用假的的 # payload = send_phone_code(phone) payload = {"code": "OK", "phone_code": "123456"} if payload["code"] == "OK": current_timestamp = get_timestamp() if not user_phone_code: user_phone_code = UserPhoneCode( user_id=user.id, code=payload["phone_code"], create_time=current_timestamp, update_time=current_timestamp, valid_time=600, ) else: user_phone_code.code = payload["phone_code"] user_phone_code.update_time = current_timestamp db.session.add(user_phone_code) db.session.commit() return json_response(message="获取成功") return json_response(message="短信发送失败", status=500)
def get(self, org_request_id): """登录界面的邀请提示 --- tags: - 用户 parameters: - name: org_request_id in: url type: string required: true description: 组织邀请id responses: 200: examples: response: {"data": {"org_request_message": "158282382ad547fc920c54c44dbd015f"}, "message": "ok"} """ query_result = db.session.query(User.nickname, Organization.name)\ .join(OrgRequest, OrgRequest.org_id == Organization.id)\ .filter(OrgRequest.sender_id == User.id, OrgRequest.id == org_request_id).first() if not query_result: return json_response(message="没有找到邀请信息", status=404) org_request_message = "用户 %s 邀请您加入 %s" %(query_result.nickname, query_result.name) data = { "org_request_message": org_request_message } return json_response(data=data, message="ok")
def delete(self, org_id): """退出组织 --- tags: - 用户 parameters: - name: org_id in: path type: int required: True description: 要退出的组织id responses: 200: examples: response: {"data": null, "message": "退出组织成功"} 403: examples: response: {"data": null, "message": "您是组织创建人,无法直接退出组织"} 404: examples: response: {"data": null, "message": "您未加入该组织"} """ user = get_current_user() query_result = db.session.query(OrgStaff.id, OrgStaff.job_title).filter( OrgStaff.org_id == org_id, OrgStaff.user_id == user.id).first() if not query_result: return json_response(message="您未加入该组织", status=404) if query_result.job_title == "组织创建人": return json_response(message="您是组织创建人,无法直接退出组织", status=403) db.session.query(OrgStaff).filter(OrgStaff.id == query_result.id).delete() db.session.commit() return json_response(message="退出组织成功")
def post(self, key, **kwargs): """获取页面详情 --- tags: - 页面 parameters: - name: id in: body type: string required: true """ share = db.session.query(SharePage).filter(SharePage.key == key).first() if not share: return json_response(message="页面未找到", status=404) if share.type == ShareType.password: if not share.password == kwargs.get('pwd'): return json_response(message="密码错误", status=401) page = db.session.query(Page).filter(Page.id == share.page_id).first() if not page: return json_response(message="找不到页面", status=404) page = self.page_schema.dump(page).data return json_response(data=page)
def delete(self, data_source_id): """删除数据源 --- tags: - 数据源 parameters: responses: 200: description: A list of colors (may be filtered by palette) examples: response: {"data": null, "message": "添加成功"} """ user = get_current_user() if not data_source_id: return json_response(message="参数错误", status=400) data_source = db.session.query(DataSource).filter( DataSource.id == data_source_id).first() org = org_exists(data_source.org_id) if not org: return json_response(message=f"组织ID错误", status=403) staff = db.session.query(OrgStaff).filter( OrgStaff.user_id == user.id and OrgStaff.org_id == org.id).first() if not staff: return json_response(message=f"用户无权限", status=403) db.session.delete(data_source) db.session.commit() return json_response(message="删除成功", data={})
def post(self, **kwargs): app_id = kwargs.get('application_id') app = db.session.query(Application).filter( Application.id == app_id).first() if not app: return json_response(message="找不到应用", status=404) """添加应用菜单 --- tags: - 轻应用 parameters: - name: id in: body type: number required: true """ menu = ApplicationMenus(name=kwargs.get('name'), application_id=app_id, icon=kwargs.get('icon'), link=kwargs.get('link'), type=ApplicationMenus.string_to_page_type( kwargs.get('type')), platform=kwargs.get('platform', 'pc, mobile'), sort=kwargs.get('sort', 0), parent_id=kwargs.get('parent_id'), page_key=kwargs.get('page_key'), page_id=kwargs.get('page_id')) db.session.add(menu) db.session.commit() return json_response(message="添加成功", data={"id": menu.id})
def put(self, id, **kwargs): """应用基本信息修改 --- tags: - 轻应用 parameters: - name: id in: body type: string required: true """ if not kwargs or not kwargs.get("title"): return json_response(message="参数错误", status=400) app = db.session.query(Application).filter( Application.id == id).first() if not app: return json_response(message=f"未找到应用", status=404) app.name = kwargs.get("name") app.title = kwargs.get("title") app.key = kwargs.get("key") app.description = kwargs.get("description") app.icon = kwargs.get("icon") db.session.add(app) db.session.commit() return json_response(message="修改成功")
def post(self, **kwargs): """创建轻应用 --- tags: - 应用 parameters: - name: title in: body type: string required: true - name: org_id in: body type: string required: true - name: description in: body type: string required: false responses: 200: description: A list of colors (may be filtered by palette) examples: response: {"data": null, "message": "添加成功"} """ org = db.session.query(Organization).filter( Organization.id == kwargs.get("org_id")).first() if not org: return json_response(message=f"组织ID错误", status=403) app_name = generate_uuid_len(7) app = Application(name=app_name, admin_route=app_name, api_route=app_name, title=kwargs.get("title"), description=kwargs.get("description", None), icon="/app.png", is_official=False, own_org_id=org.id, type=2, visible=1) db.session.add(app) db.session.commit() index_menu = ApplicationMenus(application_id=app.id, name="首页", icon="", link="/light/" + app.admin_route, platform="pc,mobile", sort=1) org_app = OrgApplication(organization_id=org.id, application_id=app.id, status=1) db.session.add(index_menu) db.session.add(org_app) db.session.commit() return json_response(message="添加成功", data={"id": app.id})
def get(self, entity_id, **kwargs): """修改业数据回显选项列表 --- tags: - page页面数据 parameters: - name: entity_id in: path type: string required: true enum: ['12363708151a46879f3efb76780c5ba9'] description: 实例id - name: org_id in: querystring type: string required: false description: 组织id responses: 200: description: 成功 examples: response: {"data": {"entity_data": [], "entity_fields": []}} """ entity = Entity.query.filter_by(id=entity_id).first() org_id = kwargs.get('org_id') if not entity: return json_response(message="未找到对应的entity", status=404) try: entity_schema = EntityOptionSchema(org_id=org_id) data = entity_schema.dump(entity).data except Exception as e: _ = e return json_response(message=str(e), status=403) return json_response(data=data)
def get(self, org_id, staff_id, **kwargs): """组织部门雇员信息 --- tags: - 组织部门雇员 parameters: - name: org_id in: url type: string required: true description: 组织id - name: staff_id in: url type: string required: true description: 雇员id responses: 200: description: A list of colors (may be filtered by palette) examples: response: {"data": {}, "message": "ok"} """ org_staff = OrgStaff.query.filter_by(id=staff_id, org_id=org_id, is_able=1).first() if not org_staff: return json_response(message="找不到该雇员信息", status=404) org_staff_schema = OrgStaffSchema(only=("id", "job_title", "user", "is_able", "parent_superior")) data = org_staff_schema.dump(org_staff).data return json_response(data=data, message="ok")
def put(self, org_id, staff_id, **kwargs): org_staff = OrgStaff.query.filter_by(id=staff_id, org_id=org_id, is_able=1).first() if not org_staff: return json_response(message="找不到该雇员信息", status=404) org_staff_schema = OrgStaffSchema(only=("id", "job_title", "user", "is_able", "parent_superior")) department_id_list = kwargs.get('department_id_list') job_title = kwargs.get('job_title') superior_id = kwargs.get('superior_id') role_id_list = kwargs.get('role_id_list') is_able = kwargs.get('is_able') if superior_id: superior_obj = OrgStaff.query.filter( OrgStaff.id == superior_id, OrgStaff.org_id == org_id).first() if not superior_obj: return json_response(None, message='直属人未找到', status=404) org_staff.job_title = job_title org_staff.superior_id = superior_id org_staff.is_able = is_able OrgDepartmentStaff.query.filter( OrgDepartmentStaff.staff_id == staff_id).delete() OrgStaffRole.query.filter(OrgStaffRole.staff_id == staff_id).delete() add_staff_role(role_id_list, staff_id) add_staff_department(department_id_list, staff_id) db.session.commit() return json_response(data={}, message="AAA")
def get(self, org_id, org_department_id): """组织部门信息获取 --- tags: - 组织部门 parameters: - name: org_id in: url type: string required: true description: 组织id - name: org_department_id in: url type: string required: true description: 部门id responses: 200: description: A list of colors (may be filtered by palette) examples: response: {"data": {}, "message": "ok"} """ user = get_current_user() org_staff = db.session.query(OrgStaff.id).filter( OrgStaff.user_id == user.id, OrgStaff.org_id == org_id).first() if not org_staff: return json_response(message="您不属于该组织", status=403) org_department = OrgDepartment.query.filter_by( id=org_department_id).first_or_404() org_department_schema = OrgDepartmentSchema() data = org_department_schema.dump(org_department).data return json_response(data=data, message="ok")
def put(self, id, **kwargs): """菜单修改 --- tags: - 轻应用 parameters: - name: id in: body type: string required: true """ if not kwargs or not kwargs.get("name"): return json_response(message="参数错误", status=400) menu = db.session.query(ApplicationMenus).filter( ApplicationMenus.id == id).first() if not menu: return json_response(message=f"未找到菜单", status=404) menu.name = kwargs.get("name") menu.icon = kwargs.get("icon") menu.link = kwargs.get("link") menu.type = kwargs.get('type', None) menu.page_key = kwargs.get("page_key") menu.page_id = kwargs.get("page_id") menu.sort = kwargs.get("sort", 1) db.session.add(menu) db.session.commit() return json_response(message="修改成功")
def delete(self, org_id, app_id): """移除组织应用 --- tags: - 组织应用 parameters: - name: org_id in: path type: string description: - name: app_id in: path type: string description: responses: 200: description: A list of colors (may be filtered by palette) examples: response: {"data": null, "message": "移除成功"} """ query = db.session.query(Application, OrgApplication).filter( OrgApplication.organization_id == org_id, OrgApplication.application_id == app_id, Application.id == OrgApplication.application_id).first_or_404() if query.Application.deletable == 0: return json_response(message='该应用不可以删除', status=403) else: db.session.delete(query.OrgApplication) db.session.commit() return json_response(message="移除成功")
def post(self, **kwargs): """添加应用页面 --- tags: - 轻应用 parameters: - name: id in: body type: string required: true """ app_id = kwargs.get('app_id') app = db.session.query(Application).filter( Application.id == app_id).first() if not app: return json_response(message="找不到应用", status=404) direction = kwargs.get('direction') key = generate_uuid_len(7) page = Page( key=key, title=kwargs.get('title'), application_id=app.id, direction=Page.string_to_direction_type(direction), ) db.session.add(page) db.session.commit() return json_response(message="添加成功", data={"id": page.id})
def get(self, cluster_id): """获取集群详情 --- tags: - 集群 parameters: - name: cluster_id in: path type: string required: true responses: 200: examples: response: {"data": {"id":"xx", "host":"https://xxx", "port":6443, "token":"xxx"}, "message": "ok"} """ user = get_current_user() cluster = db.session.query(Cluster).join(Organization, OrgStaff).filter( Cluster.id == cluster_id, OrgStaff.user_id == user.id).first() if not cluster: return json_response(message="找不到该集群", status=403) cluster_schema = ClusterSchema(exclude=("token",)) data = cluster_schema.dump(cluster).data return json_response(data=data)
def post(self, cluster_id, **kwargs): """创建集群无状态应用 --- tags: - 集群 parameters: - name: name in: json type: string required: true - name: namespace in: json type: string required: true - name: image in: json type: string required: true - name: image_tag in: json type: string required: true - name: replicas in: json type: int required: true - name: ports in: json type: array required: false examples: [{"port":5000, "protocol": "TCP"}, {"port":8080, "protocol": "UDP"}] - name: env in: json type: array required: false examples: [{"key":PYTHON_ENV, "value": "production"}] responses: 200: examples: response: {"data": [{ "create_time": "", "name": "test-deployment", "namespace": "test-namespace", "replicas": 2, "labels": "test-labels" }], "message": "ok"} """ kwargs["ports"] = valid_deployment_ports(kwargs.get("ports", list())) kwargs["env"] = valid_env(kwargs.get("env", list())) try: k8s = get_k8s(cluster_id) except Exception as e: return json_response(message=str(e), status=404) create_result = k8s.create_deployment(**kwargs) status = 200 if create_result["success"] is True else 500 message = create_result["message"] + " - " + str(create_result.get("error", "")) return json_response(message=message, status=status)
def put(self, org_id, role_id, **kwargs): """修改角色员工 --- tags: - 权限、功能 parameters: - name: role_id in: path type: string required: true - name: org_id in: path type: string required: true - name: staff_id_list in: body type: array required: true responses: 200: description: examples: response: {"data": null, "message": "ok"} 500: examples: response: {"data": null, "message": "失败"} """ staff_id_list = kwargs['staff_id_list'] org_staff_role_obj = OrgStaffRole.query.filter( OrgStaffRole.org_role_config_id == role_id).all() left_staff_id_list = [i.staff_id for i in org_staff_role_obj] temp_add_obj = list( set(staff_id_list).difference(set(left_staff_id_list))) temp_delete_obj = list( set(left_staff_id_list).difference(set(staff_id_list))) # 增加 for item in temp_add_obj: temp_add_org_staff_obj = OrgStaffRole(staff_id=item, org_role_config_id=role_id) db.session.add(temp_add_org_staff_obj) # 删除 temp_del_org_staff_obj = OrgStaffRole.query.filter( OrgStaffRole.org_role_config_id == role_id, OrgStaffRole.staff_id.in_(temp_delete_obj)).all() for item in temp_del_org_staff_obj: db.session.delete(item) try: db.session.commit() return json_response(None, message='修改成功') except Exception as e: _ = e db.session.rollback() return json_response(None, message=f'修改失败{e}', status=500)
def get(self, entity_key): entity = Entity.query.filter(Entity.key == entity_key).first() if not entity: return json_response(message="未找到对应的entity", status=404) try: entity_schema = EntityFieldsSchema() data = entity_schema.dump(entity).data except Exception as e: _ = e return json_response(message=str(e), status=403) return json_response(data=data)
def get(self, **kwargs): """获取加入的组织列表 --- tags: - 组织 parameters: - name: has_detail in: querystring type: int description: 是否需要详细的信息 0|1 responses: 200: description: A list of colors (may be filtered by palette) examples: response: {"data": [], "message": "ok"} """ has_detail = kwargs.get("has_detail") user = get_current_user() org_schema = OrganizationSchema( many=True, only=("id", "group_type", "name", "unique_id", "address", "website", "own_user_id", "official_company_name", "official_org_code", "description", "head_url")) if not has_detail: org_list = Organization.query.join( OrgStaff, Organization.id == OrgStaff.org_id).filter( OrgStaff.user_id == user.id).all() data = org_schema.dump(org_list).data return json_response(data=data, message="ok") else: org_schema.many = False org_staff_schema = OrgStaffSchema(only=("id", "organization.id", "organization.name", "org_department_staff", "job_title")) data = list() org_staff_list = db.session.query(Organization, OrgStaff).join( OrgStaff, Organization.id == OrgStaff.org_id).filter( OrgStaff.user_id == user.id).all() for org, org_staff in org_staff_list: org_data = org_schema.dump(org).data org_staff_data = org_staff_schema.dump(org_staff).data org_data["staff_info"] = org_staff_data data.append(org_data) return json_response(data=data, message='ok') # org_staff_list = OrgStaff.query.filter_by(user_id=user.id).all() # org_staff_schema = OrgStaffSchema(many=True, only=("id", "organization.id", "organization.name", "org_department_staff", "job_title")) # # data = org_staff_schema.dump(org_staff_list).data return json_response(data=data, message="ok")
def get(self, id): data_source = db.session.query(DataSource) \ .join(Page, and_(DataSource.id == id)) \ .first() if not data_source: return json_response(message="找不到对应的数据源", status=404) entities = db.session.query(Entity) \ .join(Page, and_(Entity.data_source_id == id)).all() data = self.data_source_schema.dump(data_source).data data["entities"] = self.entity_simple_schema.dump(entities).data return json_response(data=data)
def delete(self, org_id, org_department_id): """部门删除 --- tags: - 组织部门 parameters: - name: org_id in: url type: string required: true description: 组织id - name: org_department_id in: url type: string required: true description: 部门id responses: 200: description: A list of colors (may be filtered by palette) examples: response: {"data": null, "message": "删除成功"} """ user = get_current_user() org_department = db.session.query(OrgDepartment).join( OrgStaff, OrgStaff.org_id == OrgDepartment.org_id).filter( OrgStaff.user_id == user.id, OrgDepartment.id == org_department_id, OrgDepartment.org_id == org_id).first() if not org_department: return json_response(message="找不到部门", status=404) if not org_department.parent_id: return json_response(message="根部门无法删除", status=403) root_department = OrgDepartment.query.filter_by( org_id=org_id, parent_id=None).first() db.session.query(OrgDepartmentStaff).filter( OrgDepartmentStaff.org_department_id == org_department.id).update( {OrgDepartmentStaff.org_department_id: root_department.id}) db.session.query(OrgDepartment).filter( OrgDepartment.parent_id == org_department.id).update( {OrgDepartment.parent_id: root_department.id}) db.session.delete(org_department) db.session.commit() return json_response(message="部门删除成功,内部员工转到根部门")
def wrapper(*args, **kwargs): try: verify_jwt_in_request() except Exception as e: _ = e return json_response(None, message='not found token', status=401) # 这里没必要去获取user,因为只有去判断用户是否存在,只需要id就够了 # user = User.query.filter_by(id=get_jwt_identity(), is_able=1).first() user_id = db.session.query(User.id).filter( User.id == get_jwt_identity(), User.is_able == 1).first() db.session.commit() if not user_id: return json_response(None, status=401) return add_old_token_to_blacklist(fn)(*args, **kwargs)
def get(self, cluster_id, **kwargs): """获取组织集群的无状态应用 --- tags: - 集群 parameters: - name: namespace in: querystring type: string required: false - name: limit in: querystring type: int required: false responses: 200: examples: response: {"data": [{ "create_time": "", "name": "test-deployment", "namespace": "test-namespace", "replicas": 2, "labels": "test-labels" }], "message": "ok"} """ namespace = kwargs.get("namespace") try: k8s = get_k8s(cluster_id) except Exception as e: return json_response(message=str(e), status=404) if namespace: k8s_deployment_list = k8s.v1beta_app.list_namespaced_deployment(**kwargs) else: k8s_deployment_list = k8s.v1beta_app.list_deployment_for_all_namespaces(**kwargs) data = list() for item in k8s_deployment_list.items: metadata = item.metadata data.append({ "create_time": metadata.creation_timestamp, "name": metadata.name, "namespace": metadata.namespace, "replicas": item.spec.replicas, "labels": item.metadata.labels }) return json_response(data=data, message="ClusterDeploymentResource")
def get(self, org_id): """功能菜单 --- tags: - 权限、功能 responses: 200: description: examples: response: {"data": {"system_menu": "", "application_menu": ""}, "message": "ok"} """ base_query = OrgAuthPowerConfig.query.filter( OrgAuthPowerConfig.parent_id == None) system_auth_menu_obj = base_query.filter( OrgAuthPowerConfig.type == 0).all() application_auth_menu_obj = base_query.filter( OrgAuthPowerConfig.type == 1).join( Application, OrgApplication).filter( OrgApplication.organization_id == org_id).all() org_auth_power_config_schema = OrgAuthPowerConfigSchema(many=True) sys_res = org_auth_power_config_schema.dump(system_auth_menu_obj).data application_res = org_auth_power_config_schema.dump( application_auth_menu_obj).data res = {'system_menu': sys_res, 'application_menu': application_res} return json_response(res, message='查询成功')
def get(self, org_id): """获取组织下的所有部门 --- tags: - 组织部门 parameters: - name: org_id in: url type: string required: true responses: 200: examples: response: {"data": [], "message": "ok"} """ user = get_current_user() org_department_list = db.session.query(OrgDepartment) \ .filter(OrgDepartment.org_id == org_id, OrgStaff.org_id == org_id, OrgStaff.user_id == user.id, OrgDepartment.parent_id == None) \ .all() org_department_schema = OrgDepartmentSchema(many=True) org_department_data = org_department_schema.dump( org_department_list).data return json_response(data=org_department_data, message="ok")
def get(self, org_id, role_id, **kwargs): """角色员工列表 --- tags: - 权限、功能 parameters: - name: role_id in: path type: string required: true - name: org_id in: path type: string required: true responses: 200: description: examples: response: {"data": "data", "message": "ok"} """ page = kwargs.get('page', DEFAULT_PAGE) size = kwargs.get('size', DEFAULT_PAGE_SIZE) staff_obj = OrgStaff.query.join(OrgStaffRole, OrgRoleConfig).filter( OrgRoleConfig.org_id == org_id, OrgRoleConfig.id == role_id) staff_schema = OrgStaffSchema( many=True, only=('id', 'job_title', 'user', 'is_able', 'parent_superior', 'org_staff_role', 'org_department_staff')) page_result = paginate(staff_obj, page, size) res = staff_schema.dump(page_result.items).data return json_response(data=res, page=page_result)
def delete(self, org_id, role_id): """删除角色 --- tags: - 权限、功能 parameters: - name: role_id in: path type: string required: true - name: org_id in: path type: string required: true responses: 200: description: examples: response: {"data": "data", "message": "ok"} """ org_role_config_obj = OrgRoleConfig.query.filter( OrgRoleConfig.id == role_id).first_or_404() db.session.delete(org_role_config_obj) db.session.commit() return json_response(None, message='角色删除成功')
def delete(self, org_id, role_id, **kwargs): """删除角色员工 --- tags: - 权限、功能 parameters: - name: role_id in: path type: string required: true - name: org_id in: path type: string required: true - name: staff_id in: body type: string required: true responses: 200: description: examples: response: {"data": "data", "message": "ok"} """ staff_id = kwargs['staff_id'] role_id = role_id org_staff_obj = OrgStaffRole.query.filter( OrgStaffRole.staff_id == staff_id, OrgStaffRole.org_role_config_id == role_id).first_or_404() db.session.delete(org_staff_obj) db.session.commit() return json_response(None, message='删除成功')