def import_posts(self): category = self.get_category() self.write_out(self.style.STEP('- Importing entries\n')) for post in self.blogger_manager.get_posts(self.blogger_blog_id): creation_date = convert_blogger_timestamp(post.published.text) status = DRAFT if is_draft(post) else PUBLISHED title = post.title.text or '' content = post.content.text or '' slug = slugify(post.title.text or get_post_id(post))[:255] try: entry = Entry.objects.get(creation_date=creation_date, slug=slug) output = self.style.NOTICE('> Skipped %s (already migrated)\n' % entry) except Entry.DoesNotExist: entry = Entry(status=status, title=title, content=content, creation_date=creation_date, slug=slug) if self.default_author: entry.author = self.default_author entry.tags = ','.join([slugify(cat.term) for cat in post.category]) entry.last_update = convert_blogger_timestamp(post.updated.text) entry.save() entry.sites.add(self.SITE) entry.categories.add(category) entry.authors.add(self.default_author) try: self.import_comments(entry, post) except gdata_service.RequestError: # comments not available for this post pass output = self.style.ITEM('> Migrated %s + %s comments\n' % (entry.title, len(Comment.objects.for_model(entry)))) self.write_out(output)
def load_blog(self, item): """ Import content as blog entry :param item: entry in json format """ slug = item['slug'] if Entry.objects.filter(slug=slug).exists(): return status_dict = { 'draft': DRAFT, 'publish': PUBLISHED, } content = self.linebreaks(self.process_images(item['content'])) entry = Entry( title=item['title'], slug=slug, status=status_dict[item['post_status']], creation_date=self.localtime(item['date']), content=content, ) if item['meta'].get('author'): entry.display_author = item['meta']['author'].split(',')[0] if item['tags'] and item['tags'].strip(): entry.tags = item['tags'] if item['thumbnail']: resp = requests.get(item['thumbnail']) entry.image.save(item['thumbnail'].split('/')[-1], ImageFile(StringIO(resp.content)), save=False) entry.save() entry.sites.add(Site.objects.get_current()) for category_title in item['categories'].split(', '): if category_title and category_title.strip(): category_slug = slugify(category_title) category, _ = Category.objects.get_or_create( slug=category_slug, defaults={'title': category_title}) entry.categories.add(category)
def import_posts(self): category = self.get_category() for post in self.blogger_manager.get_posts(self.blogger_blog_id): creation_date = convert_blogger_timestamp(post.published.text) status = DRAFT if is_draft(post) else PUBLISHED title = post.title.text or '' content = post.content.text or '' slug = slugify(post.title.text or get_post_id(post))[:255] try: entry = Entry.objects.get(sites=self.SITE, authors=self.default_author, categories=category, status=status, title=title, content=content, creation_date=creation_date, slug=slug) output = self.style.TITLE('Skipped %s (already migrated)\n' % entry) continue except Entry.DoesNotExist: entry = Entry(status=status, title=title, content=content, creation_date=creation_date, slug=slug) if self.default_author: entry.author = self.default_author entry.tags = ','.join( [slugify(cat.term) for cat in post.category]) entry.last_update = convert_blogger_timestamp( post.updated.text) entry.save() entry.sites.add(self.SITE) entry.categories.add(category) entry.authors.add(self.default_author) try: self.import_comments(entry, post) except gdata_service.RequestError: # comments not available for this post pass output = self.style.TITLE( 'Migrated %s + %s comments\n' % (entry, len(Comment.objects.for_model(entry)))) self.write_out(output)