Skip to content

Instantly share code, notes, and snippets.

@yakky
Last active May 6, 2020 21:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yakky/11336204 to your computer and use it in GitHub Desktop.
Save yakky/11336204 to your computer and use it in GitHub Desktop.
Import posts from wordpress to djangocms_blog
def import_wp(language):
from django.template.defaultfilters import truncatewords_html
from cms.api import add_plugin
from wordpress.models import Post as WPost
from djangocms_blog.models import Post, BlogCategory
from django.contrib.auth.models import User
for post in WPost.objects.published():
print post.title
author = None
users = User.objects.filter(username=post.author.login)
if users.exists():
author = users[0]
if not author:
users = User.objects.filter(email=post.author.email)
if users.exists():
author = users[0]
if not author:
author = User.objects.get(pk=1)
new_post = Post()
new_post.set_current_language(language)
new_post.title = post.title
new_post.slug = post.slug
new_post.abstract = truncatewords_html(post.content, 20)
new_post.date_created = post.post_date
new_post.date_published = post.post_date
new_post.date_modified = post.modified
new_post.save()
add_plugin(new_post.content, 'TextPlugin', language, body=post.content)
if author:
new_post.author = author
for cat in post.categories():
try:
category = BlogCategory.objects.language(language).get(translations__name=cat.name)
except BlogCategory.DoesNotExist:
category = BlogCategory()
category.set_current_language(language)
category.name = cat.name
category.save()
new_post.categories.add(category.pk)
if post.tags():
for tag in post.tags():
new_post.tags.add(tag.name)
new_post.save()
@kynsi
Copy link

kynsi commented Jan 13, 2018

I got this work but I did have to add the BlogConfig instance. Something like "config = BlogConfig.objects.get(pk=1)".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment