def service_user_close(**kw): kw['service'] = 'user' kw = util_pattern_format_param(**kw) if 'user_id' not in kw: raise ServiceError(ErrorCode.SERVICE_MISSING_USER_ID) videos = query_video_get_by_user_id(kw['user_id']) ops = query_video_op_get_by_user_id(kw['user_id']) # delete by setting status if 'method' in kw and kw['method'] == 'status': res = query_user_update_status(kw['user_id'], 'closed') for video in videos: service_video_delete(video_id=video.to_dict()['video_id'], method='status') # delete by removing from databse else: res = query_user_delete_by_id(kw['user_id']) for video in videos: service_video_delete(video_id=video.to_dict()['video_id']) # delete all op created by this user immediately for op in ops: query_video_op_delete(op.to_dict()['video_op_id'], silent=True) return res
def service_user_reg(**kw): """ Register user :param kw: keyword :keyword: :key user_name: (required) str :key user_email: (required) str :key user_password: (required) str :key user_ip: (optional) str :return user model: """ # user_name: str, user_email: str, user_password: str, user_ip = "0.0.0.0" # service_user_reg(conf, user_name="t", user_email="k", # user_password="******") kw['service'] = 'user' kw = util_pattern_format_param(**kw) if 'user_name' not in kw or 'user_email' not in kw \ or 'user_password' not in kw: raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM) query_user_create(kw['user_name'], kw['user_email'], util_hash_encode(kw['user_password'])) return query_user_get_by_name(kw['user_name'])[0].to_dict()
def service_search_user(**kw): kw['service'] = 'user' kw = util_pattern_format_param(**kw) # Search configs if 'slice' in kw and kw['slice'] is True: raise ServiceError(ErrorCode.SERVICE_PARAM_SLICE_NOT_SUPPORT) if 'ignore_case' not in kw: kw['ignore_case'] = conf.SEARCH_IGNORE_CASE if 'exact' not in kw: kw['exact'] = conf.SEARCH_EXACT # TODO: add typo allowance, etc. # Search # TODO: Support aggregation pipeline, etc. if kw['ignore_case'] is False or kw['exact'] is True \ or 'pattern' in kw and kw['pattern'] is True: kw = util_pattern_build(**kw) res_search = service_search_user_by_pattern(**kw) elif 'aggregate' in kw and kw['aggregate'] is True: res_search = service_search_user_by_aggregation(**kw) return res_search else: res_search = service_search_user_by_contains(**kw) return util_serializer_mongo_results_to_array(res_search)
def service_video_comments(**kw): kw['service'] = 'video' kw = util_pattern_format_param(**kw) # keyword check and formatting if 'video_id' not in kw: raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM) if not is_valid_id(kw["video_id"]): raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ) # perform db operations and get result search_mongo = query_video_op_get_by_video_id(kw["video_id"]) if len(search_mongo) == 0: return [] search_result = util_serializer_mongo_results_to_array(search_mongo) comments_result = [] for each in search_result: if each["comment"] != "": user_obj = query_user_get_by_id(each["user_id"])[0].to_dict() comments_result.append({ "video_id": each["video_id"], "user_id": each["user_id"], "user_name": user_obj["user_name"], "user_thumbnail": user_obj["user_thumbnail"], "comment": each["comment"], "comment_date": str(each["comment_date"]) }) return comments_result
def service_video_delete(**kw): kw['service'] = 'video' kw = util_pattern_format_param(**kw) # keyword check and formatting if 'video_id' not in kw: raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM) if not is_valid_id(kw['video_id']): raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ) # delete by setting status if 'method' in kw and kw['method'] == 'status': res = query_video_update(kw['video_id'], video_status='deleted') # delete by removing from database else: res = query_video_delete(kw['video_id'], silent=True) # delete all op in this video immediately ops = query_video_op_get_by_video_id(kw['video_id']) for op in ops: opid = op.to_dict()['video_op_id'] query_video_op_delete(opid, silent=True) return res
def service_video_get_by_user(**kw): kw['service'] = 'video' kw = util_pattern_format_param(**kw) # keyword check and formatting if 'user_id' not in kw: raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM) if not is_valid_id(kw["user_id"]): raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ) videos = query_video_get_by_user_id(kw['user_id']) if len(videos) == 0: return [] video_array = util_serializer_mongo_results_to_array(videos) return video_array
def service_search_video(**kw): kw['service'] = 'video' kw = util_pattern_format_param(**kw) # Search configs if 'slice' not in kw: kw['slice'] = conf.SEARCH_SLICE if 'ignore_case' not in kw: kw['ignore_case'] = conf.SEARCH_IGNORE_CASE if 'exact' not in kw: kw['exact'] = conf.SEARCH_EXACT # TODO: add typo allowance, etc. # Search # TODO: Support aggregation pipeline if kw['ignore_case'] is False or kw['exact'] is True or kw['slice']\ is True: kw = util_pattern_slice(**kw) kw = util_pattern_build(**kw) res_search = service_search_video_by_pattern(**kw) elif 'pattern' in kw and kw['pattern'] is True: # Pattern search kw = util_pattern_build(**kw) res_search = service_search_video_by_pattern(**kw) elif 'aggregate' in kw and kw['aggregate'] is True: # Aggregate search res_search = service_search_video_by_aggregation(**kw) for res in res_search: res['video_id'] = str(res['_id']) res.pop('_id') for res in res_search: user = query_user_get_by_id(res['user_id'])[0] res['user_name'] = user.user_name return res_search else: # Contains keyword (single) search res_search = service_search_video_by_contains(**kw) res_array = util_serializer_mongo_results_to_array(res_search) for res in res_array: user = query_user_get_by_id(res['user_id'])[0] res['user_name'] = user.user_name # default format="dict" return res_array
def service_user_update_info(**kw): kw['service'] = 'user' kw = util_pattern_format_param(**kw) if 'user_id' not in kw: raise ServiceError(ErrorCode.SERVICE_MISSING_USER_ID) if 'user_status' in kw: query_user_update_status(kw['user_id'], kw['user_status']) if 'user_name' in kw: query_user_update_name(kw['user_id'], kw['user_name']) if 'user_email' in kw: query_user_update_email(kw['user_id'], kw['user_email']) if 'user_password' in kw: query_user_update_password(kw['user_id'], kw['user_password']) if 'user_thumbnail' in kw: query_user_update_thumbnail(kw['user_id'], kw['user_thumbnail']) query_user_update_details(**kw) return query_user_get_by_id(kw['user_id'])[0].to_dict()
def service_user_login(**kw): kw['service'] = 'user' kw = util_pattern_format_param(**kw) if 'user_name' in kw and 'user_password' in kw: users = query_user_get_by_name(kw['user_name']) if len(users) == 0: raise ServiceError(ErrorCode.SERVICE_USER_NOT_FOUND) user = users[0] if user.to_dict()["user_status"] == "closed": raise ServiceError(ErrorCode.SERVICE_USER_CLOSED) if util_hash_encode(kw['user_password']) != user.user_password: raise ServiceError(ErrorCode.SERVICE_USER_PASS_WRONG) elif 'user_email' in kw and 'user_password' in kw: users = query_user_get_by_email(kw['user_email']) if len(users) == 0: raise ServiceError(ErrorCode.SERVICE_USER_NOT_FOUND) user = users[0] if user.to_dict()["user_status"] == "closed": raise ServiceError(ErrorCode.SERVICE_USER_CLOSED) if util_hash_encode(kw['user_password']) != user.user_password: raise ServiceError(ErrorCode.SERVICE_USER_PASS_WRONG) elif 'user' in kw and 'user_password' in kw: user_names = query_user_get_by_name(kw['user']) user_emails = query_user_get_by_email(kw['user']) if len(user_emails) == 0 and len(user_names) == 0: raise ServiceError(ErrorCode.SERVICE_USER_NOT_FOUND) elif len(user_emails) != 0: user = user_emails[0] elif len(user_names) != 0: user = user_names[0] if user.to_dict()["user_status"] == "closed": raise ServiceError(ErrorCode.SERVICE_USER_CLOSED) if util_hash_encode(kw['user_password']) != user.user_password: raise ServiceError(ErrorCode.SERVICE_USER_PASS_WRONG) else: raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM) uid = user.to_dict()['user_id'] if 'ip' in kw: query_user_add_login(uid, ip=kw['ip']) return query_user_get_by_id(uid)[0].to_dict()
def service_video_info(**kw): kw['service'] = 'video' kw = util_pattern_format_param(**kw) # keyword check and formatting if 'video_id' not in kw: raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM) if not is_valid_id(kw["video_id"]): raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ) # perform db operations and get result video = query_video_get_by_video_id(kw["video_id"]) if len(video) == 0: raise ServiceError(ErrorCode.SERVICE_VIDEO_NOT_FOUND) res = video[0].to_dict() user_id = res["user_id"] user_obj = query_user_get_by_id(user_id)[0].to_dict() res["user_name"] = user_obj["user_name"] res["user_thumbnail"] = user_obj["user_thumbnail"] return res
def service_video_update(**kw): kw['service'] = 'video' kw = util_pattern_format_param(**kw) # keyword check and formatting if 'video_id' not in kw: raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM) if not is_valid_id(kw["video_id"]): raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ) if 'video_status' in kw and kw['video_status'] not in VALID_VIDEO_STATUS: raise ServiceError(ErrorCode.SERVICE_VIDEO_INVALID_STATUS) if 'video_raw_status' in kw and \ kw['video_raw_status'] not in VALID_VIDEO_RAW_STATUS: raise ServiceError(ErrorCode.SERVICE_VIDEO_INVALID_STATUS) query_video_update(**kw) return query_video_get_by_video_id(kw["video_id"])