def index(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, body=form.body.data, author=current_user) db.session.add(post) db.session.commit() flash('动态已发布!') return redirect(url_for('blog.index')) page = request.args.get('page', 1, type=int) per = current_app.config['POSTS_PER_PAGE'] postz = current_user.likes_and_self_posts() posts = postz.paginate(page, per, False) next_url = url_for('blog.index', page=posts.next_num) if posts.has_next else None prev_url = url_for('blog.index', page=posts.prev_num) if posts.has_prev else None return render_template("blog/index.html", form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def test_new_post_no_title_should_contain_error(self): form_data = {'content': LONG_BLOG_POST} form = PostForm(data=form_data) self.assertEqual(form.is_valid(), False) self.assertEqual(form.errors, { 'title' : [u'This field is required.'] })
def test_new_small_post_form_should_be_valid(self): expectedContent = "some small content" expectedTitle = 'blog title' form_data = {'title': expectedTitle, 'content': expectedContent} form = PostForm(data=form_data) self.assertEqual(form.is_valid(), True) self.assertEqual(expectedContent, form.data['content']) self.assertEqual(expectedTitle, form.data['title'])
def test_new_large_post_form_should_be_valid(self): expectedContent = LONG_BLOG_POST expectedTitle = 'blog title' form_data = {'title': 'blog title', 'content': LONG_BLOG_POST} form = PostForm(data=form_data) self.assertEqual(form.is_valid(), True) self.assertEqual(expectedContent, form.data['content']) self.assertEqual(expectedTitle, form.data['title'])
def addPost(): """Return add post form or process new blog post""" form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data) form.populate_obj(post) post.author = current_user db.session.add(post) db.session.commit() return redirect(url_for('blog.post', post_slug=post.slug)) return render_template('blog/compose.html', form=form)
def editPost(post_slug): """Edit an existing blog post""" post = get_or_404(Post, slug=post_slug) if current_user != post.author: raise Unauthorized("You don't have permission to edit this post.") form = PostForm(obj=post) if form.validate_on_submit(): form.populate_obj(post) db.session.add(post) db.session.commit() return redirect(url_for('blog.post', post_slug=post.slug)) return render_template('blog/compose.html', form=form, post=post)
def update_post(post_id): if not Admin.query.filter_by(email=current_user.email).first(): abort(403) title = 'Update Post' post = BlogPost.query.get_or_404(post_id) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash('Post has been updated.', 'success') return redirect(url_for('blog.view_post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.author.data = post.author form.content.data = post.content return render_template('blog/edit_post.html', title=title, form=form)
def new_post(): if not Admin.query.filter_by(email=current_user.email).first(): abort(403) title = 'New Post' form = PostForm() if form.validate_on_submit(): post = BlogPost(author=form.author.data, title=form.title.data, content=form.content.data) db.session.add(post) db.session.commit() flash('Your post has been created.', 'success') return redirect(url_for('blog.view_post', post_id=post.id)) elif request.method == 'GET': if request.args.get('author'): form.author.data = request.args.get('author') return render_template('blog/edit_post.html', title=title, form=form)
def edit(article_id): """ 新增或编辑文章内容 首先判断是否提交了表单,如果提交了表单,则进入表单提交的流程。 若未提交表单,则进入读取数据的流程。 """ form = PostForm() # 提交表单 if request.form: title = form.title.data text = form.text.data text_pre = form.text_pre.data category = form.category.raw_data tags = form.tags.data status = ArticleStatus.script if form.save_script.data else ArticleStatus.publish create_time = form.create_time.data # 新增文章 if article_id == 0: new_article = Article(title=title, text=text, text_pre=text_pre, status=status, author="加油马德里", create_time=create_time) db.session.add(new_article) db.session.flush() db.session.commit() new_article.creat_article_category(category_id_list=[int(x) for x in category]) new_article.creat_article_tag(tag_name_list=tags.split(";")) article_id = new_article.id flash('文章新增成功.', category='success') # 编辑文章 else: article_res = Article.query.filter_by(id=article_id).first() article_res.title = title article_res.text = text article_res.text_pre = text_pre article_res.status = status article_res.create_time = create_time article_res.creat_article_category(category_id_list=[int(x) for x in category]) article_res.creat_article_tag(tag_name_list=tags.split(";")) db.session.commit() flash('保存成功.', category='success') return redirect(url_for('blog.edit', article_id=article_id)) article_res = Article.query.filter_by(id=article_id).first() form.title.data = article_res.title if article_res else "" form.text.data = article_res.text if article_res else "" form.text_pre.data = article_res.text_pre if article_res else "" form.tags.data = article_res.text_pre if article_res else "" form.create_time.data = article_res.create_time if article_res else datetime.now() form.category.choices = [(x.get("category_id"), x.get("category_name")) for x in Category.get_all_category()] return render_template('editor/contents_edit.html', form=form, post=article_res)
def new_post(request): if request.method == 'POST': post_form = PostForm(request.POST) if post_form.is_valid(): post = post_form.save(commit = False) post.creator = request.user post.save() else: post_form = PostForm() return render(request, 'post/new.html', locals())