def delete(self): # Get user's id from token token = request.headers.get('AUTH-TOKEN') token_info = jwt.decode(token, SECRET_KEY, algorithms='HS256') user_id = token_info['id'] # Get collection_id from parser args = collection_delete_parser.parse_args() collection_id = args.get('collection_id') # If collection existed if not Collection.is_collection_exists_by_both_id( user_id, collection_id): return {'message': 'Resource not found'}, 404 # Read History and Main collection cannot be deleted read_collection_id = Collection.get_readcollection_id(user_id) main_collection_id = read_collection_id - 1 if collection_id == read_collection_id or collection_id == main_collection_id: return { 'message': 'Read History and Main collection cannot be deleted' }, 400 try: Collection.delete_collection(collection_id) return {'message': 'Delete collection successfully'}, 200 except pymysql.Error as e: return {'message': e.args[1]}, 500
def put(self): # Get collection_id and book_id from parser token = request.headers.get('AUTH-TOKEN') token_info = jwt.decode(token, SECRET_KEY, algorithms='HS256') user_id = token_info['id'] # Get args from parser args = collection_move_parser.parse_args() new_collection_id = args.get('new_collection_id') old_collection_id = args.get('old_collection_id') book_id = args.get('book_id') if not (Collection.is_collection_exists_by_both_id( user_id, new_collection_id) and Collection.is_collection_exists_by_both_id( user_id, old_collection_id)): return {'message': 'Resource not found'}, 404 if not Book.is_book_exists_by_id(book_id): return {'message': 'Resource not found'}, 404 if not Book.is_book_exists_in_collection(old_collection_id, book_id): return {'message': 'Resource not found'}, 404 if Book.is_book_exists_in_collection(new_collection_id, book_id): return { 'message': 'This book already existed in the collection you want to move to' }, 401 if old_collection_id == Collection.get_readcollection_id( user_id ) or new_collection_id == Collection.get_readcollection_id(user_id): return { 'message': 'You cannot move in or out books in Read collection' }, 401 try: collection = Collection(old_collection_id) collection.move_book_to_another_collection(new_collection_id, book_id) return { 'message': 'Move book to another collection successfully' }, 200 except pymysql.Error as e: return {'message': e.args[1]}, 500
def get(self, user_id): # is user existed if not User.is_user_exists_by_id(user_id): return {'message': 'Resource not found'}, 404 # Get each tag number collection_num = Collection.get_num_collection(user_id) readhistory_num = Collection.get_num_read_collection( user_id, Collection.get_readcollection_id(user_id)) myreviews_num = Review.get_user_num_review(user_id) target, finish_book, finish_num, finish_flag = Goal.get_goal_record( user_id, int(datetime.now().year), int(datetime.now().month)) # Format finish ratio if target != 0: finish_ratio = "%.2f%%" % (float(finish_num) / float(target) * 100) else: finish_ratio = "--" return { 'collections_num': collection_num, 'ReadHistory_num': readhistory_num, 'MonthlyGoal_num': finish_ratio, 'MyReview_num': myreviews_num }, 200
def put(self): # Get user's id from token token = request.headers.get('AUTH-TOKEN') token_info = jwt.decode(token, SECRET_KEY, algorithms='HS256') user_id = token_info['id'] # Get new_name and collection_id from parser args = collection_update_name_parser.parse_args() new_name = args.get('new_name') collection_id = args.get('collection_id') # Cannot update read history and main collection's name read_collection_id = Collection.get_readcollection_id(user_id) main_collection_id = read_collection_id - 1 if collection_id == read_collection_id or collection_id == main_collection_id: return { 'message': "Read History and Main collection's name cannot be changed" }, 201 # Name input cannot be empty if new_name == "": return {'message': "Collection's name cannot be empty"}, 401 # Is collection existed if not Collection.is_collection_exists_by_both_id( user_id, collection_id): return {'message': "Resource not found"}, 404 try: collection = Collection(collection_id) flag, message = collection.update_collection_name( user_id, new_name) if not flag: return {'message': message}, 401 else: return {'message': message}, 200 except pymysql.Error as e: return {'message': e.args[1]}, 500