示例#1
0
def addbooks():
    """ Добавление книги (только админ)"""
    form = BooksForm()
    session = db_session.create_session()
    author = session.query(Author)
    genre = session.query(Genre)
    if form.validate_on_submit():
        if session:
            book = Books(author_id=author.filter(
                Author.surname == form.author.data).first().id,
                         genre_id=genre.filter(
                             Genre.genre == form.genre.data).first().id,
                         title=form.title.data,
                         date=form.date.data,
                         price=form.price.data)
            book.cover = 'string'  # заглушка
            # Идея: создаю новую книгу, но заранее мне неизвестен ее id для корректного названия книги
            session.add(book)
            session.commit()
            book = session.query(Books).filter(
                Books.title == form.title.data).first()
            photo = f"static/img/book{book.id}.jpg"
            f = request.files['file']
            with open(photo, "wb") as file:
                file.write(f.read())
            book.cover = f'book{book.id}.jpg'
            session.commit()
            return redirect("/")
        return redirect('/logout')
    return render_template('addbooks.html',
                           title='Добавление книги',
                           form=form)
示例#2
0
 async def add_book(self, channel, *content):
     surname, title, date, genre, price, url = ' '.join(content).split(',')
     surname, title, date, genre, price, url = surname.strip(), title.strip(), \
                                               genre.strip(), date.strip(), price.strip(), url.strip()
     session = db_session.create_session()
     if session.query(Books).filter(Books.title == title).first():
         await channel.send("Такая книга уже есть в БД")
         return
     if not session.query(Genre).filter(Genre.genre == genre).first():
         await channel.send('Такого жанра нет в БД')
         return
     if not session.query(Author).filter(Author.surname == surname).first():
         await channel.send('Такого автора у нас нет в БД')
         return
     book = Books(author_id=session.query(Author).filter(
         Author.surname == surname).first().id,
                  title=title,
                  date=date,
                  price=price,
                  genre_id=session.query(Genre).filter(
                      Genre.genre == genre).first().id)
     response = requests.get(url)
     len_books = len(session.query(Books).all())
     photo = f'static/img/book{len_books + 1}.jpg'
     with open(photo, 'wb') as imgfile:
         imgfile.write(response.content)
     book.cover = f'book{len_books + 1}.jpg'
     session.add(book)
     session.commit()
     await channel.send('Ваша книга успешно добавлена')
示例#3
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')
示例#4
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)
示例#5
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)
示例#6
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
 def post(self):
     args = parser.parse_args()
     session = db_session.create_session()
     books = Books(
         title=args['title'],
         description=args['description'],
         price=args['price'],
         count=args['count'],
         author_id=args['author_id']
     )
     session.add(books)
     session.commit()
     return jsonify({'success': 'OK'})
 def post(self):
     args = parser.parse_args()
     session = db_session.create_session()
     books = Books(
         title=args['title'],
         author=args['author'],
         genre=args['genre'],
         img_url=args['img_url'],
         year_of_publishing=args['year_of_publishing'],
         description=args['description'],
         preview_url=args['preview_url']
     )
     session.add(books)
     session.commit()
     return jsonify({'success': 'OK'})
def add_book_to_author():
    form = Add_book_to_authorForm()
    if form.validate_on_submit():
        session = db_session.create_session()
        author = session.query(Author).filter(
            Author.name == form.name.data).first()
        if session.query(Books).filter(Books.title == form.title.data).first():
            return render_template(
                'add_book_to_author.html',
                title='Добавление книги автору',
                form=form,
                message="У этого автора уже есть данная книга")
        books = Books(title=form.title.data,
                      description=form.description.data,
                      price=form.price.data,
                      count=100,
                      author_id=author.id)
        session.add(books)
        session.commit()
        return redirect('/')
    return render_template('add_book_to_author.html',
                           title='Добавление книги автору',
                           form=form)