Beispiel #1
0
    def post(self):
        # Get user_id from token
        token = request.headers.get('AUTH-TOKEN')
        token_info = jwt.decode(token, SECRET_KEY, algorithms='HS256')
        user_id = token_info['id']

        # Get book_id from parser
        args = read_parser.parse_args()
        book_id = args.get('book_id')

        # Get current year and month
        now_year = int(datetime.datetime.now().strftime("%Y"))
        now_month = int(datetime.datetime.now().strftime("%m"))

        if args.get('year') > now_year or args.get('year') < 1900:
            return {'message': 'Invalid year'}, 401
        if args.get('month') > 12 or args.get('month') < 1:
            return {'message': 'Invalid month'}, 401
        if args.get('year') == now_year and args.get('month') > now_month:
            return {'message': 'Invalid month'}, 401
        date = str(args.get('year')) + "-" + str(args.get('month'))
        if Collection.is_book_read(user_id, book_id):
            return {'message': 'This book is already been marked as read'}
        if not Book.is_book_exists_by_id(book_id):
            return {'message': 'Resource not found'}, 404
        try:
            Collection.mark_as_read(user_id, book_id, date)
        except pymysql.Error as e:
            return {'message': e.args[1]}, 500
        return {'message': 'Mark successfully'}, 200
    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 get(self):
        # Get collection_id from parser
        args = collection_get_book_parser.parse_args()
        collection_id = args.get('collection_id')

        # Is collection exist
        if not Collection.is_collection_exists_by_id(collection_id):
            return {'message': 'Resource not found'}, 404
        collection = Collection(collection_id)
        books = collection.get_book_in_collection()
        return {'books': books}, 200
Beispiel #4
0
 def post(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 book_id and content from json input
     info = request.json
     book_id = info['book_id']
     rating = info['rating']
     content = info['content']
     if not Book.is_book_exists_by_id(book_id):
         return {'message': 'Resource not found'}, 404
     if not Collection.is_book_read(user_id, book_id):
         return {
             'message':
             'You can only review and rate after you read the book'
         }, 401
     # input cannot be empty string
     if book_id is None or rating is None or content == "":
         return {'message': 'Rating or review content cannot be empty'}, 401
     try:
         if Review.new_review(user_id, book_id, rating, content):
             return {'message': 'Post new review successfuly'}, 200
         else:
             return {'message': 'Review already existed'}, 401
     except pymysql.Error as e:
         return {'message': e.args[1]}, 500
    def post(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_name from parser
        args = collection_name_parser.parse_args()
        name = args.get('collection_name')

        # Connot set collection's name as "Main Collection" or "Read"
        if name == "Main collection" or name == "Read":
            return {
                'message':
                "Collection's name cannot be 'Main Collection' or 'Read'"
            }, 400

        # Name input cannot be empty string
        if name == "":
            return {'message': "Collection's name cannot be empty"}, 400
        try:
            if Collection.post_new_collection(user_id, name):
                return {'message': 'Create new collection successfully'}, 200
            else:
                return {'message': 'This collection already exist'}, 400
        except pymysql.Error as e:
            return {'message': e.args[1]}, 500
 def get(self):
     # Get collection_id from parser
     args = collection_user_id_parser.parse_args()
     user_id = args.get('user_id')
     if not User.is_user_exists_by_id(user_id):
         return {'message': 'Resource not found'}, 404
     result = Collection.get_recent_added_books(user_id)
     return {'books': result}, 200
 def get(self):
     # Get collection_id from parser
     args = collection_user_id_parser.parse_args()
     user_id = args.get('user_id')
     if not User.is_user_exists_by_id(user_id):
         return {'message': 'Resource not found'}, 404
     books = Collection.get_read_history(user_id)
     return {'books': books}, 200
 def get(self):
     # Get user_id from parser
     args = collection_user_id_parser.parse_args()
     user_id = args.get('user_id')
     result = Collection.get_user_collection(user_id)
     if result == None:
         return {'message': 'Resource not found'}, 404
     return {'Collections': result}, 200
Beispiel #9
0
    def post(self):
        # Get user_id from token
        token = request.headers.get('AUTH-TOKEN')
        token_info = jwt.decode(token, SECRET_KEY, algorithms='HS256')
        user_id = token_info['id']

        # Get book_id from parser
        args = read_id_parser.parse_args()
        book_id = args.get('book_id')
        if not Book.is_book_exists_by_id(book_id):
            return {'message': 'Resource not found'}, 404
        if not Collection.is_book_read(user_id, book_id):
            return {'message': 'This book is not been marked as read yet'}
        try:
            Collection.mark_as_unread(user_id, book_id)
            if Review.is_review_exist_by_both_id(user_id, book_id):
                Review.delete_review(user_id, book_id)
        except pymysql.Error as e:
            return {'message': e.args[1]}, 500
        return {'message': 'Mark successfully'}, 200
    def delete(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 collection_id and book_id from parser
        args = collection_add_book_parser.parse_args()
        collection_id = args.get('collection_id')
        book_id = args.get('book_id')

        if not Collection.is_collection_exists_by_both_id(
                user_id, collection_id):
            return {'message': 'Resource not found'}, 404
        if not Book.is_book_exists_by_id(book_id):
            return {'message': 'Resource not found'}, 404
        collection = Collection(collection_id)
        if collection.delete_book_in_collection(args.get('book_id')):
            return {'message': 'Delete book successfully'}, 200
        else:
            return {'message': 'Resource not found'}, 404
    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 post(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_add_book_parser.parse_args()
        collection_id = args.get('collection_id')
        book_id = args.get('book_id')

        # Check user is adding book to their own collections
        if not Collection.is_collection_exists_by_both_id(
                user_id, collection_id):
            return {'message': 'Resource not found'}, 404

        # Check if book existed
        if not Book.is_book_exists_by_id(book_id):
            return {'message': 'Resource not found'}, 404
        collection = Collection(collection_id)
        flag, message = collection.add_book_to_collection(args.get('book_id'))
        return {'message': message}, flag
Beispiel #13
0
 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
Beispiel #14
0
 def get(self):
     # Get user_id from token
     token = request.headers.get('AUTH-TOKEN')
     token_info = jwt.decode(token, SECRET_KEY, algorithms='HS256')
     user_id = token_info['id']
     # Get book_id from parser
     args = read_id_parser.parse_args()
     book_id = args.get('book_id')
     if not Book.is_book_exists_by_id(book_id):
         return {'message': 'Resource not found'}, 404
     read_flag = Collection.is_book_read(user_id, book_id)
     review_flag = Review.is_review_exist_by_both_id(user_id, book_id)
     return {'read': read_flag, 'review': review_flag}, 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
    def get(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 info from parser
        args = collection_copy_parser.parse_args()
        collection_id = args.get('collection_id')
        new_collection_name = args.get('new_collection_name')

        # Target collection existed check
        if not Collection.is_collection_exists_by_id(collection_id):
            return {'message': 'Resource not found'}, 404
        if Collection.is_collection_exists_by_both_id(user_id, collection_id):
            return {'message': 'You cannot copy your own collection'}, 201
        collection = Collection(collection_id)
        collection_name = collection.get_collection_name()

        # Target collection has same name with certain collection owned by user
        if (Collection.is_collection_exists_by_name(
                user_id, collection_name)) and (new_collection_name is None):
            return {
                'message': 'You already has a collection with same name.'
            }, 201
        if not new_collection_name is None:
            if Collection.is_collection_exists_by_name(user_id,
                                                       new_collection_name):
                return {
                    'message': 'You already has a collection with same name'
                }, 201
        else:
            new_collection_name = collection_name
        Collection.post_new_collection(user_id, new_collection_name)
        Collection.copy_collection(
            collection_id,
            Collection.get_collection_id_by_name(user_id, new_collection_name))
        return {'message': 'Copy collection successfully'}, 200
Beispiel #17
0
 def get_goal_record(user_id, year, month):
     # SQL
     conn = connect_sys_db()
     query = "SELECT goal FROM monthly_goal WHERE (user_id = \'{user_id}\' AND year = \'{year}\' AND month = \'{month}\')".format(
         user_id=user_id,
         year=year,
         month=month,
     )
     db_result = read_sql(sql=query, con=conn)
     if db_result.empty:
         target = 0
     else:
         target = int(db_result.iloc[0].goal)
     # Get all book read by user in that month
     finish_book = Collection.get_read_history_by_date(user_id, year, month)
     finish_num = len(finish_book)
     if finish_num >= target:
         finish_flag = True
     else:
         finish_flag = False
     return target, finish_book, finish_num, finish_flag
Beispiel #18
0
 def get(self, user_id):
     # Is user existed
     if not User.is_user_exists_by_id(user_id):
         return {'message': "Resource not found"}, 404
     result = Collection.get_user_collection(user_id)
     return {'list': result}, 200