def edit_post(post_id): """allows user to edit an existing post""" the_post = Post.query.get(post_id) if request.method == 'GET': form = PostForm( formdata=MultiDict({ 'title': the_post.title, 'date_written': the_post.date_written, 'body': the_post.body, 'author_display_name': the_post.author.display_name, 'language': the_post.language.name, 'nasod': the_post.country.name })) else: form = PostForm() if form.validate_on_submit(): the_author = Author.get_by_display_name(form.author_display_name.data) the_post.author_id = the_author.id the_post.title = form.title.data the_post.date_written = form.date_written.data the_post.body = form.body.data the_post.language_id = Language.query.filter( Language.name == form.language.data).first().id the_post.country_id = Country.query.filter( Country.name == form.nasod.data).first().id the_post.modified_by = current_user.id the_post.modify_timestamp = datetime.utcnow() db.session.commit() return redirect(url_for('post', post_id=post_id)) return render_template('post.html', form=form, post=the_post, author=the_post.author, poster=the_post.poster)
def new_post(): """allows user to create a new post""" form = PostForm() the_post = Post() if form.validate_on_submit(): the_author = Author.get_by_display_name(form.author_display_name.data) the_post.author_id = the_author.id the_post.title = form.title.data the_post.date_written = form.date_written.data the_post.body = form.body.data the_post.language_id = Language.query.filter( Language.name == form.language.data).first().id the_post.country_id = Country.query.filter( Country.name == form.nasod.data).first().id the_post.created_by = current_user.id the_post.modified_by = current_user.id db.session.add(the_post) db.session.commit() return redirect(url_for('post', post_id=the_post.id)) elif request.method == 'GET': the_post.title = form.title.data the_post.date_written = form.date_written.data the_post.body = form.body.data the_post.language = form.language.data return render_template('post.html', form=form, post=the_post, poster=current_user)
def validate_author_data(self, data): if "first_name" in data: if "last_name" in data: if "email" in data: return Author(data["first_name"], data["last_name"], data["email"]) raise self.handler_bad_request("JSON inválido")
def validate_new_data(self, author_data): if "first_name" in author_data: if "last_name" in author_data: if "email" in author_data: return Author(author_data["first_name"], author_data["last_name"], author_data["email"]) raise BadRequest()
def post(self): name = request.json['name'] author = Author(name) db.session.add(author) db.session.commit() result = AuthorSchema().dump(author) return result, 201
def get_authors(self): sql = """SELECT author_fname, author_lname, author_email, author_id FROM author""" self.cursor.execute(sql) rows = self.cursor.fetchall() return [Author(*row) for row in rows] if rows else None
def get_author(self, author_id): sql = f"""SELECT author_fname, author_lname, author_email, author_id FROM author WHERE author_id = %s""" self.cursor.execute(sql, (author_id, )) row = self.cursor.fetchone() return Author(*row) if row else None
async def get_authors(self, dbpool): sql = f"""SELECT author_fname, author_lname, author_email, author_id FROM author""" async with dbpool.acquire() as conn: async with conn.cursor() as cursor: await cursor.execute(sql) rows = await cursor.fetchall() return [Author(*row) for row in rows] if rows else None
async def get_author(self, dbpool, author_id): sql = f"""SELECT author_fname, author_lname, author_email, author_id FROM author WHERE author_id = %s""" async with dbpool.acquire() as conn: async with conn.cursor() as cursor: await cursor.execute(sql, (author_id, )) row = await cursor.fetchone() return Author(*row) if row else None
def author(self): """initialize author table""" with open('author.csv', encoding='utf-8-sig') as author_file: reader = csv.DictReader(author_file, delimiter=',') for row in reader: auth = Author( display_name=row['display_name'], first_name=row['first'], last_name=row['last'], middle_name=row['middle'], birth_year=None if row['birth'] == '' else row['birth'], death_year=None if row['death'] == '' else row['death']) auth.created_by, auth.modified_by = self.user_id, self.user_id coun = Country.query.filter( Country.name == row['country']).first() auth.country_id = coun.id lang = Language.query.filter( Language.name == row['language']).first() auth.languages.append(lang) db.session.add(auth) self.authors += 1 db.session.commit() print('Added {} authors to the database'.format(self.authors))
def post(self): """initialize post table""" with open('post.csv', encoding='utf-8-sig') as post_file: reader = csv.DictReader(post_file, delimiter=',') for row in reader: post = Post(title=row['title'], body=row['body'], date_written=row['date_written']) post.created_by, post.modified_by = self.user_id, self.user_id coun = Country.query.filter( Country.name == row['country']).first() post.country_id = coun.id lang = Language.query.filter( Language.name == row['language']).first() post.language_id = lang.id auth = Author.get_by_display_name(row['author']) post.author_id = auth.id db.session.add(post) self.posts += 1 db.session.commit() print('Added {} posts to the database'.format(self.posts))
def test_author(): mock_extracted_hash = {u'works_written': {u'count': 3.0, u'valuetype': u'object', u'values': [{u'lang': u'en', u'text': u'Business @ the Speed of Thought: Using a Digital Nervous System', u'id': u'/m/01xfkm', u'timestamp': u'2008-07-13T18:56:21Z', u'creator': u'/user/earlye'}, {u'lang': u'en', u'text': u'The Road Ahead', u'id': u'/m/01sqgw', u'timestamp': u'2008-11-03T17:25:53.001Z', u'creator': u'/user/ts_bot'}, {u'lang': u'en', u'text': u'Open Letter to Hobbyists', u'id': u'/m/01lt72', u'timestamp': u'2013-12-12T18:13:07Z', u'creator': u'/user/fact_farm'}]}, u'book_editions_published': {u'count': 16.0, u'valuetype': u'object', u'values': [{u'lang': u'en', u'text': u'Los Negocios En La Era Digital', u'id': u'/m/04txs8t', u'timestamp': u'2008-11-10T20:09:47.005Z', u'creator': u'/user/ts_bot'}, {u'lang': u'en', u'text': u'Der Weg Nach Vorn', u'id': u'/m/04txs91', u'timestamp': u'2008-11-10T20:09:47.006Z', u'creator': u'/user/ts_bot'}, {u'lang': u'en', u'text': u'The Road Ahead', u'id': u'/m/04txs98', u'timestamp': u'2008-11-10T20:09:47.007Z', u'creator': u'/user/ts_bot'}, {u'lang': u'en', u'text': u'Business at the Speed of Thought (Penguin Joint Venture Readers)', u'id': u'/m/04txs9q', u'timestamp': u'2008-11-10T20:09:47.008Z', u'creator': u'/user/ts_bot'}, {u'lang': u'en', u'text': u'Bill Gates speaks', u'id': u'/m/04txs9z', u'timestamp': u'2008-11-10T20:09:47.009Z', u'creator': u'/user/ts_bot'}, {u'lang': u'en', u'text': u'Business @ the Speed of Thought', u'id': u'/m/04v22tc', u'timestamp': u'2008-11-11T19:51:15.001Z', u'creator': u'/user/ts_bot'}, {u'lang': u'en', u'text': u'Business @ the Speed of Thought: Using a Digital Nervous System', u'id': u'/m/04v22vd', u'timestamp': u'2008-11-11T19:51:15.005Z', u'creator': u'/user/ts_bot'}, {u'lang': u'en', u'text': u'Business @ the Speed of Thought', u'id': u'/m/04v22vx', u'timestamp': u'2008-11-11T19:51:15.006Z', u'creator': u'/user/ts_bot'}, {u'lang': u'en', u'text': u'The Road Ahead', u'id': u'/m/04vhdjk', u'timestamp': u'2008-11-13T02:26:15Z', u'creator': u'/user/ts_bot'}, {u'lang': u'en', u'text': u'The Road Ahead', u'id': u'/m/04vhdk0', u'timestamp': u'2008-11-13T02:26:15.001Z', u'creator': u'/user/ts_bot'}]}, u'openlibrary_id': {u'count': 1.0, u'valuetype': u'string', u'values': [{u'lang': '', u'text': u'OL34537A', u'timestamp': u'2009-05-04T19:38:11Z', u'value': u'OL34537A', u'creator': u'/user/ts_bot'}]}, '/book/author': {}} author = Author() author.extract(mock_extracted_hash) assert_equal(author.books, ['Business @ the Speed of Thought: Using a Digital Nervous System', 'The Road Ahead', 'Open Letter to Hobbyists']) assert_equal(author.books_about, ['Los Negocios En La Era Digital', 'Der Weg Nach Vorn', 'The Road Ahead', 'Business at the Speed of Thought (Penguin Joint Venture Readers)', 'Bill Gates speaks', 'Business @ the Speed of Thought', 'Business @ the Speed of Thought: Using a Digital Nervous System'])
def test_author(): mock_extracted_hash = { u'works_written': { u'count': 3.0, u'valuetype': u'object', u'values': [{ u'lang': u'en', u'text': u'Business @ the Speed of Thought: Using a Digital Nervous System', u'id': u'/m/01xfkm', u'timestamp': u'2008-07-13T18:56:21Z', u'creator': u'/user/earlye' }, { u'lang': u'en', u'text': u'The Road Ahead', u'id': u'/m/01sqgw', u'timestamp': u'2008-11-03T17:25:53.001Z', u'creator': u'/user/ts_bot' }, { u'lang': u'en', u'text': u'Open Letter to Hobbyists', u'id': u'/m/01lt72', u'timestamp': u'2013-12-12T18:13:07Z', u'creator': u'/user/fact_farm' }] }, u'book_editions_published': { u'count': 16.0, u'valuetype': u'object', u'values': [{ u'lang': u'en', u'text': u'Los Negocios En La Era Digital', u'id': u'/m/04txs8t', u'timestamp': u'2008-11-10T20:09:47.005Z', u'creator': u'/user/ts_bot' }, { u'lang': u'en', u'text': u'Der Weg Nach Vorn', u'id': u'/m/04txs91', u'timestamp': u'2008-11-10T20:09:47.006Z', u'creator': u'/user/ts_bot' }, { u'lang': u'en', u'text': u'The Road Ahead', u'id': u'/m/04txs98', u'timestamp': u'2008-11-10T20:09:47.007Z', u'creator': u'/user/ts_bot' }, { u'lang': u'en', u'text': u'Business at the Speed of Thought (Penguin Joint Venture Readers)', u'id': u'/m/04txs9q', u'timestamp': u'2008-11-10T20:09:47.008Z', u'creator': u'/user/ts_bot' }, { u'lang': u'en', u'text': u'Bill Gates speaks', u'id': u'/m/04txs9z', u'timestamp': u'2008-11-10T20:09:47.009Z', u'creator': u'/user/ts_bot' }, { u'lang': u'en', u'text': u'Business @ the Speed of Thought', u'id': u'/m/04v22tc', u'timestamp': u'2008-11-11T19:51:15.001Z', u'creator': u'/user/ts_bot' }, { u'lang': u'en', u'text': u'Business @ the Speed of Thought: Using a Digital Nervous System', u'id': u'/m/04v22vd', u'timestamp': u'2008-11-11T19:51:15.005Z', u'creator': u'/user/ts_bot' }, { u'lang': u'en', u'text': u'Business @ the Speed of Thought', u'id': u'/m/04v22vx', u'timestamp': u'2008-11-11T19:51:15.006Z', u'creator': u'/user/ts_bot' }, { u'lang': u'en', u'text': u'The Road Ahead', u'id': u'/m/04vhdjk', u'timestamp': u'2008-11-13T02:26:15Z', u'creator': u'/user/ts_bot' }, { u'lang': u'en', u'text': u'The Road Ahead', u'id': u'/m/04vhdk0', u'timestamp': u'2008-11-13T02:26:15.001Z', u'creator': u'/user/ts_bot' }] }, u'openlibrary_id': { u'count': 1.0, u'valuetype': u'string', u'values': [{ u'lang': '', u'text': u'OL34537A', u'timestamp': u'2009-05-04T19:38:11Z', u'value': u'OL34537A', u'creator': u'/user/ts_bot' }] }, '/book/author': {} } author = Author() author.extract(mock_extracted_hash) assert_equal(author.books, [ 'Business @ the Speed of Thought: Using a Digital Nervous System', 'The Road Ahead', 'Open Letter to Hobbyists' ]) assert_equal(author.books_about, [ 'Los Negocios En La Era Digital', 'Der Weg Nach Vorn', 'The Road Ahead', 'Business at the Speed of Thought (Penguin Joint Venture Readers)', 'Bill Gates speaks', 'Business @ the Speed of Thought', 'Business @ the Speed of Thought: Using a Digital Nervous System' ])
def post(): data = request.form db.session.add(Author(name=data['name'])) db.session.commit()