Advertisement
MarioYC

migrate_posts

Feb 8th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.14 KB | None | 0 0
  1. # -*- coding:utf-8 -*-
  2. # vim: ai ts=4 sts=4 et sw=4 ft=python nowrap
  3.  
  4. import redis
  5. import sqlsoup
  6. from datetime import date
  7.  
  8. from django.core.management.base import BaseCommand
  9. from django.contrib.auth.models import Group
  10.  
  11. from blogs.models import Blog, Post, BlogMember, Topic, AbstractPost
  12. from local_accounts.models import Account
  13.  
  14. from sqlalchemy import Date, cast
  15. from sqlalchemy import and_, or_
  16. from mongoengine import Document
  17. from api.forms import NewsForm, ColumnForm
  18.  
  19. from common.utils import slugify
  20.  
  21.  
  22. BLOG_LIST_KEY = 'mula2_blogs'
  23.  
  24.  
  25. class Command(BaseCommand):
  26.     def handle(self, *args, **kwargs):
  27.         # redis para blogs
  28.         R = redis.StrictRedis('', password='')
  29.         db = sqlsoup.SQLSoup('mysql://%s:%s@%s/%s' % ('', '', '', ''))
  30.  
  31.         # redaccion mulera
  32.         news_blogs = (14379, )
  33.         # post_status equivalentes a draft
  34.         draft_status = ('draft', 'pending', 'private',)
  35.         # post_status equivalentes a publish
  36.         publish_status = ('publish',)
  37.  
  38.         try:
  39.             print('Starting mula2 user migration. Connecting to database...')
  40.             for i in range(15000):
  41.                 if R.llen(BLOG_LIST_KEY) == 0:
  42.                     print('No blog to migrate')
  43.                     break
  44.                 else:
  45.                     # Sacando un blog no migrado la lista de redis
  46.                     blog_wp_id, blog_guid = str(R.lpop(BLOG_LIST_KEY)).split(',')
  47.  
  48.                     # Todos los posts de ese blog
  49.                     blog_posts = db.entity('wp_{id}_posts'.format(id=blog_wp_id))
  50.                     where = and_(blog_posts.post_type == 'post', cast(blog_posts.post_modified, Date) >= date(2012, 12, 31))  # nopep8
  51.                     blog_posts = blog_posts.filter(where)
  52.  
  53.                     try:
  54.                         blog = Blog.objects.get(guid=blog_guid)
  55.                         fail = False
  56.                     except Blog.DoesNotExist:
  57.                         print "LOG: No se migro blog wp_id: %s, blog_guid: %s" % (blog_wp_id, blog_guid)
  58.                         fail = True
  59.  
  60.                     if fail:
  61.                         continue
  62.  
  63.                     print blog.subdomain
  64.                     counter = 0
  65.  
  66.                     for post in blog_posts:
  67.                         if post.post_status in draft_status or post.post_status in publish_status:
  68.                             print counter + 1
  69.                             counter = counter + 1
  70.                             wp_author = post.post_author
  71.                             author_username = R.hget('mula1_users', wp_author)
  72.  
  73.                             try:
  74.                                 account = Account.objects.get(user__username=author_username)
  75.                                 author_fail = False
  76.                             except Account.DoesNotExist:
  77.                                 print "LOG: No se migro cuenta: %s, wp_id: %s, blog_guid: %s" % (author_username, blog_wp_id, blog_guid)
  78.                                 author_fail = True
  79.  
  80.                             if author_fail:
  81.                                 continue
  82.                             print author_username
  83.  
  84.                             title = post.post_title.decode('iso-8859-1').encode('utf8', errors='ignore')[:255]
  85.                             content = post.post_content.decode('iso-8859-1').encode('utf8', errors='ignore')
  86.                             publish_date = post.post_date
  87.                             modified_date = post.post_modified
  88.                             print title
  89.  
  90.                             if len(title.strip()) == 0 or len(content.strip()) == 0:
  91.                                  print "titulo o contenido vacio"
  92.                                  continue
  93.  
  94.                             # Obtener blogmember del autor y sino lo creamos
  95.                             try:
  96.                                 author = BlogMember.objects.get(blog=blog, account=account)
  97.                             except:
  98.                                 author = BlogMember(
  99.                                     blog=blog,
  100.                                     account=account,
  101.                                     role=Group.objects.get(name='Editor')
  102.                                 )
  103.                                 author.save()
  104.  
  105.                             # Guardando abstract post
  106.                             abst_post = AbstractPost(
  107.                                 blog=blog
  108.                             )
  109.                             # abst_post.save()   # No se puede guardar un abstract sin draft post
  110.  
  111.                             # Guardando contenido en mongo
  112.                             if blog_wp_id in news_blogs:
  113.                                 form = NewsForm({
  114.                                     'subtitle' : '',
  115.                                     'content' : content,
  116.                                 })
  117.                             else:
  118.                                 form = ColumnForm({
  119.                                     'subtitle' : '',
  120.                                     'content' : content,
  121.                                 })
  122.  
  123.                             if form.is_valid():
  124.                                 document = form.save()
  125.                                 pass
  126.                             else:
  127.                                 print form.errors
  128.                                 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__))
  129.  
  130.                             # Guardando draft
  131.                             draft = Post(
  132.                                 author=author,
  133.                                 publish_date=publish_date,
  134.                                 modified=modified_date,
  135.                                 title=title,
  136.                                 version='draft',
  137.                                 content_id = str(document.id)
  138.                             )
  139.                             draft.slug = slugify(draft.title.encode('utf-8'))
  140.                             draft.save()
  141.  
  142.                             abst_post.draft = draft
  143.                             abst_post.save()
  144.  
  145.                             if post.post_status in publish_status:
  146.                                 # Guardando contenido en mongo
  147.                                 if blog_wp_id in news_blogs:
  148.                                     form = NewsForm({
  149.                                         'subtitle' : '',
  150.                                         'content' : content,
  151.                                     })
  152.                                 else:
  153.                                     form = ColumnForm({
  154.                                         'subtitle' : '',
  155.                                         'content' : content,
  156.                                     })
  157.  
  158.                                 if form.is_valid():
  159.                                     document = form.save()
  160.                                     pass
  161.                                 else:
  162.                                     print form.errors
  163.                                     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__))
  164.  
  165.                                 # Guardando freeze
  166.                                 freeze = Post(
  167.                                     author=author,
  168.                                     publish_date=publish_date,
  169.                                     modified=modified_date,
  170.                                     title=title,
  171.                                     version='published',
  172.                                     content_id = str(document.id)
  173.                                 )
  174.                                 freeze.slug = slugify(freeze.title.encode('utf-8'))
  175.                                 freeze.save()
  176.  
  177.                                 abst_post.freeze = freeze
  178.                                 abst_post.save()
  179.                     if counter > 0:
  180.                         print '%s de %s' % (str(counter), blog.subdomain)
  181.         except Exception, e:
  182.             print('Something really bad happened: %s - %s' % (type(e), e.message,))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement