def post(self): # add new post or edit existed post t_values = {} current_post_id = self.request.POST["current_post_id"] post_title = self.request.POST["blog_title"] post_slug = get_safe_slug(self.request.POST["blog_slug"]) post_content = self.request.POST["blog_content"] # find category blog_category_id = self.request.POST["blog_category_id"] post_category = Category.get_by_id(long(blog_category_id)) if post_category: logging.info("find category %s for id %s" % (post_category.name, blog_category_id)) else: logging.error("category id %s can't be located" % (blog_category_id)) if current_post_id: logging.info("PostManager: post : edit post current_post_id = %s" % (current_post_id)) # update existed post post = Entry.get_by_id(long(current_post_id)) if post: t_values['alert_message'] = "Post %s has been updated!" % (post.title) post.title = post_title post.slug = post_slug post.content = post_content post.entrytype = "post" # update category count if this post is public if post.is_external_page and post.category != post_category: if post.category and (post.category.entrycount > 0): post.category.entrycount -= 1 post.category.put() post_category.entrycount += 1 post.category.put() post.category = post_category post.put() else: logging.info("PostManager: post : new post title %s" % (self.request.POST['blog_title'])) # create new post post = Entry() post.title = post_title post.slug = post_slug post.content = post_content post.entrytype = 'post' post.category = post_category # save as public or private? operation = self.request.POST["submit_action"] if operation == "save_publish": post.is_external_page = True # update category count post.category.entrycount += 1 post.category.put() else: # "save" operation post.is_external_page = False # save the post post.put() t_values['alert_message'] = "Post %s has been created!" % (post.title) # show all posts posts = Entry.all().filter("entrytype =", 'post') t_values['posts'] = posts return self.response.out.write(render_template("posts.html", t_values, "", True))
def post(self): result = {'message': '', 'status': 'success', 'content': ''} logging.info(self.request.POST) current_cate_id = self.request.POST.get("current_cate_id") cate_name = self.request.POST.get("cate_name") cate_slug = self.request.POST.get("cate_slug") operation = self.request.POST.get("operation") logging.info("CategoryManager post: current_cate_id = %s, cate_name = %s, cate_slug = %s, operation = %s" % (current_cate_id, cate_name, cate_slug, operation)) if current_cate_id: # edit existed link cate = Category.get_by_id(long(current_cate_id)) if cate: # current_cate_id exists if operation == "delete": if cate.entrycount == 0: cate.delete() result['message'] = "category %s has been deleted" % (current_cate_id) else: result['status'] = 'failed' result['message'] = "category %s can't be delete since it's still being used!" % (current_cate_id) else: cate.name = cate_name cate.slug = cate_slug cate.put() result['message'] = "category %s has been updated" % (cate.name) else: logging.info("category id=%s does not exist!" % (current_cate_id)) else: # create new cate cate = Category(name=cate_name, slug=cate_slug) cate.put() result['message'] = "category %s has been created" % (cate.name) result['content'] = render_template("category_block.html", {'cate': cate}, "", True) logging.info("content = %s" % (result['content'])) # return json result self.response.content_type = 'application/json' return self.response.out.write(json.encode(result))