Example #1
0
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')
Example #2
0
def add_book():
    form = BookForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        book = Books()
        book.title = form.title.data
        book.pic_url = form.pic_url.data
        book.author = form.author.data
        book.content = form.content.data
        book.file_name = form.file.data.filename
        book.marked_file_name = f"marked_{form.file.data.filename}"
        form.file.data.save(os.path.join(
            app.auto_find_instance_path(), form.file.data.filename))
        current_user.books.append(book)
        db_sess.merge(current_user)
        db_sess.commit()
        book_load(os.path.join(
            app.auto_find_instance_path(), form.file.data.filename), f'{book.file_name}')
        path = os.path.join(
            app.auto_find_instance_path(), form.file.data.filename)
        path2 = os.path.join(
            app.auto_find_instance_path(), f'{book.marked_file_name}.csv')
        if marking(book.file_name, path, path2):
            book_load(os.path.join(
                app.auto_find_instance_path(), f'{book.marked_file_name}.csv'), f'{book.marked_file_name}.csv')
        os.remove(os.path.join(
            app.auto_find_instance_path(), book.file_name))
        os.remove(os.path.join(
            app.auto_find_instance_path(), f'{book.marked_file_name}.csv'))
        disk.publish(f"/book/{book.file_name}")
        disk.publish(f"/book/{book.marked_file_name}.csv")
        return redirect('/')
    return render_template('books.html', file_label='Файл книги (.txt)', title='Добавление книги',
                           form=form)
Example #3
0
def add_books():
    form = BooksForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        books = Books()
        books.title = form.title.data
        books.author = form.author.data
        books.content = form.content.data
        current_user.books.append(books)
        db_sess.merge(current_user)
        db_sess.commit()
        return redirect('/')
    return render_template('newBook.html', title='Добавление книги', form=form)
Example #4
0
def add_book(update, context):
    if update.message.text == 'Меню':
        back_to_menu(update, context)
        return ConversationHandler.END
    db_ses = db_session.create_session()
    user = db_ses.query(User).filter(
        User.id == update.effective_user.id).first()
    book = Books()
    book.title = update.message.text
    if book.title not in [i.title for i in user.books]:
        user.books.append(book)
        db_ses.commit()
        update.message.reply_text('Книга успешно добавлена',
                                  reply_markup=library_markup)
        return 1
    else:
        update.message.reply_text('У Вас уже есть эта книга',
                                  reply_markup=library_markup)
        return 1