# -*- coding: utf-8 -*- import MySQLdb import datetime from MySQLdb.cursors import DictCursor from django.core.management.base import BaseCommand, CommandError from functions.translate import fill_with_signal from translator.models import Translator from accounts.models import User, Profile from country.models import Country from city.models import City from functions.form_check import is_positive_integer def to_phone(phone): if not phone: return None deduct = ('-','(',')','.',' ', '+') for elem in deduct: phone = phone.replace(elem, '') if phone.isdigit(): return int(phone) else: return None def get_country_name(id): db = MySQLdb.connect(host="localhost", user="kotzilla", passwd="qazedc", db="crm", charset='utf8', cursorclass=DictCursor) sql =""" SELECT country_name as name FROM `country` WHERE `country_id` =%d"""%id cursor = db.cursor() cursor.execute(sql) result = cursor.fetchone() return result['name'] def get_city_name(id): db = MySQLdb.connect(host="localhost", user="kotzilla", passwd="qazedc", db="crm", charset='utf8', cursorclass=DictCursor) sql =""" SELECT city_name as name FROM `city` WHERE `city_id` =%d"""%id cursor = db.cursor() cursor.execute(sql) result = cursor.fetchone() return result['name'] class Command(BaseCommand): def handle(self, *args, **options): db = MySQLdb.connect(host="localhost", user="kotzilla", passwd="qazedc", db="crm", charset='utf8', cursorclass=DictCursor) cursor = db.cursor() sql = """SELECT * FROM `interpreter_table` """ cursor.execute(sql) result = cursor.fetchall() for data in result: email = data['email'] print(email.encode('utf8')) if email: try: user = User.objects.select_related('translator').get(username=email) except User.DoesNotExist: user = None if user: # add information to user profile = user.profile if data['phone1']: phone = to_phone(data['phone1']) if len(str(phone))<15: profile.phone = phone if data['skype']: profile.skype = data['skype'] if data['country_id']: name = get_country_name(data['country_id']) country_qs = Country.objects.filter(translations__name=name) if country_qs.exists(): country = country_qs[0] else: country = None profile.country = country if profile.country and data['city_id']: name = get_city_name(data['city_id']) city_qs = City.objects.filter(translations__name=name, country=profile.country) if city_qs.exists(): city = city_qs[0] else: city = None profile.city = city profile.save() else: # create new user user = User.objects.create_user(email, data['fam'], data['name'], data['pass']) user.is_active = True user.save() profile = user.profile #user.url = 'id%s'%str(self.id) if data['phone1']: phone = to_phone(data['phone1']) if len(str(phone))<15: profile.phone = phone if data['skype']: profile.skype = data['skype'] if data['country_id']: name = get_country_name(data['country_id']) country_qs = Country.objects.filter(translations__name=name) if country_qs.exists(): country = country_qs[0] else: country = None profile.country = country if profile.country and data['city_id']: name = get_city_name(data['city_id']) city_qs = City.objects.filter(translations__name=name, country=profile.country) if city_qs.exists(): city = city_qs[0] else: city = None profile.city = city profile.save() user.save() # fill translator if user.translator: translator_id = user.translator_id translator = Translator.objects.language('ru').get(id=translator_id) new = 0 else: translator = Translator() new = 1 gender = data['gender'] if gender == u'мужской': gender = 'male' elif gender == u'женский': gender = 'female' else: gender = 'male' translator.gender = gender translator.birth = data['born'] if data['automobile'] == u'да': translator.car = 1 if new: translator.translate('ru') translator.education = data['education'] translator.specialization = data['spec'] translator.languages = ', '.join([data['selfLang'], data['selfLang2']]) translator.save() user.translator = translator user.save()