Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding:utf-8 -*-
- # vim: ai ts=4 sts=4 et sw=4 ft=python nowrap
- import redis
- import sqlsoup
- from datetime import date
- from django.core.management.base import BaseCommand
- from django.contrib.auth.models import Group
- from blogs.models import Blog, Post, BlogMember, Topic, AbstractPost
- from local_accounts.models import Account
- from sqlalchemy import Date, cast
- from sqlalchemy import and_, or_
- from mongoengine import Document
- from api.forms import NewsForm, ColumnForm
- from common.utils import slugify
- BLOG_LIST_KEY = 'mula2_blogs'
- class Command(BaseCommand):
- def handle(self, *args, **kwargs):
- # redis para blogs
- R = redis.StrictRedis('', password='')
- db = sqlsoup.SQLSoup('mysql://%s:%s@%s/%s' % ('', '', '', ''))
- # redaccion mulera
- news_blogs = (14379, )
- # post_status equivalentes a draft
- draft_status = ('draft', 'pending', 'private',)
- # post_status equivalentes a publish
- publish_status = ('publish',)
- try:
- print('Starting mula2 user migration. Connecting to database...')
- for i in range(15000):
- if R.llen(BLOG_LIST_KEY) == 0:
- print('No blog to migrate')
- break
- else:
- # Sacando un blog no migrado la lista de redis
- blog_wp_id, blog_guid = str(R.lpop(BLOG_LIST_KEY)).split(',')
- # Todos los posts de ese blog
- blog_posts = db.entity('wp_{id}_posts'.format(id=blog_wp_id))
- where = and_(blog_posts.post_type == 'post', cast(blog_posts.post_modified, Date) >= date(2012, 12, 31)) # nopep8
- blog_posts = blog_posts.filter(where)
- try:
- blog = Blog.objects.get(guid=blog_guid)
- fail = False
- except Blog.DoesNotExist:
- print "LOG: No se migro blog wp_id: %s, blog_guid: %s" % (blog_wp_id, blog_guid)
- fail = True
- if fail:
- continue
- print blog.subdomain
- counter = 0
- for post in blog_posts:
- if post.post_status in draft_status or post.post_status in publish_status:
- print counter + 1
- counter = counter + 1
- wp_author = post.post_author
- author_username = R.hget('mula1_users', wp_author)
- try:
- account = Account.objects.get(user__username=author_username)
- author_fail = False
- except Account.DoesNotExist:
- print "LOG: No se migro cuenta: %s, wp_id: %s, blog_guid: %s" % (author_username, blog_wp_id, blog_guid)
- author_fail = True
- if author_fail:
- continue
- print author_username
- title = post.post_title.decode('iso-8859-1').encode('utf8', errors='ignore')[:255]
- content = post.post_content.decode('iso-8859-1').encode('utf8', errors='ignore')
- publish_date = post.post_date
- modified_date = post.post_modified
- print title
- if len(title.strip()) == 0 or len(content.strip()) == 0:
- print "titulo o contenido vacio"
- continue
- # Obtener blogmember del autor y sino lo creamos
- try:
- author = BlogMember.objects.get(blog=blog, account=account)
- except:
- author = BlogMember(
- blog=blog,
- account=account,
- role=Group.objects.get(name='Editor')
- )
- author.save()
- # Guardando abstract post
- abst_post = AbstractPost(
- blog=blog
- )
- # abst_post.save() # No se puede guardar un abstract sin draft post
- # Guardando contenido en mongo
- if blog_wp_id in news_blogs:
- form = NewsForm({
- 'subtitle' : '',
- 'content' : content,
- })
- else:
- form = ColumnForm({
- 'subtitle' : '',
- 'content' : content,
- })
- if form.is_valid():
- document = form.save()
- pass
- else:
- print form.errors
- print "LOG: Error de validacion en form: %s, wp_id: %s, blog_guid: %s, post: %s" % (author_username, blog_wp_id, blog_guid, str(post.__dict__))
- # Guardando draft
- draft = Post(
- author=author,
- publish_date=publish_date,
- modified=modified_date,
- title=title,
- version='draft',
- content_id = str(document.id)
- )
- draft.slug = slugify(draft.title.encode('utf-8'))
- draft.save()
- abst_post.draft = draft
- abst_post.save()
- if post.post_status in publish_status:
- # Guardando contenido en mongo
- if blog_wp_id in news_blogs:
- form = NewsForm({
- 'subtitle' : '',
- 'content' : content,
- })
- else:
- form = ColumnForm({
- 'subtitle' : '',
- 'content' : content,
- })
- if form.is_valid():
- document = form.save()
- pass
- else:
- print form.errors
- print "LOG: Error de validacion en form: %s, wp_id: %s, blog_guid: %s, post: %s" % (author_username, blog_wp_id, blog_guid, str(post.__dict__))
- # Guardando freeze
- freeze = Post(
- author=author,
- publish_date=publish_date,
- modified=modified_date,
- title=title,
- version='published',
- content_id = str(document.id)
- )
- freeze.slug = slugify(freeze.title.encode('utf-8'))
- freeze.save()
- abst_post.freeze = freeze
- abst_post.save()
- if counter > 0:
- print '%s de %s' % (str(counter), blog.subdomain)
- except Exception, e:
- print('Something really bad happened: %s - %s' % (type(e), e.message,))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement