def setUp(self): Base.metadata.drop_all(self.engine) Base.metadata.create_all(self.engine) self.author_1 = Authors( name='Nason Alex', born='1943-03-26', nationality='American', education='Yale University, BA, 1965', description='investigative journalist', wikipedia_url='https://en.wikipedia.org/wiki/Bob_Woodward', image_url= 'http://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Bob_Woodward.jpg/220px-Bob_Woodward.jpg' ) self.author_2 = Authors( name='Patrick Rothfuss', born='1994-08-13', nationality='Chinese', image_url= "http://upload.wikimedia.org/wikipedia/commons/thumb/7/7f/Patrick-rothfuss-2014-kyle-cassidy.jpg/250px-Patrick-rothfuss-2014-kyle-cassidy.jpg" ) self.author_3 = Authors(name='Robin Hortz', nationality='Australian', born='1952-03-05') self.publisher_1 = Publishers( name='Simon & Schuster', founded='1924', wikipedia_url="https://en.wikipedia.org/wiki/Simon_%26_Schuster", website="http://www.simonandschuster.com") self.publisher_2 = Publishers(name='HarperCollins', founded='1989', location='New York', website='http://harpercollins.com') self.publisher_3 = Publishers(name='Penguin Group', founded='1935', location='City of Westminster, London') self.book_1 = Books(title='Royal Assassin', google_id='kx12345', publication_date='2003-04-05') self.book_1.authors.append(self.author_3) self.book_2 = Books(title='Under the sea', google_id='34567', publication_date='2017-09-21') self.book_2.publishers.append(self.publisher_1) self.author_2.publishers.append(self.publisher_2) self.session.add(self.author_1) self.session.add(self.author_2) self.session.add(self.author_3) self.session.add(self.publisher_1) self.session.add(self.publisher_2) self.session.add(self.publisher_3) self.session.add(self.book_1) self.session.add(self.book_2) self.session.commit()
def post_authors(payload): body = request.get_json() author = Authors(body['auth_name'], body['auth_gender']) author.insert() return jsonify({"sucess": True, "auth_id": author.id}), 200
def add_author(): form = AddAuthorForm() if form.validate_on_submit(): author = Authors(author_name=form.author_name.data) db.session.add(author) db.session.commit() return redirect('/author_choice') return render_template('add_author.html', title='Добавление автора', form=form)
def test_books_update(self): self.session.add( Authors(name="Paprika Jiang", born="1924-02-14", nationality='American', education='Yale University, BA, 1965')) self.session.query(Authors).filter_by(name='Paprika Jiang').update( {'nationality': 'Chinese'}, synchronize_session=False) self.session.commit() result = self.session.query(Authors).filter_by( name='Paprika Jiang').one() self.assertTrue(result.nationality, 'Chinese') self.session.query(Authors).filter_by(name="Paprika Jiang").delete() self.session.commit()
def test_author_3(self): author_count_before = self.session.query(Authors).count() # self.assertEqual(author_count_before,3) self.author_4 = Authors( name='Orson Scott Card', born='1950', nationality='American', wikipedia_url='https://en.wikipedia.org/wiki/Orson_Scott_Card') self.session.add(self.author_4) author_count_now = self.session.query(Authors).count() self.assertEqual(author_count_now, author_count_before + 1) self.session.delete(self.author_4) self.session.commit()
def test_delete_authors(self): self.session.add( Authors(name='Benjamin', born='1998-08-13', nationality='Chinese', image_url= "http://upload.wikimedia.org/wikipedia/commons/thumbs/")) self.session.commit() query = self.session.query(Authors).filter_by( born="1998-08-13").first() self.assertTrue(query.born, "1998-08-13") self.session.query(Authors).filter_by(born="1998-08-13").delete() self.session.commit() number = self.session.query(Authors).filter_by( born="1998-08-13").count() self.assertTrue(str(number), "0")
def edit_table(row_number_delete=None, row_number_update=None): try: table_name = request.form['table-name'] session['table_name'] = table_name except KeyError: table_name = session['table_name'] tables = {'books': Books, 'authors': Authors} model = tables[table_name] if row_number_delete is not None: item = model.query.filter_by(id=row_number_delete).first() db.session.delete(item) db.session.commit() return redirect(url_for('edit_table')) if row_number_update is not None: item = model.query.filter_by(id=row_number_update).first() columns_names = item.columns_names()[1:] columns_data = item.columns_data() if request.method == 'POST': if model == Books: new_book_title = request.form['name1'] Books.query.filter_by(id=row_number_update).update( {'book_title': new_book_title}, synchronize_session=False ) new_authors_list = request.form['name2'].split(',') new_authors_list = [author_name.strip() for author_name in new_authors_list] authors_id_list = Books.query.filter_by(id=row_number_update).first().get_authors_id() for index, author_id in enumerate(authors_id_list): try: Authors.query.filter_by(id=author_id).update( {'author_name': new_authors_list[index]}, synchronize_session=False ) db.session.commit() except IndexError: item = Authors.query.filter_by(id=author_id).first() db.session.delete(item) db.session.commit() if len(authors_id_list) > len(new_authors_list) or not authors_id_list: for new_author in new_authors_list[len(authors_id_list):]: item = Authors(new_author) db.session.add(item) Book = Books.query.filter_by(id=row_number_update).first() Book.append_author(item) db.session.commit() else: Authors.query.filter_by(id=row_number_update).update( {'author_name': request.form['name1']}, synchronize_session=False ) db.session.commit() return redirect(url_for('edit_table')) return render_template('edit-row.html', columns_names=columns_names, columns_data=columns_data) form = AddForm(request.form) if form.one_of_two_validate(): book_title = form.book_title.data.strip() authors_list = form.author_name.data.split(',') authors_list = [author_name.strip() for author_name in authors_list] if book_title and authors_list: item = Books(book_title, [Authors(author_name) for author_name in authors_list]) db.session.add(item) elif not authors_list: item = Books(book_title) db.session.add(item) else: items = (Authors(author_name) for author_name in authors_list) [db.session.add(i) for i in items] db.session.commit() rows = model.query.all() rows = [row.columns_data() for row in rows] rows.insert(0, model.columns_names()) return render_template('edit-table.html', rows=rows, table_name=table_name)
def insert_authors(): author = Authors(name='Scr2', cpf='00000000002') print(author) author.save()
def parse_json(): jsn = load_json('books.json') for book in jsn: google_id = book.get('google_id', "") title = book.get('title', "") publication_date = book.get('publication_date', "") book_image_url = book.get('image_url', "") book_description = book.get('description', "") publishers = book.get('publishers') publishers_ = [] for publisher in publishers: publisher_wikipedia_url = publisher.get('wikipedia_url', "") publisher_name = publisher.get('name', "") publisher_description = publisher.get('description', "") owner = publisher.get('owner', "") publisher_image_url = publisher.get('image_url', "") founded = publisher.get('founded', "") # founded and location are not belonging to every publisheres location = publisher.get('location', "") publisher_website = publisher.get('website', "") publishers_.append( Publishers(name=publisher_name, wikipedia_url=publisher_wikipedia_url, description=publisher_description, owner=owner, image_url=publisher_image_url, website=publisher_website, founded=founded, location=location)) authors = book.get('authors') authors_ = [] for author in authors: born = author.get('born', "") author_name = author.get('name', "") education = author.get('education', "") nationality = author.get('nationality', "") author_description = author.get('description', "") alma_mater = author.get('alma_mater', "") author_wikipedia_url = author.get('wikipedia_url', "") author_image_url = author.get('image_url', "") authors_.append( Authors( name=author_name, born=born, education=education, nationality=nationality, description=author_description, alma_mater=alma_mater, wikipedia_url=author_wikipedia_url, image_url=author_image_url, )) yield Books(title=title, google_id=google_id, publication_date=publication_date, image_url=book_image_url, description=book_description, digest=book_description, ), \ authors_, \ publishers_