# -*- coding: utf-8 -*- import datetime 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 django.db import IntegrityError class Command(BaseCommand): def handle(self, *args, **options): db = MySQLdb.connect(host="localhost", user="expomap", passwd="7FbLtAGjse", db="old_db", charset='utf8', cursorclass=DictCursor) cursor = db.cursor() sql = """SELECT news_id as id , headline as main_title, content as description, cid as author, date_added as created FROM `payed_news` WHERE status = 1 order by created DESC""" cursor.execute(sql) result = cursor.fetchall() user = User.objects.get(id=1) now = datetime.datetime.now() #Article.objects.news().delete() for a in result: title = a['main_title'] id = a['id'] published = a['created'] #try: # news = Article.objects.get(id=id) #except Article.DoesNotExist: # continue #news.publish_date = published #news.save() if len(a['main_title'])>255 or not a['main_title']: continue article = Article(type=Article.news, old_id=a['id'], publish_date=a['created'], created=now) if a['author']: try: author = User.objects.get(id=a['author']) except User.DoesNotExist: author = user else: author = user article.author = author article.translate('ru') article.main_title = a['main_title'] article.description = a['description'] try: article.save() print(article) except : print ('error. id:%d'%a['id']) #print(a['main_title'])