"""Moulinette of convertion Wordpress 2.1.2 to Zinnia""" from django.utils.encoding import smart_str from django.core.management.base import NoArgsCommand from django.contrib.auth.models import User from django.contrib.comments.models import Comment from django.contrib.sites.models import Site from django.contrib.contenttypes.models import ContentType from mywebsite.wordpress2django.models import WpTags from mywebsite.wordpress2django.models import WpPosts from mywebsite.wordpress2django.models import WpPost2Tag from mywebsite.wordpress2django.models import WpPost2Cat from mywebsite.wordpress2django.models import WpComments from mywebsite.wordpress2django.models import WpCategories from zinnia.models import Entry from zinnia.models import Category def _(str): return smart_str(str, 'iso-8859-1') class Command(NoArgsCommand): help = 'Migration data process of wordpress 2.1.2 to Zinnia App' def handle_noargs(self, **options): """Migrate all the datas""" self.wp_categories = {} self.migrate_users() self.migrate_categories() self.migrate_posts() def migrate_users(self): """Not programmed because do manual config in admin""" pass def migrate_categories(self): """Duplicate WPcategories to zinnia categories""" for wpcategory in WpCategories.objects.all(): category, created = Category.objects.get_or_create(title=_(wpcategory.cat_name), slug=wpcategory.category_nicename, description=_(wpcategory.category_description)) self.wp_categories[wpcategory.cat_id] = category print 'Inserting category : %s' % _(wpcategory.cat_name) def migrate_posts(self): """Duplicate WPposts to zinnia entries""" author = User.objects.get(username='fantomas') for post in WpPosts.objects.all(): entry, created = Entry.objects.get_or_create(title=_(post.post_title), slug=_(post.post_name), content=_(post.post_content),#Need to convert IMG author=author, visible=bool(post.post_status == 'publish'), creation_date=post.post_date_gmt, last_update=post.post_modified_gmt, ) print 'Inserting post : %s' % _(post.post_title) self.entry_categories(entry, post.id) self.entry_tags(entry, post.id) self.entry_comments(entry, post.id) def entry_tags(self, entry, wpid): """Duplicate wptags to zinnia entry tags""" tags = [] for wppost2tag in WpPost2Tag.objects.filter(post_id=wpid): wptag = WpTags.objects.get(tag_id=wppost2tag.tag_id) tags.append(_(wptag.tag)) entry.tags = ' '.join(tags) entry.save() print '-- Associating tags "%s" to entry %s' % (entry.tags, entry.title) def entry_comments(self, entry, wpid): """Duplicate wpcomments to zinnia entry comment""" site = Site.objects.all()[0] for comment in WpComments.objects.filter(comment_post_id=wpid): Comment.objects.get_or_create(content_type=ContentType.objects.get_for_model(entry), object_pk=entry.pk, site=site, user_name=_(comment.comment_author), user_email=_(comment.comment_author_email), user_url=_(comment.comment_author_url), comment=_(comment.comment_content), submit_date=comment.comment_date_gmt, ip_address=comment.comment_author_ip, is_public=bool(comment.comment_approved == '1'), is_removed=False, ) print '-- Associating comment %s' % comment.comment_id def entry_categories(self, entry, wpid): """Duplicate wprelation post/categories to zinnia""" for rel in WpPost2Cat.objects.filter(post_id=wpid): category = self.wp_categories[rel.category_id] entry.categories.add(category) print '-- Associating %s to category %s' % (entry.title, category.title)