def sell(): if request.method == 'GET': return render_template('sell.html', title='Продажа книги') elif request.method == 'POST': try: if request.form['accept'] == 'on': book = Books() book.title = request.form['title'] book.description = request.form['description'] book.cost = request.form['cost'] book.amount = request.form['amount'] book.genre = request.form['genre'] book.user_id = current_user.id session = db_session.create_session() session.add(book) session.commit() book.image = str(random.random()) + "-" + str( random.randint(1, 9999999)) + "-" + book.created_date img = request.files['photo'].read() out = open("static/img/" + book.image + ".jpg", "wb") out.write(img) out.close session.commit() return redirect('/') except Exception: return redirect('/sell')
def add_books(): form = BooksForm() if form.validate_on_submit(): session = db_session.create_session() books = Books() books.title = form.title.data books.author = form.author.data books.genre = form.genre.data current_user.books.append(books) session.merge(current_user) session.commit() return redirect('/') return render_template('book.html', title='Добавление книги', form=form)