Exemplo n.º 1
0
def reg_user():
    email = request.form['email']
    password = request.form['password']

    md5 = hashlib.md5()
    md5.update(password.encode("UTF-8"))

    user = User.query.filter_by(email=email).first()
    if not user:
        new_user = User(email, md5.hexdigest())
        try:
            db.session.add(new_user)
            db.session.commit()

            auth_token = new_user.encode_auth_token()
            if auth_token:
                return Response(auth_token, True, 0).to_json()

            return Response("Error generating token", False,
                            ErrorCodes.generateTokenError).to_json()
        except (SQLAlchemyError, DBAPIError) as e:
            return Response.error_json(e, ErrorCodes.internalError)
    else:
        return Response('User already exists', False,
                        ErrorCodes.userAlreadyExists).to_json()
Exemplo n.º 2
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()
Exemplo n.º 3
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()
Exemplo n.º 4
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()
Exemplo n.º 5
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()
Exemplo n.º 6
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()
Exemplo n.º 7
0
def login_user():
    email = request.form['email']
    password = request.form['password']

    md5 = hashlib.md5()
    md5.update(password.encode("UTF-8"))

    try:
        user = User.query.filter_by(email=email).first()
        if user is None or not user.password == md5.hexdigest():
            return Response("Invalid email or password", False,
                            ErrorCodes.userNotFoundError).to_json()
        auth_token = user.encode_auth_token()
        if auth_token:
            return Response(auth_token.decode(), True, 0).to_json()
    except Exception as e:
        print(e)
        return Response.error_json(e, ErrorCodes.internalError)
Exemplo n.º 8
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()
Exemplo n.º 9
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()
Exemplo n.º 10
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()
Exemplo n.º 11
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()
Exemplo n.º 12
0
def add_book():
    name = request.form['name']
    author = request.form['author']
    description = request.form['description']
    url = request.form.get('url')
    coef_love = request.form['coef_love']
    coef_fantastic = request.form['coef_fantastic']
    coef_fantasy = request.form['coef_fantasy']
    coef_detective = request.form['coef_detective']
    coef_adventure = request.form['coef_adventure']
    coef_art = request.form['coef_art']

    try:
        book = Book.query.filter_by(name=name).first()
        if book is None or not book.author == author:
            new_book = Book(name, author, description, url)
            db.session.add(new_book)
            db.session.flush()

            love = Coefficient(new_book.id, LOVE_ID, coef_love)
            fantastic = Coefficient(new_book.id, FANTASTIC_ID, coef_fantastic)
            fantasy = Coefficient(new_book.id, FANTASY_ID, coef_fantasy)
            detective = Coefficient(new_book.id, DETECTIVE_ID, coef_detective)
            adventure = Coefficient(new_book.id, ADVENTURE_ID, coef_adventure)
            art = Coefficient(new_book.id, ART_ID, coef_art)

            db.session.add(love)
            db.session.add(fantastic)
            db.session.add(fantasy)
            db.session.add(detective)
            db.session.add(adventure)
            db.session.add(art)
            db.session.commit()
            return Response.success_json()
        else:
            return Response(book_already_exists_message, False,
                            ErrorCodes.bookAlreadyExists).to_json()
    except (SQLAlchemyError, DBAPIError) as e:
        return Response.error_json(e)
Exemplo n.º 13
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()