Last active
May 6, 2020 21:50
-
-
Save yakky/11336204 to your computer and use it in GitHub Desktop.
Import posts from wordpress to djangocms_blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
@oesah: nice!
In other projects I used the rss feed to achieve the same result
I might post / document that too
I guest it would be nice to add such examples in the djangocms-blog documentation
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
This requires you to have the database locally. However, it would be nicer if you used
wordpress_xmlrpc
module, so you can use remote database.I am currently working on it, if you like I can post it once I am done.