Example #1
0
def start():
    app = Flask(__name__)

    container = []  # FIXME: так делать будет нельзя
    wp = create_book('War And Piece', 'Tolstoy')
    anna = create_book('Anna Karenina', 'Tolstoy')

    # TODO: сделать распаковку в add_book
    container = add_book(container, wp)
    container = add_book(container, anna)

    @app.route('/')
    def index():
        search = request.args.get('search')
        if search:
            # TODO: почистить
            results = search_book_by_title(container, search)
            return render_template('index.html', books=results, search=search)

        return render_template('index.html', books=container)

    print(os.getenv('APP_ENV'))
    print(os.getenv('PORT'))
    if os.getenv('APP_ENV') == 'PROD' and os.getenv('PORT'):
        waitress.serve(app, port=os.getenv('PORT'))
    else:
        app.run(port=9876, debug=True)
Example #2
0
def test_add_one_book_to_empty_library():
    container = []
    book = create_book(
        'Война и мир', 'Толстой', 1000, 'война, любовь',
        'http://forkidsandmum.ru/pictures/books_covers/1010257708.jpg')

    add_book(container, book)

    assert len(container) == 1
    assert book in container
Example #3
0
def main():
    app = Flask(__name__)

    container = []
    wp = create_book('War and peace', 'Tolstoy')
    anna = create_book('Anna Karenina', 'Tolstoy')

    #TODO: сделать распаковку в add_book
    container = add_book(container, wp)
    container = add_book(container, anna)

    @app.route('/')
    def index():
        search = request.args.get('search')
        if search:
            # TODO: почистить
            results = search_book_by_title(container, search)
            return render_template('index.html', books=results, search=search)

        return render_template("index.html", books=container)

    @app.route('/books/<book_id>')
    def book_details(book_id):
        result = search_book_by_id(container, book_id)
        return render_template('book-details.html', book=result)

    @app.route('/books/<book_id>/edit')
    def book_edit(book_id):
        book = None
        if book_id == 'new':
            book = create_empty_book()
        else:
            book = search_book_by_id(container, book_id)
        return render_template('book-edit.html', book=book)

    @app.route('/books/<book_id>/save', methods=['POST'])
    def book_save(book_id):
        nonlocal container
        title = request.form['title']
        author = request.form['author']
        if book_id == 'new':
            book = create_book(title=title, author=author)
            container = add_book(container, book)
        else:
            book = search_book_by_id(container, book_id)
            pass # TODO сохранить изменеия
        return redirect(url_for('book_details', book_id=book['id']))

    @app.route('/books/<book_id>/remove', methods=['POST'])
    def book_remove(book_id):
        nonlocal container
        container = remove_book_by_id(container, book_id)
        return redirect(url_for('index'))

    app.run(port=9876, debug=True)
Example #4
0
 def book_save(book_id):
     nonlocal container
     title = request.form['title']
     author = request.form['author']
     if book_id == 'new':
         book = create_book(title=title, author=author)
         container = add_book(container, book)
     else:
         book = search_book_by_id(container, book_id)
         pass # TODO сохранить изменеия
     return redirect(url_for('book_details', book_id=book['id']))
Example #5
0
def test_search_author():
    books_list = []
    war_and_piece = create_book(
        'Война и мир', 'Толстой', 1000, 'война, любовь',
        'http://forkidsandmum.ru/pictures/books_covers/1010257708.jpg')

    idiot = create_book(
        'Идиот', 'Достоевский', 700, 'роман, любовь, драма, достоевский',
        'https://ozon-st.cdn.ngenix.net/multimedia/1022171034.jpg')

    crime_and_justice = create_book(
        'Преступление и наказание', 'Достоевский', 700,
        'роман, достоевский, драма',
        'https://biblio.by/media/catalog/product/cache/1/image/1200x1200/9df78eab33525d08d6e5fb8d27136e95/2/7/2785.jpg'
    )
    add_book(books_list, war_and_piece)
    add_book(books_list, idiot)
    add_book(books_list, crime_and_justice)

    founded_book = [{
        'title':
        'Война и мир',
        'author':
        'Толстой',
        'price':
        1000,
        'tags':
        'война, любовь',
        'url':
        'http://forkidsandmum.ru/pictures/books_covers/1010257708.jpg'
    }]

    result = search_books(books_list, 'Толстой')
    assert founded_book == result
Example #6
0
def main():
    app = Flask(__name__)
    container = []
    anna_karenina_tags = ['поезд', 'любовь', 'толстой']
    war_and_piece_tags = ['война', 'любовь', 'толстой']

    wp = create_book('War And Peace', 'Tolstoy', war_and_piece_tags)
    anna = create_book('Anna Karenina', 'Tolstoy', anna_karenina_tags)

    container = add_book(container, wp)
    container = add_book(container, anna)

    @app.route('/')
    def index():
        from flask import request
        search = request.args.get('search')
        if search:
            results = search_book(container, search)
            return render_template('in.html', books=results)
        return render_template('in.html', books=container, search=search)

    app.run(port=9876, debug=True)
Example #7
0
def test_add_book():
    data = []
    expected = [
        {
            'title': 'Война и мир',
            'author': 'Лев Толстой',
            'price': 1000,
            'tags': ['#война', '#безухов', '#дуб']
        },
    ]
    actual = add_book(
        data, {
            'title': 'Война и мир',
            'author': 'Лев Толстой',
            'price': 1000,
            'tags': ['#война', '#безухов', '#дуб']
        })
    assert expected == actual
Example #8
0
 def book_save(book_id):
     nonlocal container
     title = request.form['title']
     author = request.form['author']
     price = request.form['price']
     tags = request.form['tags']
     if book_id == 'new':
         book = create_book(title=title,
                            author=author,
                            price=price,
                            tags=tags)
         container = add_book(container, book)
     else:
         book = search_book_by_id(container, book_id)
         update_book(book,
                     title=title,
                     author=author,
                     price=price,
                     tags=tags)
         # pass  # TODO: сохранить измения
     return redirect(url_for('book_details', book_id=book['id']))
Example #9
0
def test_search_tag():
    books_list = []
    war_and_piece = create_book(
        'Война и мир', 'Толстой', 1000, 'война, любовь',
        'http://forkidsandmum.ru/pictures/books_covers/1010257708.jpg')
    anna_karenina = create_book(
        'Анна Каренина', 'Толстой', 500, 'поезд, любовь',
        'https://img.yakaboo.ua/media/catalog/product/cache/1/image/234c7c011ba026e66d29567e1be1d1f7/c/o/cover1_229_52.jpg'
    )

    idiot = create_book(
        'Идиот', 'Достоевский', 700, 'роман, любовь, драма, достоевский',
        'https://ozon-st.cdn.ngenix.net/multimedia/1022171034.jpg')

    crime_and_justice = create_book(
        'Преступление и наказание', 'Достоевский', 700,
        'роман, достоевский, драма',
        'https://biblio.by/media/catalog/product/cache/1/image/1200x1200/9df78eab33525d08d6e5fb8d27136e95/2/7/2785.jpg'
    )
    add_book(books_list, war_and_piece)
    add_book(books_list, anna_karenina)
    add_book(books_list, idiot)
    add_book(books_list, crime_and_justice)

    founded_book = [{
        'title':
        'Анна Каренина',
        'author':
        'Толстой',
        'price':
        500,
        'tags':
        'поезд, любовь',
        'url':
        'https://img.yakaboo.ua/media/catalog/product/cache/1/image/234c7c011ba026e66d29567e1be1d1f7/c/o/cover1_229_52.jpg'
    }]
    result = search_books(books_list, 'поезд')
    assert founded_book == result
Example #10
0
def start():
    app = Flask(__name__)

    container = []
    wp = create_book('War and Peace', 'Tolstoy')
    anna = create_book('Anna Karenina', 'Tolstoy')

    container = add_book(container, wp)
    container = add_book(container, anna)

    @app.route('/')
    def index():
        search = request.args.get('search')
        if search:
            results = search_book_by_title(container, search)
            return render_template('i.html', books=results, search=search)

        return render_template('i.html', books=container)


    @app.route('/books/<book_id>/edit')
    def book_edit(book_id):
        book = None
        if book_id == 'book.id':
            book = create_empty_book()
            return render_template('book-edit.html', book=book)
        else:
            book = search_book_by_id(container, book_id)
        return render_template('book-edit.html', book=book)

    @app.route('/books/<book_id>/save', methods=['POST'])
    def book_save(book_id):
        nonlocal container
        title = request.form['title']
        author = request.form['author']
        if book_id == 'new':
            book = create_book(title=title, author=author)
            container = add_book(container, book)
            return redirect(url_for('book_details', book_id=book['id']))
        else:
            book = search_book_by_id(container, book_id)
            pass
        return redirect(url_for('book_details', book_id=book['id']))


    @app.route('/books/<book_id>')
    def book_details(book_id):
        result = search_book_by_id(container, book_id)
        return render_template('book-details.html', book=result)

    @app.route('/books/<book_id>/remove', methods=['POST'])
    def book_remove(book_id):
        nonlocal container
        container = remove_book_by_id(container, book_id)
        return redirect(url_for('index'))

    @app.route('/books/<book_id>/modify', methods=['GET', 'POST'])
    def book_modify(book_id):
        nonlocal container
        book_title = request.form['title']
        book_author = request.form['author']
        container = modify_book(container, book_id, book_title, book_author)
        return redirect(url_for('index'))


    if os.getenv('APP_ENV') == 'PROD' and os.getenv('PORT'):
        waitress.serve(app, port=os.getenv('PORT'))
    else:
        app.run(port=9843, debug=True)
Example #11
0
def start():
    app = Flask(__name__)

    container = []  # FIXME: так делать будет нельзя
    wp = create_book('Война и мир', 'Лев Толстой', 1000,
                     ['#война', '#безухов', '#дуб'])
    anna = create_book('Анна Каренина', 'Лев Толстой', 800,
                       ['#поезд', '#любовь'])
    potter_phoenix = create_book('Гарри Поттер и Орден Феникса',
                                 'Джоан Роулинг', 700,
                                 ['#волшебство', '#магия', '#драконы'])
    robinson = create_book('Робинзон Крузо', 'Даниэль Дефо', 500,
                           ['#остров', '#одиночество', '#пятница'])
    musketeers = create_book('Три мушкетера', 'Александр Дюма', 900,
                             ['#честь', '#миледи', '#шпага'])

    container = add_book(container, wp)
    container = add_book(container, anna)
    container = add_book(container, potter_phoenix)
    container = add_book(container, robinson)
    container = add_book(container, musketeers)

    @app.route('/')
    def index():
        search = request.args.get('search')
        if search:
            results = search_book(container, search)
            return render_template('index.html', books=results, search=search)
        return render_template('index.html', books=container)

    @app.route('/books/<book_id>')
    def book_details(book_id):
        result = search_book_by_id(container, book_id)
        return render_template('book-details.html', book=result)

    @app.route('/books/<book_id>/save', methods=['POST'])
    def book_save(book_id):
        nonlocal container
        title = request.form['title']
        author = request.form['author']
        price = request.form['price']
        tags = request.form['tags']
        if book_id == 'new':
            book = create_book(title=title,
                               author=author,
                               price=price,
                               tags=tags)
            container = add_book(container, book)
        else:
            book = search_book_by_id(container, book_id)
            update_book(book,
                        title=title,
                        author=author,
                        price=price,
                        tags=tags)
            # pass  # TODO: сохранить измения
        return redirect(url_for('book_details', book_id=book['id']))

    @app.route('/books/<book_id>/edit'
               )  # id = 0, новый объект, id <> 0 - существующий
    def book_edit(book_id):
        book = None
        if book_id == 'new':
            book = create_empty_book()
        else:
            book = search_book_by_id(container, book_id)
        return render_template('book-edit.html', book=book)

    @app.route('/books/<book_id>/remove',
               methods=['POST'])  # routing, mapping url -> конкретной функцией
    def book_remove(book_id):
        nonlocal container
        container = remove_book_by_id(container, book_id)
        return redirect(url_for('index'))

    if os.getenv('APP_ENV') == 'PROD' and os.getenv('PORT'):
        waitress.serve(app, port=os.getenv('PORT'))
    else:
        app.run(port=9875, debug=True)