Exemplo n.º 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)
Exemplo n.º 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('Ваша книга успешно добавлена')