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()
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()
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()
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()
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)