def index(): page = request.args.get('page', 1, type=int) if request.cookies.get( 'show_followed' ) == 'show_followed' and current_user.is_authenticated: query = current_user.followed_posts else: query = Post.query pagination = query.order_by(Post.created.desc()).paginate(page, per_page=10) form = PostForm() if form.validate_on_submit(): post = Post(body=form.body.data, author_id=current_user.id) post.set_updated() try: db.session.add(post) db.session.commit() except: db.session.rollback() flash('发表失败请重试') return redirect(url_for('main.index')) context = { 'pagination': pagination, 'form': form, } return render_template('main/index.html', **context)
def create_test_post(): for i in range(1, 7): board_id = 9 title = "板块%s——标题%s"%(board_id,i) content = "内容%s"%i board = Board.query.get(board_id) author = FrontUser.query.first() post = Post(title=title, content=content) post.board = board post.author = author db.session.add(post) db.session.commit() print("测试帖子添加成功!")
def test_comments(self): # add two users r = Role.query.filter_by(name='User').first() self.assertIsNotNone(r) u1 = User(email='*****@*****.**', username='******', password='******', confirmed=True, role=r) u2 = User(email='*****@*****.**', username='******', password='******', confirmed=True, role=r) db.session.add_all([u1, u2]) db.session.commit() # add a post post = Post(body='body of the post', author=u1) db.session.add(post) db.session.commit() # write a comment response = self.client.post( url_for('api.new_post_comment', id=post.id), headers=self.get_api_headers('*****@*****.**', 'dog'), data=json.dumps({'body': 'Good [post](http://example.com)!'})) self.assertTrue(response.status_code == 201) json_response = json.loads(response.data.decode('utf-8')) url = response.headers.get('Location') self.assertIsNotNone(url) self.assertTrue(json_response['body'] == 'Good [post](http://example.com)!') self.assertTrue( re.sub('<.*?>', '', json_response['body_html']) == 'Good post!') # get the new comment response = self.client.get( url, headers=self.get_api_headers('*****@*****.**', 'cat')) self.assertTrue(response.status_code == 200) json_response = json.loads(response.data.decode('utf-8')) self.assertTrue(json_response['url'] == url) self.assertTrue(json_response['body'] == 'Good [post](http://example.com)!') # add another comment comment = Comment(body='Thank you!', author=u1, post=post) db.session.add(comment) db.session.commit() # get the two comments from the post response = self.client.get( url_for('api.get_post_comments', id=post.id), headers=self.get_api_headers('*****@*****.**', 'dog')) self.assertTrue(response.status_code == 200) json_response = json.loads(response.data.decode('utf-8')) self.assertIsNotNone(json_response.get('comments')) self.assertTrue(json_response.get('count', 0) == 2) # get all the comments response = self.client.get( url_for('api.get_comments', id=post.id), headers=self.get_api_headers('*****@*****.**', 'dog')) self.assertTrue(response.status_code == 200) json_response = json.loads(response.data.decode('utf-8')) self.assertIsNotNone(json_response.get('comments')) self.assertTrue(json_response.get('count', 0) == 2)