示例#1
0
    def addPage(self, item):
        title = item.find("title").text # About
        link = item.find("link").text # http://mart-e.be/?p=2 ou http://mart-e.be/about
        page_id = item.find("{http://wordpress.org/export/1.2/}post_id").text # 2
        author = item.find("{http://purl.org/dc/elements/1.1/}creator").text # mart
        content = item.find("{http://purl.org/rss/1.0/modules/content/}encoded").text # the whole article
        slug = item.find("{http://wordpress.org/export/1.2/}post_name").text # nouveau-blog (CAN BE EMPTY)
        allow_comments = item.find("{http://wordpress.org/export/1.2/}comment_status").text # open
        status = item.find("{http://wordpress.org/export/1.2/}status").text # publish
        
        if slug:
            pages = Page.objects.filter(slug=slug)
        else:
            pages = Page.objects.filter(id=int(page_id))
        if len(pages) != 0:
            print("Skipping {0}".format(pages[0].slug))
            return pages[0]

        try:
            print("Creating page '"+title+"'")
        except:
            print("Creating page #"+page_id)
            
        page = Page()
        page.title = title
        page.id = page_id
        if slug:
            page.slug = slug[:50]
        else:
            page.slug = slugify(title)[:50]
        if author != self.author_name:
            raise Exception("Unknown author {0}".format(author))
        page.author = self.author
        page.body = content
        
        # in wordpress don't use markdown
        page.body_html = page.body
        
        if status == "publish":
            page.status = 2
        else:
            page.status = 1
        if allow_comments == "open":
            page.allow_comments = True
        else:
            page.allow_comments = False
                
        page.save()

        self.addComments(item, page)
        
        return page
示例#2
0
    def test_page_slugify_on_save(self):
        """ Tests the slug generated when saving a Page. """
        # Author is a required field in our model.
        # Create a user for this test and save it to the test database.
        user = User()
        user.save()

        # Create and save a new page to the test database.
        page = Page(title="My Test Page", content="test", author=user)
        page.save()

        # Make sure the slug that was generated in Page.save()
        # matches what we think it should be: slug will always convert spaces into _ and capitalized letters to lowercase
        self.assertEqual(page.slug, "my-test-page")
        tags = []
        if post_type == 'post':
                if type(item['category']) == list:
                    for d in item['category']:
                        process_category(d, categories, tags)
                else:
                    process_category(item['category'], categories, tags)
        if post_type == 'post':
            post = Post(id=id,title=title,slug=slug,content=content,publish_time=publish_time)
            post.save()
            print title
            for category in categories:
                try:
                    cat = Category.objects.get(name=category)
                except:    
                    cat = Category.objects.create(name=category)
                post.category.add(cat) 
            for tag in tags:
                post.tag.add(tag)
            post.save()
            post.publish_time=publish_time
            post.save()
        else:
            try:
                page = Page(id=id,title=title,slug=slug,content=content,publish_time=publish_time)
                page.save()
                page.publish_time=publish_time
                page.save()
            except:
                pass