def test_post_archive(self): now = utc_now() posts = [self.create_post(self.create_user(), status=PostStatus.published, pub_date=now)] self.assert_archive(posts, year=now.year) self.assert_archive(posts, year=now.year, month=now.month) self.assert_archive(posts, year=now.year, month=now.month, day=now.day)
def test_view_post(self): post = self.create_post(self.create_user()) now = utc_now() url = url_for("blog.view_post", year=now.year, month=now.month, day=now.day, slug=post.slug) self.assert_404(self.client.get(url)) post.publish() self.assert_page(url, "blog/post_view.html")
def create_post(self, user, commit=True, **kwargs): post = Post(author=user, title=u'My First Post', slug=u'my-first-post', body='Post body', **kwargs) if post.status == PostStatus.published and post.pub_date is None: post.pub_date = utc_now() db.session.add(post) if commit: db.session.commit() return post
def test_post_archive(self): now = utc_now() posts = [ self.create_post(self.create_user(), status=PostStatus.published, pub_date=now) ] self.assert_archive(posts, year=now.year) self.assert_archive(posts, year=now.year, month=now.month) self.assert_archive(posts, year=now.year, month=now.month, day=now.day)
def test_view_post(self): post = self.create_post(self.create_user()) now = utc_now() url = url_for('blog.view_post', year=now.year, month=now.month, day=now.day, slug=post.slug) self.assert_404(self.client.get(url)) post.publish() self.assert_page(url, 'blog/post_view.html')
def test_edit_post_page(self): post = self.create_post_and_login({ 'status': PostStatus.published, 'pub_date': utc_now() }) self.client.get(url_for('blog.edit_post', post_id=post.id)) self.assert_template_used('blog/post_edit.html') form = self.get_context_variable('form') self.assertEqual(form.title.data, post.title) self.assertEqual(form.status.data, 'published') self.assertEqual(form.body.data, u'Post body')
def create_post(self, user, commit=True, **kwargs): default_fields = {"author": user, "title": u"My First Post", "slug": u"my-first-post", "body": u"Post body"} default_fields.update(kwargs) post = Post(**default_fields) if post.status == PostStatus.published and post.pub_date is None: post.pub_date = utc_now() db.session.add(post) if commit: db.session.commit() return post
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 create_post(self, user, commit=True, **kwargs): default_fields = { 'author': user, 'title': u'My First Post', 'slug': u'my-first-post', 'body': u'Post body' } default_fields.update(kwargs) post = Post(**default_fields) if post.status == PostStatus.published and post.pub_date is None: post.pub_date = utc_now() db.session.add(post) if commit: db.session.commit() return post
def create_item(self, commit=True, **field_values): default_field_values = { 'content': 'Go to the store', 'details': 'Publix in South Miami', 'due': utc_now() + timedelta(hours=3) } default_field_values.update(field_values) if 'todo_list' not in field_values: default_field_values['todo_list'] = self.create_list(commit=False) item = Item(**default_field_values) db.session.add(item) if commit: db.session.commit() return item
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_edit_post_page(self): post = self.create_post_and_login({"status": PostStatus.published, "pub_date": utc_now()}) self.client.get(url_for("blog.edit_post", post_id=post.id)) self.assert_template_used("blog/post_edit.html") form = self.get_context_variable("form") self.assertEqual(form.title.data, post.title) self.assertEqual(form.status.data, "published") self.assertEqual(form.body.data, u"Post body")