def add_post(self, title, content, author, tags=[], make_visible=True): ''' Create a new blog post Parameters ---------- title: str, Blog post title. content: str, Blog post content. author : Admin, Admin user who is authring the post. tags : list, A list of Tag objects, defaults to empty list. make_visible: Boolean, Whether to make the post visible to public (publish), defaults to True. Returns ------- BlogPost The blog post object that is added. ''' blog_post = BlogPost() blog_post.author = author blog_post.title = title blog_post.content = content blog_post.is_visible = make_visible blog_post.tags = tags blog_post.post_date = datetime.now() db_session.add(blog_post) db_session.commit() return blog_post
def recover(request): xmldoc = dom.parse(dazhu.settings.BASE_DIR + '/backup.xml') itemlist = xmldoc.getElementsByTagName('blog') result = "over" tools.debug("start import") for item in itemlist: tempguid = item.attributes['guid'].value tools.debug("current guid is ", tempguid) try: tempblog = BlogPost.objects.get(guid=tempguid) except: tools.debug(tempguid, "tempguid is exist") result += tempguid + " imported <br>" tempblog = BlogPost() tempblog.guid = item.attributes["guid"].value tempblog.author = item.attributes["author"].value tools.debug("author ", tempblog.author) tempblog.title = item.attributes["title"].value tempblog.category = item.attributes["category"].value tempblog.timestamp = datetime.strptime( item.attributes["timestamp"].value, "%Y%m%d %H%M%S") bodynode = item.getElementsByTagName('value')[0] tempblog.body = bodynode.firstChild.nodeValue tools.debug("value ", tempblog.body) tempblog.save() attachments = item.getElementsByTagName('attachment') for atts in attachments: rndName = atts.attributes["rndName"].value sourceName = atts.attributes["sourceName"].value tempAttachment = attachment() tempAttachment.blog = tempblog tempAttachment.sourceName = sourceName tempAttachment.rndName = rndName tempAttachment.save() cmts = item.getElementsByTagName('comment') for cmt in cmts: author = cmt.attributes["author"].value body = cmt.attributes["body"].value timestamp = datetime.strptime( cmt.attributes["timestamp"].value, "%Y%m%d %H%M%S") tempComment = Comment() tempComment.author = author tools.debug("commentUser ", author) tempComment.body = body tools.debug("message ", body) tempComment.blog = tempblog tempComment.timestamp = timestamp tools.debug("timestamp ", timestamp) tempComment.save() return HttpResponse(result)
def add_post(request): context = RequestContext(request) if request.method == 'POST': form = BlogPostForm(request.POST) if form.is_valid(): blog = BlogPost() blog.author = request.user blog.title = request.POST['title'] blog.bodytext = request.POST['bodytext'] blog.save() messages.add_message(request, SUCCESS, 'Post Added') return HttpResponseRedirect('/main/blog/') else: messages.add_message(request, SUCCESS, 'Enter a valid Post') return HttpResponseRedirect('/main/add/') else: form = BlogPostForm() return render_to_response('blog/add.html', {'form': form}, context)