def get(self): """ Search videos by keyword """ try: req_dict = request.args.to_dict() if 'keyword' not in req_dict: raise RouteError(ErrorCode.ROUTE_INVALID_REQUEST_PARAM) if 'param' not in req_dict: param = 'all' else: param = req_dict['param'] if param == 'all': search_result = service_search_video(title=req_dict['keyword'], ignore_case=True, slice=True) search_result.extend( service_search_video(channel=req_dict['keyword'], ignore_case=True, slice=True)) search_result.extend( service_search_video(description=req_dict['keyword'], ignore_case=True, slice=True)) search_result.extend( service_search_video(category=req_dict['keyword'], ignore_case=True)) search_result.extend( service_search_video(tag=req_dict['keyword'], ignore_case=True)) search_result = list({v['video_id']: v for v in search_result}.values()) elif param == 'title' or param == 'video_title': search_result = service_search_video(title=req_dict['keyword'], ignore_case=True, slice=True) elif param == 'channel' or param == 'video_channel': search_result = service_search_video( channel=req_dict['keyword'], ignore_case=True, slice=True) elif param == 'description' or param == 'video_descripton': search_result = service_search_video( description=req_dict['keyword'], ignore_case=True, slice=True) elif param == 'category' or param == 'video_category': search_result = service_search_video( category=req_dict['keyword'], ignore_case=True) elif param == 'tag' or param == 'video_tag': search_result = service_search_video(tag=req_dict['keyword'], ignore_case=True) else: raise RouteError(ErrorCode.ROUTE_INVALID_REQUEST_PARAM) return util_serializer_api_response( 200, body=service_auth_hide_video(get_jwt_identity(), search_result), msg="Search video successfully") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def post(self): """ AWS update video info """ try: kw = util_serializer_request(request) # check authority if 'aws_auth_key' not in kw: raise RouteError(ErrorCode.ROUTE_TOKEN_REQUIRED) if kw['aws_auth_key'] != conf.AWS_AUTH_KEY: raise RouteError(ErrorCode.ROUTE_TOKEN_NOT_PERMITTED) if 'video_id' not in kw: raise RouteError(ErrorCode.ROUTE_VIDEO_ID_REQUIRED) update_result = service_video_update(video_id=kw['video_id'], video_raw_status="streaming") return_body = util_serializer_mongo_results_to_array(update_result, format="json") return util_serializer_api_response( 200, body=return_body, msg="Successfully updated video") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def put(self, video_id, user_id): """ Update a video watching process """ try: kw = util_serializer_request(request) video_id = request.url.split('/')[-3] user_id = request.url.split('/')[-1] token = get_jwt_identity() # check authority if not service_auth_video_op_modify(token, user_id): raise RouteError(ErrorCode.ROUTE_TOKEN_REQUIRED) kw['video_id'] = video_id kw['user_id'] = user_id process_result = service_video_op_update_process(**kw) return util_serializer_api_response( 200, body=process_result, msg="Successfully update video process") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def get(self, user_id): """ Get user information by id """ try: user_id = request.url.split('/')[-1] token = get_jwt_identity() result = {} user = service_user_get_user(user_id=user_id) vid = service_video_get_by_user(user_id=user_id) op = service_video_op_get_by_user(user_id=user_id) # remove deleted video video = [] for v in vid: if v['video_status'] != 'deleted': video.append(v) result['user'] = user result['video'] = video result['video_op'] = op if not service_auth_user_get(token, user_id): if user['user_status'] == 'closed': raise RouteError(ErrorCode.ROUTE_DELETED_USER) result['user'] = service_user_hide_private(user) result['video'] = service_auth_hide_video('', video) result['video_op'] = [] return util_serializer_api_response( 200, body=result, msg="Get user info successfully") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def get(self): try: req_dict = request.args.to_dict() if 'keyword' not in req_dict: raise RouteError(ErrorCode.ROUTE_INVALID_REQUEST_PARAM) if req_dict['keyword'] == 'video_upload_time' or \ req_dict['keyword'] == 'upload_time' or \ req_dict['keyword'] == 'time': keyword = 'video_upload_time' elif req_dict['keyword'] == 'video_like' or \ req_dict['keyword'] == 'video_likes' or \ req_dict['keyword'] == 'like' or \ req_dict['keyword'] == 'likes': keyword = 'video_like' elif req_dict['keyword'] == 'video_share' or \ req_dict['keyword'] == 'video_shares' or \ req_dict['keyword'] == 'share' or \ req_dict['keyword'] == 'shares': keyword = 'video_share' elif req_dict['keyword'] == 'video_star' or \ req_dict['keyword'] == 'video_stars' or \ req_dict['keyword'] == 'star' or \ req_dict['keyword'] == 'stars': keyword = 'video_star' elif req_dict['keyword'] == 'video_view' or \ req_dict['keyword'] == 'video_views' or \ req_dict['keyword'] == 'view' or \ req_dict['keyword'] == 'views': keyword = 'video_view' elif req_dict['keyword'] == 'video_duration' or \ req_dict['keyword'] == 'duration': keyword = 'video_duration' else: raise RouteError(ErrorCode.ROUTE_INVALID_REQUEST_PARAM) search_dict = [{"$sort": {keyword: -1}}, {"$limit": 100}] search_result = service_search_video(aggregate=True, search_dict=search_dict) if keyword == 'video_upload_time': search_result.reverse() return util_serializer_api_response( 200, body=service_auth_hide_video(get_jwt_identity(), search_result), msg="Search video successfully") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def test_route_error(self): # Raise normal ServiceError successfully with self.assertRaises(RouteError) as e: raise RouteError(ErrorCode.ROUTE_INVALID_REQUEST_PARAM) self.assertEqual(e.exception.error_code, ErrorCode.ROUTE_INVALID_REQUEST_PARAM) # Wrong ServiceError Param with self.assertRaises(RouteError) as e: RouteError(4000) self.assertEqual(e.exception.error_code, ErrorCode.ERR_INCORRECT_CODE) self.assertEqual(e.__str__(), e.__str__()) # Get code & msg with self.assertRaises(RouteError) as e: raise RouteError(ErrorCode.ROUTE_INVALID_REQUEST_PARAM) self.assertEqual(e.exception.get_code(), ErrorCode.ROUTE_INVALID_REQUEST_PARAM.get_code()) self.assertEqual(e.exception.get_msg(), ErrorCode.ROUTE_INVALID_REQUEST_PARAM.get_msg())
def get(self, video_id): """ Get video information by video ID """ try: video_id = request.url.split('/')[-1] token = get_jwt_identity() video = service_video_info(video_id=request.url.split('/')[-1]) # remove deleted video if video['video_status'] == 'deleted': raise RouteError(ErrorCode.ROUTE_DELETED_VIDEO) # check authority if service_auth_video_get(token, video_id) is False: raise RouteError(ErrorCode.ROUTE_PRIVATE_VIDEO) return util_serializer_api_response( 200, body=video, msg="Successfully got video by ID.") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def put(self, user_id): """ Update user information by id """ try: kw = util_serializer_request(request) kw['user_id'] = user_id print(kw) if not service_auth_user_modify(get_jwt_identity(), kw['user_id']): raise RouteError(ErrorCode.ROUTE_TOKEN_REQUIRED) result = service_user_update_info(**kw) return util_serializer_api_response( 200, body=result, msg="Update user info successfully") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def get(self, video_id): """ Get a list of like by video id """ try: video_id = request.url.split('/')[-2] token = get_jwt_identity() # check authority if service_auth_video_get(token, video_id) is False: raise RouteError(ErrorCode.ROUTE_PRIVATE_VIDEO) like_result = service_video_likes(video_id=video_id) return util_serializer_api_response( 200, body=like_result, msg="Successfully got video likes") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def delete(self, user_id): """ Delete user by id """ try: token = get_jwt_identity() if not service_auth_user_modify(token, user_id=user_id): raise RouteError(ErrorCode.ROUTE_TOKEN_REQUIRED) result = service_user_close( method='status', user_id=user_id, ) return util_serializer_api_response(200, body=result, msg="Delete user successfully") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def delete(self, video_id): """ Delete video information by video ID """ try: video_id = request.url.split('/')[-1] token = get_jwt_identity() # check authority if service_auth_video_modify(token, video_id) is False: raise RouteError(ErrorCode.ROUTE_TOKEN_NOT_PERMITTED) service_video_delete(method='status', video_id=video_id) return util_serializer_api_response( 200, msg="Successfully deleted video") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def post(self, video_id, user_id): """ Post a comment by specified video id and user id """ try: kw = util_serializer_request(request) video_id = request.url.split('/')[-3] user_id = request.url.split('/')[-1] token = get_jwt_identity() # check authority if not service_auth_video_op_post(token, user_id, video_id): raise RouteError(ErrorCode.ROUTE_TOKEN_REQUIRED) kw['video_id'] = video_id kw['user_id'] = user_id comments_result = service_video_op_add_comment(**kw) return util_serializer_api_response( 200, body=comments_result, msg="Successfully post a comment") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def get(self, video_id, user_id): """ Get a new video watching process """ try: video_id = request.url.split('/')[-3] user_id = request.url.split('/')[-1] token = get_jwt_identity() # check authority if not service_auth_video_op_get(token, user_id, video_id): raise RouteError(ErrorCode.ROUTE_TOKEN_REQUIRED) process_result = service_video_op_get_process(video_id=video_id, user_id=user_id) return util_serializer_api_response( 200, body=process_result, msg="Successfully get video process") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def get(self, video_id): """ Get video view comments list by video ID """ # TODO # print("get user name", get_jwt_identity()) try: video_id = request.url.split('/')[-2] token = get_jwt_identity() # check authority if service_auth_video_get(token, video_id) is False: raise RouteError(ErrorCode.ROUTE_PRIVATE_VIDEO) comments_result = service_video_comments(video_id=video_id) return util_serializer_api_response( 200, body=comments_result, msg="Successfully got video comments") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def delete(self, video_id, user_id): """ Undo a star by specified user and video """ try: video_id = request.url.split('/')[-3] user_id = request.url.split('/')[-1] token = get_jwt_identity() # check authority if not service_auth_video_op_modify(token, user_id): raise RouteError(ErrorCode.ROUTE_TOKEN_REQUIRED) star_result = service_video_op_cancel_star(user_id=user_id, video_id=video_id) return util_serializer_api_response( 200, body=star_result, msg="Successfully cancel a star") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def post(self, video_id, user_id): """ Post a dislike by specified user and video """ try: video_id = request.url.split('/')[-3] user_id = request.url.split('/')[-1] token = get_jwt_identity() # check authority if not service_auth_video_op_post(token, user_id, video_id): raise RouteError(ErrorCode.ROUTE_TOKEN_REQUIRED) dislike_result = service_video_op_add_dislike(video_id=video_id, user_id=user_id) return util_serializer_api_response( 200, body=dislike_result, msg="Successfully post a dislike") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def get(self, video_id, user_id): """ Get a comment by specified video id and user id """ try: video_id = request.url.split('/')[-3] user_id = request.url.split('/')[-1] token = get_jwt_identity() # check authority if not service_auth_video_op_get(token, user_id, video_id): raise RouteError(ErrorCode.ROUTE_TOKEN_REQUIRED) comments_result = service_video_op_get_comment(video_id=video_id, user_id=user_id) return util_serializer_api_response( 200, body=comments_result, msg="Successfully get comments of the user") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def put(self, video_id): """ Update video information by video ID """ try: kw = util_serializer_request(request) video_id = request.url.split('/')[-1] token = get_jwt_identity() # check authority if not service_auth_video_modify(token, video_id): raise RouteError(ErrorCode.ROUTE_TOKEN_NOT_PERMITTED) kw["video_id"] = video_id update_result = service_video_update(**kw) return_body = util_serializer_mongo_results_to_array(update_result, format="json") return util_serializer_api_response( 200, body=return_body, msg="Successfully updated video") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)
def get(self): """ Search users by keyword """ # TODO try: req_dict = request.args.to_dict() if 'keyword' not in req_dict: raise RouteError(ErrorCode.ROUTE_INVALID_REQUEST_PARAM) if 'param' not in req_dict: param = 'all' else: param = req_dict['param'] if param == 'all': search_result = service_search_user(name=req_dict['keyword'], ignore_case=True) search_result.extend( service_search_user(email=req_dict['keyword'], ignore_case=True)) search_result.extend( service_search_user(first_name=req_dict['keyword'], ignore_case=True)) search_result.extend( service_search_user(last_name=req_dict['keyword'], ignore_case=True)) search_result.extend( service_search_user(phone=req_dict['keyword'], ignore_case=True)) search_result.extend( service_search_user(street1=req_dict['keyword'], ignore_case=True)) search_result.extend( service_search_user(street2=req_dict['keyword'], ignore_case=True)) search_result.extend( service_search_user(city=req_dict['keyword'], ignore_case=True)) search_result.extend( service_search_user(state=req_dict['keyword'], ignore_case=True)) search_result.extend( service_search_user(country=req_dict['keyword'], ignore_case=True)) search_result.extend( service_search_user(zip=req_dict['keyword'], ignore_case=True)) search_result.extend( service_search_user(status=req_dict['keyword'], ignore_case=True)) search_result = list({v['user_id']: v for v in search_result}.values()) elif param == 'name' or param == 'user_name': search_result = service_search_user(name=req_dict['keyword'], ignore_case=True) elif param == 'email' or param == 'user_email': search_result = service_search_user(email=req_dict['keyword'], ignore_case=True) elif param == 'first_name' or param == 'user_first_name': search_result = service_search_user( first_name=req_dict['keyword'], ignore_case=True) elif param == 'last_name' or param == 'user_last_name': search_result = service_search_user( last_name=req_dict['keyword'], ignore_case=True) elif param == 'street1' or param == 'user_street1': search_result = service_search_user( street1=req_dict['keyword'], ignore_case=True) elif param == 'street2' or param == 'user_street2': search_result = service_search_user( street2=req_dict['keyword'], ignore_case=True) elif param == 'city' or param == 'user_city': search_result = service_search_user(city=req_dict['keyword'], ignore_case=True) elif param == 'state' or param == 'user_state': search_result = service_search_user(state=req_dict['keyword'], ignore_case=True) elif param == 'country' or param == 'user_country': search_result = service_search_user( country=req_dict['keyword'], ignore_case=True) elif param == 'zip' or param == 'user_zip': search_result = service_search_user(zip=req_dict['keyword'], ignore_case=True) else: raise RouteError(ErrorCode.ROUTE_INVALID_REQUEST_PARAM) return util_serializer_api_response(200, body=service_auth_hide_user( get_jwt_identity(), search_result), msg="Search user successfully") except (ServiceError, MongoError, RouteError, Exception) as e: return util_error_handler(e)