def ueditor_post(request): token = token_service.get_token(request) if not token.is_valid: raise Exception('403 Forbidden') action = str(request.arguments.get('action')[0], encoding='utf-8') if action != 'uploadimage' and action != 'uploadscrawl': return {'state': '不支持操作!'} try: file_obj = request.files.get('upfile')[0] filename = file_obj.filename filebody = file_obj.body except: return {'state': '文件为空!'} name = str(uuid.uuid4()).replace('-', '') ext = filename[filename.rfind('.'):] filename = name + ext short_url = "ueditor/picture/" + filename long_url = nexus_client.upload_artifact_data(short_url, filebody) if long_url: return { 'state': 'SUCCESS', 'url': long_url, } else: return {'state': '上传文件出错'}
def find_article(id, http_request): token = token_service.get_token(http_request) has_role = token.has_role('ROLE_CONTENT') if not has_role: raise Exception('403 Forbidden') return article_db.get_article(id)
def get_deployment_logs(**args): username = args.get('username') deployment_uuid = args.get('deploymentUuid') token = token_service.get_token(args.get('http_request')) has_role = token.has_role('ROLE_OPERATOR') user_login = token.username if user_login is None or (user_login != username and not has_role): raise Exception('403 Forbidden') try: namespace = 'ucumos-' + replace_special_char(username) res = g.k8s_client.core_api.list_namespaced_pod( namespace, label_selector='ucumos=' + deployment_uuid) lines = [] for pods in res.items: if pods.status.phase != 'Running': continue lines.append('[pod ' + pods.metadata.name[-15:] + ']:') lines.append( g.k8s_client.core_api.read_namespaced_pod_log( pods.metadata.name, namespace)) except: raise Exception('获取k8s日志失败') return '\n'.join(lines)
def update_user(**args): token = token_service.get_token(args.get('http_request')) user_login = token.username has_role = token.has_role('ROLE_ADMIN') if not has_role: raise Exception('403 Forbidden') user = User() user.__dict__ = args.get('user') old = user_db.find_one_by_kv('email', user.email) if old is not None and old.id != user.id: raise Exception('400 email已存在') old = user_db.find_one_by_kv('phone', user.phone) if old is not None and old.id != user.id: raise Exception('400 phone已存在') user.lastModifiedBy = user_login user.lastModifiedDate = mytime.now() user_db.update_user(user) if hasattr(user, 'password') and user.password: user_db.change_password(user.login, passwd_service.encode(user.password)) return 0
def create_user(**args): token = token_service.get_token(args.get('http_request')) user_login = token.username has_role = token.has_role('ROLE_ADMIN') if not has_role: raise Exception('403 Forbidden') user = User() user.__dict__ = args.get('user') old = user_db.find_one_by_kv('login', user.login) if old is not None: raise Exception('400 已存在同名用户') old = user_db.find_one_by_kv('email', user.email) if old is not None: raise Exception('400 email已存在') old = user_db.find_one_by_kv('phone', user.phone) if old is not None: raise Exception('400 phone已存在') user.password = passwd_service.encode(user.password) user.activated = True user.imageUrl = '' user.langKey = 'en' user.createdBy = user_login user.createdDate = mytime.now() user.lastModifiedBy = user.createdBy user.lastModifiedDate = mytime.now() id = user_db.create_user(user) return id
def update_solution_active(**args): token = token_service.get_token(args.get('http_request')) user_login = token.username has_role = token.has_role('ROLE_MANAGER') solution = Solution() solution.__dict__ = solution_db.get_solution_by_id(args.get('solutionId')) if user_login != solution.authorLogin and not has_role: raise Exception('403 Forbidden') if solution.active and not args.get('active'): credit = Credit() credit.__dict__ = credit_service.find_user_credit(user_login) if credit.credit < 20: raise Exception('400 Bad request') else: solution.active = args.get('active') solution_db.update_solution_active(solution) credit_service.do_update_credit( credit, -20, '将AI模型<{}>设为私有'.format(solution.name)) return 0 else: solution.active = args.get('active') solution_db.update_solution_active(solution) return 0
def upload_document(**args): http_request = args.get('http_request') token = token_service.get_token(http_request) if not token.is_valid: raise Exception('403 Forbidden') user_login = token.username file_body = args.get('stream') solution_uuid = args.get('path_arg') res = umm_client.get_solutions(solution_uuid, token.jwt) if res['status'] != 'ok' or res['value']['total'] < 1: raise Exception('文档模型不存在') solution = Solution() solution.__dict__ = res['value']['results'][0] if solution.authorLogin != user_login: raise Exception('403 Forbidden') filename = http_request.files.get('upload_document')[0].filename short_url = solution.authorLogin + '/' + solution_uuid + '/document/' + filename long_url = nexus_client.upload_artifact_data(short_url, file_body) if long_url is None: raise Exception('向Nexus上传文件出错!') document = Document() document.solutionUuid = solution.uuid document.name = filename document.url = long_url document.fileSize = len(file_body) umm_client.create_document(document, token.jwt) return 0
def update_solution_baseinfo(**args): token = token_service.get_token(args.get('http_request')) user_login = token.username has_role = token.has_role('ROLE_MANAGER') solution = Solution() solution.__dict__ = solution_db.get_solution_by_id(args.get('solutionId')) if user_login != solution.authorLogin and not has_role: raise Exception('403 Forbidden') solution.name = args.get('name') if args.get('name') else '' solution.company = args.get('company') if args.get('company') else '' solution.version = args.get('version') if args.get('version') else '' solution.summary = args.get('summary') if args.get('summary') else '' solution.tag1 = args.get('tag1') if args.get('tag1') else '' solution.tag2 = args.get('tag2') if args.get('tag2') else '' solution.tag3 = args.get('tag3') if args.get('tag3') else '' solution.modelType = args.get('modelType') if args.get('modelType') else '' solution.toolkitType = args.get('toolkitType') if args.get( 'toolkitType') else '' solution.modifiedDate = mytime.now() solution_db.update_solution_baseinfo(solution) return 0
def get_deployment_status(**args): username = args.get('username') deployment_uuid = args.get('deploymentUuid') token = token_service.get_token(args.get('http_request')) has_role = token.has_role('ROLE_OPERATOR') user_login = token.username if user_login is None or (user_login != username and not has_role): raise Exception('403 Forbidden') try: namespace = 'ucumos-' + replace_special_char(username) res = g.k8s_client.apps_api.read_namespaced_deployment_status( 'deployment-' + deployment_uuid, namespace) deployment_status = DeploymentStatus() if res.status.replicas: deployment_status.replicas = res.status.replicas if res.status.ready_replicas: deployment_status.replicasReady = res.status.ready_replicas deployment_status.limitsCpu = res.spec.template.spec.containers[ 0].resources.limits["cpu"] deployment_status.limitsMem = res.spec.template.spec.containers[ 0].resources.limits["memory"] deployment_status.requestsCpu = res.spec.template.spec.containers[ 0].resources.requests["cpu"] deployment_status.requestsMem = res.spec.template.spec.containers[ 0].resources.requests["memory"] except: raise Exception('获取k8s状态失败') return deployment_status.__dict__
def get_credit_history(**args): target_login = args.get('targetLogin') # 带 targetLogin 表示是管理员在查看 pageable = { 'page': args.get('page'), 'size': args.get('size'), 'sort': args.get('sort'), } token = token_service.get_token(args.get('http_request')) user_login = token.username has_role = token.has_role('ROLE_ADMIN') if user_login is None or (target_login is not None and not has_role): raise Exception('403 Forbidden') if target_login is not None: user_login = target_login where = 'WHERE user_login = "******"'.format(user_login) total, results = credit_history_db.get_credit_historys(where, pageable) return { 'total': total, 'results': results, }
def find_application(id, http_request): token = token_service.get_token(http_request) has_role = token.has_role('ROLE_APPLICATION') if not has_role: raise Exception('403 Forbidden') return application_db.get_application(id)
def get_authorities(**args): token = token_service.get_token(args.get('http_request')) has_role = token.has_role('ROLE_ADMIN') if not has_role: raise Exception('403 Forbidden') return authority_db.get_authorities()
def update_current_account(**args): http_request = args.get('http_request') token = token_service.get_token(http_request) user_login = token.username user = User() user.__dict__ = args.get('user') if user_login is None or user_login != user.login: raise Exception('403 Forbidden') old = user_db.find_one_by_kv('email', user.email) if old is not None and old.login != user_login: raise Exception('400 email已存在') old = user_db.find_one_by_kv('phone', user.phone) if old is not None and old.login != user_login: raise Exception('400 phone已存在') old = user_db.find_one_by_kv('login', user_login) if old is None: raise Exception('400 用户不存在') user_db.update_user_base_info(user) return 0
def upload_attachment(**args): http_request = args.get('http_request') token = token_service.get_token(http_request) user_login = token.username has_role = token.has_role('ROLE_CONTENT') if not has_role: raise Exception('403 Forbidden') file_body = args.get('stream') file_obj = http_request.files.get('upload_attachment')[0] filename = file_obj.filename short_url = 'attachment/{}/{}'.format( str(uuid.uuid4()).replace('-', ''), filename) long_url = nexus_client.upload_artifact_data(short_url, file_body) if long_url is None: raise Exception('400 Upload to Nexus fail') attachment = Attachment() attachment.authorLogin = user_login attachment.name = filename attachment.url = long_url attachment.fileSize = len(file_body) attachment.createdDate = mytime.now() attachment.modifiedDate = mytime.now() id = attachment_db.create_attachment(attachment) return id
def delete_user(login, http_request): token = token_service.get_token(http_request) has_role = token.has_role('ROLE_ADMIN') if not has_role: raise Exception('403 Forbidden') user_db.delete_user_by_login(login) return 0
def get_credits(**args): token = token_service.get_token(args.get('http_request')) has_role = token.has_role('ROLE_ADMIN') if not has_role: raise Exception('403 Forbidden') user_login = args.get('userLogin') return credit_db.get_credits(user_login)
def delete_task(**args): token = token_service.get_token(args.get('http_request')) has_role = token.has_role('ROLE_ADMIN') if not has_role: raise Exception('403 Forbidden') task_db.delete_task(args.get('taskId')) return 0
def delete_deployment(**args): token = token_service.get_token(args.get('http_request')) has_role = token.has_role('ROLE_OPERATOR') if not has_role: raise Exception('403 Forbidden') deployment_db.delete_deployment(args.get('deploymentId')) return 0
def get_messages(**args): http_request = args.get('http_request') receiver = args.get('receiver') sender = args.get('sender') deleted = args.get('deleted') filter = args.get('filter') pageable = { 'page': args.get('page'), 'size': args.get('size'), 'sort': args.get('sort'), } token = token_service.get_token(http_request) user_login = token.username if user_login is None: raise Exception('403 Forbidden') if receiver is None and sender is None: raise Exception('403 Forbidden') if not (receiver is not None and receiver == user_login or sender is not None and sender == user_login): raise Exception('403 Forbidden') where1 = '' if receiver is not None: where1 += 'and receiver = "{}" '.format(receiver) if sender is not None: where1 += 'and sender = "{}" '.format(sender) if deleted is not None: where1 += 'and deleted = {} '.format(deleted) where1 = where1[4:] where2 = '' if filter is not None: where2 += 'receiver like "%{}%"'.format(filter) where2 += ' or sender like "%{}%"'.format(filter) where2 += ' or subject like "%{}%"'.format(filter) where2 += ' or content like "%{}%"'.format(filter) where = '' if where1: where += 'and {}'.format(where1) if where2: where += 'and {}'.format(where2) if where: where = where[4:] if where != '': where = 'WHERE ' + where total, results = message_db.get_messages(where, pageable) return { 'total': total, 'results': results, }
def delete_artifact(**args): token = token_service.get_token(args.get('http_request')) user_login = token.username if user_login != 'internal': # 只允许onboarding失败时由internal调用。 raise Exception('403 Forbidden') artifact_db.delete_artifact(id) return 0
def get_current_account(**args): http_request = args.get('http_request') token = token_service.get_token(http_request) user_login = token.username if user_login is None: raise Exception('401 Unauthorized') user = user_service.get_user_by_login(user_login) return user.__dict__
def delete_authority(authority, http_request): token = token_service.get_token(http_request) has_role = token.has_role('ROLE_ADMIN') if not has_role: raise Exception('403 Forbidden') authorities = authority_db.get_authorities() if authority in authorities: authority_db.delete_authority(authority) return 0
def update_credit(**args): token = token_service.get_token(args.get('http_request')) has_role = token.has_role('ROLE_ADMIN') if not has_role: raise Exception('403 Forbidden') credit = Credit() credit.__dict__ = credit_db.get_credit(args.get('creditId')) do_update_credit(credit, args.get('creditPlus'), '管理员后台配置') return 0
def delete_star(**args): token = token_service.get_token(args.get('http_request')) if not token.is_valid: raise Exception('403 Forbidden') user_login = token.username star_user = star_db.get_star_user_login(args.get('starId')) if user_login != star_user: raise Exception('403 Forbidden') star_db.delete_star(args.get('starId')) return 0
def delete_message(id, http_request): token = token_service.get_token(http_request) user_login = token.username if user_login is None: raise Exception('403 Forbidden') message = message_db.find_one_by_id(id) if message is None or message.receiver != user_login: raise Exception('403 Forbidden') message_db.delete_message(id) return 0
def update_article(**args): token = token_service.get_token(args.get('http_request')) has_role = token.has_role('ROLE_CONTENT') if not has_role: raise Exception('403 Forbidden') article = Article() article.__dict__ = args.get('article') article.modifiedDate = mytime.now() article_db.update_article(article) return 0
def update_deployment_status(**args): token = token_service.get_token(args.get('http_request')) user_login = token.username if user_login != 'internal': raise Exception('403 Forbidden') deployment = Deployment() deployment.__dict__ = deployment_db.get_deployment_by_id( args.get('deploymentId')) deployment.status = args.get('status') deployment_db.update_deployment_status(deployment) return 0
def get_message(id, http_request): token = token_service.get_token(http_request) user_login = token.username if user_login is None: raise Exception('403 Forbidden') message = message_db.find_one_by_id(id) if message is None or (message.sender != user_login and message.receiver != user_login): raise Exception('403 Forbidden') return message.__dict__
def change_password(**args): http_request = args.get('http_request') token = token_service.get_token(http_request) user_login = token.username if user_login is None: raise Exception('403 Forbidden') password = args.get('password') user_db.change_password(user_login, passwd_service.encode(password)) return 0
def delete_attachment(id, http_request): token = token_service.get_token(http_request) user_login = token.username has_role = token.has_role('ROLE_ADMIN') attachment = attachment_db.get_attachment(id) if not has_role and attachment.authorLogin != user_login: raise Exception('403 Forbidden') nexus_client.delete_artifact(attachment.url) attachment_db.delete_attachment(id) return 0