Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
  def submitApost(self):
      returnedTags=[]
 
      def Tagupdate (tag):
          #logging.info(self.tags.filter('tag',tag).count())
          if  tag!="" and self.tags.filter('tag',tag).count()==0:#tag does not exist
              return(Tag(tag=tag).put())
          else:
              return(self.tags.filter('tag',tag)[0].key())#otherwise find its key
          
      posttagkeys=[]
     
      if not self.tags:#Tags are empty therefore insert new tags
          posttagkeys=[Tag(tag=tag).put() for tag in self.posttags  if tag!=""]
      elif self.posttags[0]!="": posttagkeys=map(Tagupdate ,self.posttags)
      for key in posttagkeys:
          obj=db.get(key)
          returnedTags.append({"tag":obj.tag,"tagid":obj.key().id()})  
      catnames=[]
      catkeys=[]
      if self.categories:   #categories exist make list of them 
          [catnames.append(catobj.category) for catobj in self.categories]
          [catkeys.append(catobj.key()) for catobj in self.categories]
          catobjs=dict(zip(catnames,catkeys))
          if  self.postcategory in catobjs.keys():catkey=catobjs[self.postcategory]
          else:#this post has a new category
              newcatobj=Category()
              newcatobj.category=self.postcategory 
              newcatobj.put()
              catkey=newcatobj.key()
      else:
          newcatobj=Category()
          newcatobj.category=self.postcategory
          newcatobj.put()
          catkey=newcatobj.key()
   
            
      
      post=BlogPost()
      post.title=self.title
      post.body=self.body
      post.tags=posttagkeys
      post.category=catkey
      post.put()
     
      self.deleteMemcache(self)
      self.refetch(self)
      return(post.key(),returnedTags)
Ejemplo n.º 4
0
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)