def add_book_to_list_handler(book_info, list_type, session_storage, res): try: # If all data is correct if book_info['book_name'] and book_info[ 'book_author'] and list_type != -1: # Getting user user = User.query.filter_by( alice_id=session_storage['alice_id']).first() # Getting book book = Book.query.filter_by( title=book_info['book_name'], author=book_info['book_author']).first() # If book already in some list if book: response_text = 'Book already in \"{}\" list!'.format( ['Wish', 'Now reading', 'Already read'][book.status]) res['response']['text'] = response_text # If book not in lists else: books = list(Book.query.filter_by(status=list_type)) # If list consists from 5 books if len(books) >= 5: res['response'][ 'text'] = 'You can\'t add more than 5 books in one list!' # If list consists from less than 5 books else: Book.add(user, book_info['book_name'], book_info['book_author'], list_type) res['response'][ 'text'] = 'Book has been added!' + '\n\n\nWhat else I can do for you, {}?'.format( session_storage['username']) except Exception as e: print(e)
def books(): book_edit_form_errors = None book_edit_forms = [] # generate book edit forms for book in Book.query.all(): book_edit_form = BookEditForm() writers = Writer.query.all() book_edit_form.writers.data = [writer.id for writer in book.writers] book_edit_form.title.data = book.title book_edit_form.id.data = book.id book_edit_forms.append(book_edit_form) if request.method == 'GET': book_add_form = BookAddForm() else: if request.form['btn'] == 'Edit': book_add_form = BookAddForm() book_edit_form_errors = BookEditForm(request.form) if book_edit_form_errors.validate(): writers = Writer.query.filter(Writer.id.in_(book_edit_form_errors.writers.data)).all() book = Book.query.filter(Book.id == book_edit_form_errors.id.data).first() book.edit(title=book_edit_form_errors.title.data, writers=writers) return redirect(url_for('books')) else: book_add_form = BookAddForm(request.form) if book_add_form.validate(): writer_ids = [writer.id for writer in book_add_form.writers.data] writers = Writer.query.filter(Writer.id.in_(writer_ids)).all() Book.add(title=book_add_form.title.data, writers=writers) return redirect(url_for('books')) return render_template('books.html', book_add_form=book_add_form, book_edit_forms=book_edit_forms, book_edit_form_errors=book_edit_form_errors)
def add_books_to_db(fin=books_json): from PyQt4 import QtCore from models import Book, Cat with open(fin, 'r') as books_file: data = books_file.read() data = json.loads(data) cats = data.keys() for c in cats: cat = Cat(name=c) cat.put() cat = Cat.get(name=c)[0] cat_id = cat.id for book in data[c]: title = book[0] cat_order = book[1] if cat_order.isdigit(): b = Book.add(title=title, cat_id=cat_id, cat_order=int(cat_order), copies=1, available=True) if type(b) in [str, QtCore.QString]: print b, title, cat_order