# -*- coding: utf-8 -*- import MySQLdb from MySQLdb.cursors import DictCursor from django.core.management.base import BaseCommand, CommandError from functions.translate import fill_with_signal from article.models import Article from accounts.models import User from theme.models import Theme,Tag class Command(BaseCommand): def handle(self, *args, **options): db = MySQLdb.connect(host="localhost", user="debian-sys-maint", passwd="LPoEhMvzURQoH1aJ", db="expomap_old_blog", charset='utf8', cursorclass=DictCursor) cursor = db.cursor() sql = """SELECT post.post_date as publish_date, post.post_content as description, post.post_modified as modified, post.post_title as main_title, post.post_name as slug, tag.name as tag_name FROM wp_posts post INNER JOIN wp_term_relationships rel ON rel.object_id = post.ID INNER JOIN wp_terms tag ON tag.term_id=rel.term_taxonomy_id WHERE post_status = "publish";""" cursor.execute(sql) result = cursor.fetchall() user = User.objects.get(id=1) tags = {x.name: x.id for x in Tag.objects.language('ru')} clear_result = {} errors = [] for a in result: if a.get('tag_name') in tags.keys(): tag_id = tags[a['tag_name']] a['tag_id'] = [tag_id] print a else: tag_id = None if not clear_result.get(a['main_title']): clear_result[a['main_title']] = a else: if clear_result[a['main_title']].get('paid_id') and tag_id: clear_result[a['main_title']]['tag_id'].append(tag_id) for a in clear_result.values(): article = Article(type=1, created = a['publish_date'], publish_date= a['publish_date'], modified = a['modified'], ) article.author = user article.translate('ru') article.main_title = a['main_title'] article.description = a['description'] article.slug = a['slug'] if a.get('tag_id'): article.tag = [Tag.objects.language('ru').get(id=id) for id in a['tag_id']] try: article.save() except Exception as e: errors.append(e) print errors