def get_by_user( user_id, page_no=1, page_size=100, ): start = (page_no - 1) * page_size end = page_no * page_size receivers = receiver_repo.read({'user': ObjectId(user_id)}) receivers_info = json_utility.convert_to_json( [i.to_mongo() for i in receivers]) messages = [] for receiver in receivers_info: message = message_repo.read_unique_one( {'_id': ObjectId(receiver['message'])}) message_info = message.to_mongo() message_info['user_ID'] = message.user.user_ID if message.user_request: message_info['user_request_title'] = message.user_request.title message_info['user_request_type'] = message.user_request.type message_info['is_read'] = receiver['is_read'] message_info['receiver_id'] = receiver['_id'] messages.append(message_info) messages = json_utility.convert_to_json(messages) return messages
def set_action_entity(entity_id): """ 在用户和api下都存一份 用户存api_id api存 user_ID :return: :rtype: """ user_ID = get_jwt_identity() data = request.get_json() action = data.pop("action") entity = data.pop("entity") result = UserService.action_entity( user_ID=user_ID, entity_id=entity_id, action=action, entity=entity) if result: return jsonify({ 'message': "success", 'response': { "entity": json_utility.convert_to_json( result.entity.to_mongo()), "user": json_utility.convert_to_json(result.user.to_mongo()) } }), 200 else: return jsonify({'response': "failed"}), 400
def convert_used_datasets(app): ums = [{ 'dataset': json_utility.convert_to_json(d.dataset.to_mongo()) } for d in app.used_datasets] del app.used_datasets app = json_utility.convert_to_json(app.to_mongo()) app['used_datasets'] = ums return app
def convert_used_modules(app): ums = [{ 'module': json_utility.convert_to_json(m.module.to_mongo()), 'version': '.'.join(m.version.split('_')) } for m in app.used_modules] del app.used_modules app = json_utility.convert_to_json(app.to_mongo()) app['used_modules'] = ums return app
def convert_action_entity(objects, action_entity): if action_entity == 'used_modules': ums = [{ 'module': json_utility.convert_to_json(m.module.to_mongo()), 'version': '.'.join(m.version.split('_')) } for m in objects] return ums if action_entity == 'used_datasets': uds = [{ 'dataset': json_utility.convert_to_json(m.dataset.to_mongo()) } for m in objects] return uds
def update_user_account(): user_ID = get_jwt_identity() data = request.get_json() token_for_update_info = data['tokenForUpdateInfo'] phone = data.get('phone', None) email = data.get('email', None) password = data.get('password', None) captcha = data.get('captcha', None) payload = jwt.decode(token_for_update_info, UPDATE_USER_INFO_SK, algorithm='HS256') if payload['user_ID'] != user_ID or payload['expireTime'] < time.time(): return jsonify({'response': {'error': 'tokenError'}}), 400 elif phone: # 更改手机 # 验证手机 验证码 try: if UserBusiness.get_by_phone(phone): return jsonify({ "response": {'error': "手机号已被注册,请更换手机号"} }), 400 res = user_service.verify_code(code=captcha, phone=phone) user = UserBusiness.get_by_user_ID(user_ID) user.phone = phone user.save() return jsonify({'response': { "user": json_utility.convert_to_json(user.to_mongo()) }}), 200 except Error as e: return jsonify({ "response": {'error': "验证码错误"} }), 400 elif email: # 更改邮箱 # 验证邮箱 验证码 if UserBusiness.get_by_email(email): return jsonify({ "response": {'error': "邮箱已被注册,请更换邮箱"} }), 400 try: user = UserService.update_user_email(user_ID, email, captcha) return jsonify({'response': { "user": json_utility.convert_to_json(user.to_mongo()) }}), 200 except Error as e: return jsonify({ "response": {'error': e.args[0]} }), 400 elif password: UserBusiness.update_password(user_ID, password) else: return jsonify({'response': {'error': 'unkownError'}}), 400 return jsonify({'response': 'ok'}), 200
def get_app(app_id): user_ID = get_jwt_identity() yml = request.args.get('yml') commits = request.args.get('commits') version = request.args.get('version') used_modules = request.args.get('used_modules') used_datasets = request.args.get('used_datasets') app = AppService.get_by_id(app_id, yml=yml, commits=commits, version=version, used_modules=used_modules) # 如果是私有项目,需要确定其登陆才能查看,否则返回error if app.privacy == 'private' and app.user.user_ID != user_ID: return jsonify({'response': 'error'}), 200 # 将app.user 更换为 user_ID 还是name? user_ID = app.user.user_ID if used_modules == 'true': app = convert_used_modules(app) if used_datasets == 'true': app = convert_used_datasets(app) if used_modules != 'true' and used_datasets != 'true': app = json_utility.convert_to_json(app.to_mongo()) app["user_ID"] = user_ID app["commits"].reverse() return jsonify({"response": app}), 200
def deploy_module(project_id): data = request.get_json() commit_msg = data.get('commit_msg') project = ModuleService.deploy(project_id=project_id, commit_msg=commit_msg) project = json_utility.convert_to_json(project.to_mongo()) return jsonify({"response": project})
def create_project(): if not request.json \ or 'name' not in request.json \ or 'type' not in request.json: return jsonify({'response': 'insufficient arguments'}), 400 user_token = request.headers.get('Authorization').split()[1] user_ID = get_jwt_identity() data = request.get_json() name = data.pop('name') type = data.pop('type') description = data.pop('description') tags = data.pop('tags', '') if not isinstance(tags, list) and tags is not None: data['tags'] = str_utility.split_without_empty(tags) type_mapper = { "app": AppService, "module": ModuleService, "dataset": DatasetService, } try: project = type_mapper[type].create_project(name, description, user_ID, tags=tags, type=type, user_token=user_token, **data) except Exception as e: return jsonify({'response': str(e)}), 400 project = json_utility.convert_to_json(project.to_mongo()) return jsonify({'response': project}), 200
def update_job_log(job_id): data = request.get_json() log_type = data.get('log_type') message = data.get('message') result = JobBusiness.update_log(job_id, log_type, message) result = json_utility.convert_to_json(result.to_mongo()) return jsonify({"response": result}), 200
def create_job(): """用于生成用户工作单位section :return: :rtype: """ data = request.get_json() print(data) project_id = data.get("project_id") type = data.get("type") user_ID = data.get("user_ID") source_file_path = data.get("source_file_path") run_args = data.get("run_args") running_module = data.get("running_module") running_code = data.get("running_code") job_obj = JobService.create_job( project_id=project_id, type=type, user_ID=user_ID, source_file_path=source_file_path, run_args=run_args, running_module=running_module, running_code=running_code, ) job_obj = json_utility.convert_to_json(job_obj.to_mongo()) return jsonify({"response": job_obj})
def refresh_token(): # Access the identity of the current user with get_jwt_identity # current_user = get_jwt_identity() claims = get_jwt_claims() user = UserBusiness.get_by_user_ID(claims['user_ID']) user = json_utility.convert_to_json(user.to_mongo()) return jsonify({'response': {'user': user}}), 200
def upload_predict_image(): # user_ID = request.form.get('user_ID') # if not user_ID: # return jsonify({'response': 'no user_ID'}), 400 user_ID = request.args.get('user_ID') if request.method == 'POST': # check if the post request has the file part if REQUEST_FILE_NAME not in request.files: return jsonify({'response': 'no file part'}), 400 file = request.files[REQUEST_FILE_NAME] if file.filename == '': return jsonify({'response': 'no selected file'}), 400 if file and file_service.allowed_file(file.filename): url_base = PREFIX + UPLOAD_URL saved_file = file_service.add_file(file.filename, file, url_base, user_ID, is_private=True, ds_type='image', predict=True, **request.form) file_json = json_utility.convert_to_json(saved_file.to_mongo()) return jsonify({'response': file_json}) else: return jsonify({'response': 'file is not allowed'}), 400
def login_with_phone(): """ 通过手机号登录, :return: :rtype: """ data = request.get_json() phone = data.pop("phone") # message_id = data.pop("message_id") code = data.pop("code") if not UserBusiness.check_exist({'phone': phone}): return jsonify({"response": { "error": "This phone number is not registered, " "please input the right phone number"}}), 400 try: result = user_service.verify_code(code=code, phone=phone) if result: user = user_business.get_by_phone(phone=phone) response = {'response': { 'token': create_access_token(identity=user.user_ID), 'user': json_utility.convert_to_json(user.to_mongo())}} return jsonify(response), 200 except Error as e: return jsonify({ "response": { "error": 'Please enter correct verify code' } }), 400
def project_fork(project_id): user_ID = request.args.get('user_ID') if not user_ID: raise ValueError('no user ID arg') new_project = project_service.fork(project_id, user_ID) new_project = json_utility.convert_to_json(new_project.to_mongo()) return jsonify({'response': new_project}), 200
def add(): """ :return: :rtype: """ if not request.json \ or 'name' not in request.json \ or 'type' not in request.json: return jsonify({'response': 'insufficient arguments'}), 400 user_token = request.headers.get('Authorization').split()[1] user_ID = get_jwt_identity() data = request.get_json() name = data.pop('name') type = "app" # data.pop('type') description = data.pop('description') tags = data.pop('tags', '') tags = str_utility.split_without_empty(tags) project = AppService.create_project(name, description, user_ID, tags=tags, type=type, user_token=user_token, **data) project = json_utility.convert_to_json(project.to_mongo()) return jsonify({'response': project}), 200
def deploy_in_docker(app_id): data = request.get_json() handler_file_path = data.get('file_path') commit_msg = data.get('commit_msg') project = AppService.deploy(app_id, commit_msg, handler_file_path) project = json_utility.convert_to_json(project.to_mongo()) return jsonify({"response": project})
def get_by_staging_data_set_and_fields(): data = request.get_json() staging_data_set_id = data.get('staging_data_set_id') toolkit_id = data.get('toolkit_id') project_id = data.get('project_id') conf = data.get('conf') # conf初步操作 flag = isinstance(conf["data_fields"][0], (list, tuple)) x_fields = conf["data_fields"][0] if flag else conf["data_fields"] y_fields = conf["data_fields"][1] if flag else None fields = x_fields + y_fields if flag else x_fields data = staging_data_business.get_by_staging_data_set_and_fields( ObjectId(staging_data_set_id), fields) # 数据库转to_mongo和to_dict data = [d.to_mongo().to_dict() for d in data] # 拿到conf fields = [x_fields, y_fields] conf = conf.get('args') result = toolkit_service.convert_json_and_calculate(project_id, staging_data_set_id, toolkit_id, fields, data, conf) result.update({"fields": [x_fields, y_fields]}) return jsonify({'response': json_utility.convert_to_json(result)}), 200
def list_user_request(): group = request.args.get('group') page_no = int(request.args.get('page_no', 1)) page_size = int(request.args.get('page_size', 5)) search_query = request.args.get('search_query', None) type = request.args.get('type', None) user_ID = request.args.get('user_ID', None) if type == 'all': type = None user_requests, total_number = user_request_service.get_list( type=type, search_query=search_query, page_no=page_no, page_size=page_size, user_ID=user_ID ) # user_requests_info = json_utility.me_obj_list_to_json_list(user_requests) # 读取每个request下anser的数量 user_requests_info = [] for each_request in user_requests: each_request_info = json_utility.convert_to_json( each_request.to_mongo()) each_request_info['answer_number'] = \ request_answer_service.get_all_answer_of_this_user_request( each_request_info['_id'], get_number=True) each_request_info['comment_number'] = \ comments_service.count_comments_of_this_user_request( each_request_info['_id']) each_request_info['user_ID'] = each_request.user.user_ID user_requests_info.append(each_request_info) return jsonify({'response': {'user_request': user_requests_info, 'total_number': total_number}}), 200
def login_with_phone(): """ 通过手机号登录, :return: :rtype: """ data = request.get_json() phone = data.pop("phone") # message_id = data.pop("message_id") code = data.pop("code") try: result = user_service.verify_code(code=code, phone=phone) if result: user = user_business.get_by_phone(phone=phone) response = {'response': { 'token': create_access_token(identity=user), 'user': json_utility.convert_to_json(user.to_mongo())}} return jsonify(response), 200 except Error as e: print("e.args[0]", e.args[0]) return jsonify({ "response": { "error": e.args[0] } }), 400
def get_model(model_id): try: model = model_business.get_by_model_id(ObjectId(model_id)) model = json_utility.convert_to_json(model.to_mongo()) except Exception as e: return jsonify({'response': '%s: %s' % (str(Exception), e.args)}), 400 return jsonify({'response': model})
def log_batch_end(*args, **kw): global epoch args = list(args) args = json_utility.convert_to_json(args) batch = 'Epoch: {}, Batch: {}'.format(epoch, args[0]) project_id = args[-1] emit_message({"batch": batch}, project_id, **kw)
def upload_file(): data = dict(request.form.items()) user_ID = get_jwt_identity() data_set_name = data.pop('data_set_name') is_private = data.pop('if_private') description = data.pop('description') ds_type = data.pop('type') names = data.pop('names', None) # convert string to bool is_private = str(is_private).lower() == 'true' if request.method == 'POST': # check if the post request has the file part if REQUEST_FILE_NAME not in request.files: return jsonify({'response': 'no file part'}), 400 file = request.files[REQUEST_FILE_NAME] if file.filename == '': return jsonify({'response': 'no selected file'}), 400 if file and file_service.allowed_file(file.filename): url_base = PREFIX + UPLOAD_URL saved_file = file_service.add_file(data_set_name, file, url_base, user_ID, is_private, description, ds_type, names=names, **data) file_json = json_utility.convert_to_json(saved_file.to_mongo()) return jsonify({'response': file_json}) else: return jsonify({'response': 'file is not allowed'}), 400
def emit_deploy_project(user_ID, project_id, state, version): msg = json_utility.convert_to_json({ 'user_ID': user_ID, 'project_id': project_id, 'state': state, 'version': version, }) socketio.emit('deploy_project', msg, namespace='/log/%s' % user_ID)
def add_used_module(app_id): data = request.get_json() used_modules = data.get('used_modules', []) func = data.get('func') version = data.get('version') print(app_id) app = AppService.add_used_module(app_id, used_modules, func, version) return jsonify({"response": json_utility.convert_to_json(app.to_mongo())})
def update_user_request_star(): data = request.get_json() user_request_id = data["user_request_id"] star_user_id = data["star_user_id"] result = user_service.update_request_star(user_request_id, star_user_id) result = json_utility.convert_to_json(result.to_mongo()) print('update_user_request_star') return jsonify({'response': result}), 200
def convert_to_json(data): """ Convert python object into JSON format :param data: :return: data in JSON format """ return json_utility.convert_to_json(data)
def publish_module(project_id, version): data = request.get_json() commit_msg = data.get('commit_msg') project = ModuleService.publish(project_id=project_id, commit_msg=commit_msg, version=version) project = json_utility.convert_to_json(project.to_mongo()) return jsonify({"response": project})
def update_request_answer_votes(): data = request.get_json() request_answer_id = data["request_answer_id"] votes_user_id = data["votes_user_id"] result = user_service.update_answer_vote(request_answer_id, votes_user_id) result = json_utility.convert_to_json(result) print('update_request_answer_votes') return jsonify({'response': result}), 200
def emit_world_message(world): if hasattr(world, "sender") and world.sender: world.sender_user_ID = world.sender.user_ID else: world.sender_user_ID = "system" world_message = json_utility.convert_to_json(world.to_mongo()) socketio.emit('world', world_message, namespace='/log')