Пример #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
Пример #2
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
Пример #3
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
Пример #4
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