def update_post(request, post_id=None, title=None, body=None, published=False, tags=None): """ Add/Edit Blog Post :param: post_id [LONG] id of post to update [optional] :param: title [STR] post title [optional] :param: body [STR] post body [optional] :param: published [BOOL] post published flag [optional] :param: tags [LIST] list of tags[optional] :return: success [BOOL] success or failure """ # Create or retrieve Post if post_id: post = Post.get_by_id(post_id, parent=get_blog_key()) else: post = Post(parent=get_blog_key()) # Update Post if body is not None: post.body = body if title is not None: post.title = title if tags is not None: post.tags = tags post.published = published # Persist try: post.put() success = True except Exception: success = False logging.error("Error saving post", exc_info=True) return HttpResponse(success)
class PostTestCase(NdbTestCase): def setUp(self): super(PostTestCase, self).setUp() self.post = Post( title = 'Test Post', brief = 'To infinity ... and beyond.', content = 'Vestibulum id ligula porta felis euismod semper.', is_active = True, comments_enabled = False, date_published = datetime.date(2013,07,04), ) self.client = Client() def test_index_page(self): response = self.client.get(reverse('post_index')) self.assertEqual(response.status_code, 200) def test_archive_page(self): response = self.client.get(reverse('archive_index')) self.assertEqual(response.status_code, 200) def test_post_that_does_exist(self): post = self.post.put().get() response = self.client.get(reverse('post_view', args=[ post.date_published.strftime("%Y"), post.date_published.strftime("%m"), post.date_published.strftime("%d"), slugify(post.title)]) ) self.assertEqual(response.status_code, 200) def test_post_that_does_not_exist(self): new_title = 'I don\'t exist' post = self.post.put().get() response = self.client.get(reverse('post_view', args=[ post.date_published.strftime("%Y"), post.date_published.strftime("%m"), post.date_published.strftime("%d"), slugify(new_title)]) ) self.assertEqual(response.status_code, 404) def test_post_is_not_active(self): post = self.post.put().get() post.is_active = False edited_post = post.put().get() response = self.client.get(reverse('post_view', args=[ edited_post.date_published.strftime("%Y"), edited_post.date_published.strftime("%m"), edited_post.date_published.strftime("%d"), slugify(edited_post.title)]) ) self.assertEqual(response.status_code, 404)
def create_post(request): """ Create a new post """ local = {} if request.method == 'POST': try: title = request.POST.get('title') content = request.POST.get('content') post = Post(title=title, content=content) post.put() return HttpResponseRedirect('/') except: status = "msg msg-error" message = "Error: Create post" local = { "status": status, "message": message } return direct_to_template(request, 'create_post.html', local)
def admin_add_post(request): form = PostForm() if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): post = Post( title = form.cleaned_data['title'], brief = form.cleaned_data['brief'], content = form.cleaned_data['content'], is_active = form.cleaned_data['is_active'], comments_enabled = form.cleaned_data['comments_enabled'], ) post.put() return redirect(post.get_absolute_url()) else: form = PostForm() r = render( request, 'admin/add.html', { 'form': form } ) return r