def test_tag_name_ordering_preserved(self): self.form_data['tags'] = 'blue, green, blue, red' form = PostForm(self.form_data) self.assertTrue(form.validate()) self.assertEqual(len(form.tags.data), 3) self.assertEqual(form.tags.data[0].name, 'blue') self.assertEqual(form.tags.data[1].name, 'green') self.assertEqual(form.tags.data[2].name, 'red')
def test_comma_separated_tag_value_serialized_to_tag_objects(self): original_green_tag = Tag('green') db.session.add(original_green_tag) db.session.commit() self.form_data['tags'] = 'green, red, red, blue' form = PostForm(self.form_data) self.assertTrue(form.validate()) self.assertEqual(len(form.tags.data), 3) tags = form.tags.data self.assertEqual(tags[0], original_green_tag) self.assertEqual(tags[1].id, None) self.assertEqual(tags[2].id, None)
def edit_post(post_id): post = Post.query.filter_by(author=current_user, id=post_id).first_or_404() form = PostForm(request.form, obj=post) if form.validate_on_submit(): form.populate_obj(post) if (post.pub_date is None and post.status == PostStatus.published): post.pub_date = utc_now() db.session.commit() message = u'Edited post "{}".'.format(form.title.data) flash(message, 'success') return redirect(url_for('.view_posts')) return render_template('blog/post_edit.html', post=post, form=form)
def add_post(): form = PostForm(request.form) if form.validate_on_submit(): status = PostStatus.from_string(form.status.data) post = Post(author=current_user, title=form.title.data, slug=form.slug.data, body=form.body.data, status=status, tags=form.tags.data) if status == PostStatus.published: post.pub_date = utc_now() db.session.add(post) db.session.commit() message = u'Post "{}" successfully added.'.format(form.title.data) flash(message, 'success') return redirect(url_for('.view_posts')) return render_template('blog/post_add.html', form=form)
def test_blank_values_not_considered_tags(self): self.form_data['tags'] = ', pink, ,' form = PostForm(self.form_data) self.assertTrue(form.validate()) self.assertEqual(len(form.tags.data), 1)
def test_empty_tags_value_creates_empty_set(self): self.form_data['tags'] = '' form = PostForm(self.form_data) self.assertTrue(form.validate()) self.assertEqual(len(form.tags.data), 0)
def test_used_slug_raises_validation_error(self): self.create_post(self.create_user(commit=False)) form = PostForm(self.form_data) self.assertFalse(form.validate()) self.assertEqual(form.errors['slug'], [u'Slug is not unique.'])
def test_post_tags_converted_to_comma_separated_string(self): post = Post(tags=[Tag('green'), Tag('blue')]) form = PostForm(obj=post) self.assertEqual(form.tags._value(), 'green, blue')