Example #1
0
def set_mark_null_book(book_id, token):
    try:
        user_id = User.decode_auth_token(token)
        set_mark_book(book_id, user_id, None)
        return Response.success_json()
    except SQLAlchemyError as e:
        return Response.error_json(e)
    except ExpiredSignatureError:
        return Response.expired_token_json()
    except InvalidTokenError:
        return Response.invalid_token_json()
Example #2
0
def get_like_status_book(book_id, token):
    try:
        user_id = User.decode_auth_token(token)
        user_and_book = UsersAndBooks \
            .query \
            .filter(UsersAndBooks.user_id == user_id, UsersAndBooks.book_id == book_id) \
            .first()
        data = {'mark': user_and_book.mark, 'bookID': book_id}
        return jsonify(data)
    except SQLAlchemyError as e:
        return Response.error_json(e)
    except ExpiredSignatureError:
        return Response.expired_token_json()
    except InvalidTokenError:
        return Response.invalid_token_json()
Example #3
0
def set_user_genres(token, genre_id):
    try:
        user_id = User.decode_auth_token(token)
        user_and_genre = UsersAndGenres(user_id, genre_id)
        db.session.add(user_and_genre)
        db.session.commit()
        return Response.success_json()
    except IntegrityError:
        return Response("Genre already exists", False,
                        ErrorCodes.genreAlreadyExists).to_json()
    except SQLAlchemyError as e:
        return Response.error_json(e)
    except ExpiredSignatureError:
        return Response.expired_token_json()
    except InvalidTokenError:
        return Response.invalid_token_json()
Example #4
0
def get_user_books(token):
    try:
        user_id = User.decode_auth_token(token)
        user = User.query.get(user_id)
        user_and_book_list = user.users_and_books

        books = [
            BookWithMarks(book.book, book.mark) for book in user_and_book_list
        ]

        return BookWithMarks.schema.jsonify(books, True)
    except SQLAlchemyError as e:
        return Response.error_json(e)
    except ExpiredSignatureError:
        return Response.expired_token_json()
    except InvalidTokenError:
        return Response.invalid_token_json()
Example #5
0
def add_user_book(book_id, token):
    try:
        user_id = User.decode_auth_token(token)

        users_and_books = UsersAndBooks(user_id, book_id, None)
        db.session.add(users_and_books)
        db.session.commit()
        return Response.success_json()
    except IntegrityError:
        return Response("Book already exists or not found.", False,
                        ErrorCodes.bookAlreadyExists).to_json()
    except SQLAlchemyError as e:
        return Response.error_json(e)
    except ExpiredSignatureError:
        return Response.expired_token_json()
    except InvalidTokenError:
        return Response.invalid_token_json()
Example #6
0
def check_book_for_user(token, book_id):
    try:
        user_id = User.decode_auth_token(token)
        user_and_book = UsersAndBooks\
            .query\
            .filter(UsersAndBooks.book_id == book_id, UsersAndBooks.user_id == user_id)\
            .first()
        if user_and_book is not None:
            data = {'result': True, 'bookID': book_id}
        else:
            data = {'result': False, 'bookID': book_id}
        return jsonify(data)
    except SQLAlchemyError as e:
        return Response.error_json(e)
    except ExpiredSignatureError:
        return Response.expired_token_json()
    except InvalidTokenError:
        return Response.invalid_token_json()
Example #7
0
def check_user_genres(token):
    try:
        user_id = User.decode_auth_token(token)
        user = User.query.get(user_id)
        if len(user.genres) == 0:
            return Response(USER_GENRES_CHECK_RESULT_MESSAGE, False,
                            0).to_json()
        else:
            return Response(USER_GENRES_CHECK_RESULT_MESSAGE, True,
                            0).to_json()
    except IntegrityError:
        return Response("Genre already exists", False,
                        ErrorCodes.genreAlreadyExists).to_json()
    except SQLAlchemyError as e:
        return Response.error_json(e)
    except ExpiredSignatureError:
        return Response.expired_token_json()
    except InvalidTokenError:
        return Response.invalid_token_json()
Example #8
0
def get_genre_recommend_books(token, page=1, coefficient=0.25):
    try:
        user_id = User.decode_auth_token(token)
        user = User.query.get(user_id)
        user_genres = user.genres

        genres_id = [genre.id for genre in user_genres]

        coefficients = Coefficient \
            .query \
            .filter(Coefficient.genre_id.in_(genres_id), Coefficient.value > coefficient) \
            .distinct(Coefficient.book_id).paginate(page, BOOKS_PER_PAGE, False).items
        books = [coefficient.book for coefficient in coefficients]

        return Book.schema.jsonify(books, True)
    except SQLAlchemyError as e:
        return Response.error_json(e)
    except ExpiredSignatureError:
        return Response.expired_token_json()
    except InvalidTokenError:
        return Response.invalid_token_json()
Example #9
0
def delete_user_book(token, book_id):
    try:
        user_id = User.decode_auth_token(token)
        user_and_book = UsersAndBooks\
            .query\
            .filter(UsersAndBooks.user_id == user_id, UsersAndBooks.book_id == book_id)\
            .first()
        if user_and_book is not None:
            UsersAndBooks.query.filter(
                UsersAndBooks.user_id == user_id,
                UsersAndBooks.book_id == book_id).delete()
            db.session.commit()
            return Response.success_json()
        else:
            return Response("Book not found", False,
                            ErrorCodes.bookNotFound).to_json()
    except SQLAlchemyError as e:
        return Response.error_json(e)
    except ExpiredSignatureError:
        return Response.expired_token_json()
    except InvalidTokenError:
        return Response.invalid_token_json()
Example #10
0
def get_user_recommend_books(token):
    try:
        user_id = User.decode_auth_token(token)
        user = User.query.get(user_id)
        user_books_id = [book.id for book in user.books]

        recommend_like_books = get_recommend_books(True, user_id,
                                                   user_books_id)
        recommend_dislike_books = get_recommend_books(False, user_id,
                                                      user_books_id)

        recommend_books = [
            book for book in recommend_like_books
            if book not in recommend_dislike_books
        ]

        return Book.schema.jsonify(recommend_books, True)
    except SQLAlchemyError as e:
        return Response.error_json(e)
    except ExpiredSignatureError:
        return Response.expired_token_json()
    except InvalidTokenError:
        return Response.invalid_token_json()