def create_post(slug): """Create a new or edit an existing blog post""" next = request.values.get('next', '') post = None if slug: post = Post.query.filter_by(slug=slug).first() if slug and not post: flash('Invalid slug', 'error') return redirect(next) if request.method == 'GET': return render_template('admin/compose.html', post=post, categories=Category.query.all()) if request.method == 'POST': if request.form['action'] == 'Cancel': return redirect(next) title = request.form['title'] markup = request.form['markup'] tags = normalize_tags(request.form['tags']) comments_allowed = bool(request.values.get('comments_allowed', False)) visible = bool(request.values.get('visible', False)) #: Contains the ids of the categories categories = [] for name, id in request.form.iteritems(): if 'category-' in name: categories.append(id) if title == '': flash('You must provide a title', 'error') return render_template('admin/compose.html') elif request.form['action'] == 'Publish': post = Post(title, markup, comments_allowed, visible) post.tags = tags post.categories = categories db.session.add(post) db.session.commit() signals.post_created.send(post) flash('New post was successfully posted') return redirect(url_for('main.show_posts')) elif request.form['action'] == 'Update': post.title = title post.markup = markup post.comments_allowed = comments_allowed post.visible = visible post.tags = tags post.categories = categories db.session.commit() signals.post_updated.send(post) flash('Post was successfully updated') return redirect(url_for('main.show_post', slug=post.slug))
def test_post_creation(self): """Test if posts are created and saved properly to the database""" self.clear_db() title = "My post" markup = "# Title" tags = ['django','franz-und-bertha','vil-bil'] expected_slug = "my-post" expected_html = "<h1>Title</h1>" expected_date = datetime.date.today() post = Post(title=title, markup=markup) post.tags = tags db.session.add(post) db.session.commit() assert_equal(post.title, title) assert_equal(post.markup, markup) assert_true(post.comments_allowed) assert_true(post.visible) assert_equal(post.slug, expected_slug) assert expected_html in post.html assert_equal(post.datetime.date(), expected_date) assert_equal(sorted(tag.name for tag in post.tags), sorted(tags)) assert_equal([], post.categories) # Add another post db.session.add(Post(title=title, markup=markup, comments_allowed=False, visible=False)) db.session.commit() assert_false(Post.query.get(2).comments_allowed) assert_false(Post.query.get(2).visible) assert_equal(Post.query.count(), 2)
def test_post_creation(self): """Test if posts are created and saved properly to the database""" self.clear_db() title = "My post" markup = "# Title" tags = ['django', 'franz-und-bertha', 'vil-bil'] expected_slug = "my-post" expected_html = "<h1>Title</h1>" expected_date = datetime.date.today() post = Post(title=title, markup=markup) post.tags = tags db.session.add(post) db.session.commit() assert_equal(post.title, title) assert_equal(post.markup, markup) assert_true(post.comments_allowed) assert_true(post.visible) assert_equal(post.slug, expected_slug) assert expected_html in post.html assert_equal(post.datetime.date(), expected_date) assert_equal(sorted(tag.name for tag in post.tags), sorted(tags)) assert_equal([], post.categories) # Add another post db.session.add( Post(title=title, markup=markup, comments_allowed=False, visible=False)) db.session.commit() assert_false(Post.query.get(2).comments_allowed) assert_false(Post.query.get(2).visible) assert_equal(Post.query.count(), 2)
def test_tag_tidying(self): """Test if tags are automatically deleted when a post is deleted and there are no tag associations after that""" self.clear_db() db.session.add(Tag('cool')) db.session.add(Tag('cooler')) db.session.commit() post1 = Post(title='t', markup='') post1.tags = ['cool'] post2 = Post(title='t2', markup='') post2.tags = ['cool', 'cooler'] db.session.add(post1) db.session.add(post2) db.session.commit() db.session.delete(post2) db.session.commit() signals.post_deleted.send(post2) assert_equal(Tag.query.count(), 1) assert_equal(Tag.query.first().name, 'cool')
def test_tag_associations(self): """Test if tags and posts are correctly associated with each other""" self.clear_db() db.session.add(Tag('cool')) db.session.add(Tag('cooler')) db.session.commit() post1 = Post(title='t', markup='') post1.tags = ['cool'] post2 = Post(title='t2', markup='') post2.tags = ['cool', 'cooler'] db.session.add(post1) db.session.add(post2) db.session.commit() assert_equal(Tag.query.get(1).posts.count(), 2) # cool assert_equal(Tag.query.get(2).posts.count(), 1) # cooler assert_equal(len(post1.tags), 1) assert_equal(post1.tags[0].name, 'cool') assert_equal(len(post2.tags), 2) assert_equal(post2.tags[0].name, 'cool') assert_equal(post2.tags[1].name, 'cooler')