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 api_author(aid): if request.method == 'GET': rs = Authors.select().where(Authors.id == aid).dicts().get() return jsonify(rs) else: data = request.get_json(force=True) rs = Authors.update(**data).where(Authors.id == aid).execute() return jsonify(data)
def api_authors(): if request.method == 'GET': rs = Authors.select().order_by(Authors.second_name, Authors.first_name).dicts() return jsonify({'result': list(rs)}) else: rs = Authors.create(**request.get_json(force=True)) return jsonify(model_to_dict(rs))
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 add_authors_to_graph(nodes: list, edges: list, author_ids: Union[list, int], base_author_id: int=None) \ -> (list, list): """ Add a list of author ids to the graph Args: edges: current list of edges nodes: current list of nodes author_ids: a list of authors to add to the list base_author_id: the author of which the authors should be connected by edges Returns: updates list of nodes and edges """ author_ids = makelist(author_ids) for author_id in author_ids: author = Authors.select().where(Authors.id == author_id).get() new_node = { "id": author.id, "label": author.name, "shape": "dot", "value": author.pagerank, "font": "14px arial black", "color": '#97C2FC' if author.id == int(options.author) else '#FB7E81' } if new_node not in nodes: nodes.append(new_node) if base_author_id is not None and int(base_author_id) != author.id: new_edge = { "from": min(author.id, int(base_author_id)), "to": max(author.id, int(base_author_id)) } if new_edge not in edges: edges.append(new_edge) return nodes, edges
def main(): authors = Authors.select().limit(LIMIT) rating_list = [] for a in authors: print(a.name) rating_list.append(get_data(a.name)) ratings_to_csv(rating_list)
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 authors_info(payload): try: data = [] info = Authors.query.all() for auth in info: data.append({ "auth_name": auth.auth_name, "auth_gender": auth.auth_gender }) return jsonify({"success": True, "author": data}) author_info = [Authors.format() for auth in info] except BaseException: abort(401) return jsonify({"success": True, "authors": author_info}), 200
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")
author = int(author) except ValueError: continue if paper in paper_index: paper_index[paper].append(author) else: paper_index[paper] = [author] for paper, authors in paper_index.items(): for author1 in authors: for author2 in authors: if author1 != author2: author_matrix[author1, author2] += 1 # print(author_matrix, 10) # Graph analysis G_mat = author_matrix # Create a directed graph G1 = nx.DiGraph(G_mat) # Write edge list to csv file for further analysis in Gephi or something else #nx.write_edgelist(G1,'edges.csv',delimiter=',' ,data=False) # Calculate PageRank per author with parameter alpha = 0.8 pagerank = nx.pagerank(G1, alpha=0.8) for author in Authors.select(): author.pagerank = pagerank[author.id] author.save()
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 api_stories_by_id(aid): story = Stories.select().where(Stories.id == aid).dicts().get() story['authors'] = list( Authors.select().join(AuthorsStories).join(Stories).where( Stories.id == aid).dicts()) return jsonify({'result': story})
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_