def new_post(): form = PostForm() if form.validate_on_submit(): form.save(set_author=True) flash('Added post.', 'success') return form.redirect(url_for('admin.index')) return render_template('admin/post_form.html', title='New post', form=form)
def edit_post(id, slug=None): post = Post.query.get_or_404(id) if slug is None: return redirect(url_for('admin.edit_post', id=id, slug=post.slug)) form = PostForm(obj=post) if form.validate_on_submit(): form.save() flash('Edited post.', 'success') return form.redirect(url_for('admin.index')) return render_template('admin/post_form.html', title='Edit post', form=form)
def test_save(self): form_1 = PostForm(DummyPostData(title='foo', short_text='bar', tags='foo, bar, foo bar')) post_1 = form_1.save() tags_1 = Tag.query.all() self.assertListEqual(tags_1, post_1.tags.all()) form_2 = PostForm(DummyPostData(title='foo', short_text='bar', tags='foo, bar, someothertag')) form_2.save() # Assert that only a single tag ('someothertag') has been added self.assertEqual(len(Tag.query.all()), 4) form_3 = PostForm(obj=post_1) form_3.tags.data = ['foo', 'bar', 'baz', 'duplicate', 'duplicate'] form_3.save() tag_1 = Tag.query.filter_by(name='baz').first() tag_2 = Tag.query.filter_by(name='foo bar').first() self.assertIsNotNone(tag_1) self.assertNotIn(tag_2, post_1.tags.all()) # Assert that the 'duplicate' tag has only been added once self.assertEqual(Tag.query.filter_by(name='duplicate').count(), 1)