# -*- coding: utf-8 -*- import os import MySQLdb from MySQLdb.cursors import DictCursor from django.core.management.base import BaseCommand from django.conf import settings from conference.models import Conference from django.core.files import File from functions.translate import fill_with_signal from country.models import Country from city.models import City 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 products.products_id as id, products_date_added as created, products_last_modified as modified, discount, expohit, ufi, products_name as name, products_description as description, products_short_description as main_title, products_viewed as viewed, products_period as period, products_org as organiser,products_products as products, products_official as web_page, products_img1 as logo, products_startdate as data_begin, products_enddate as data_end, url as old_url, places_id FROM `products` LEFT JOIN `products_description` ON products.products_id=products_description.products_id WHERE `products_status` =1 AND `conference` =1 AND places_id >0 """ cursor.execute(sql) result = cursor.fetchall() names = [item['name'] for item in result] media = settings.MEDIA_ROOT.replace('media/', '') for i, item in enumerate(result): name = item['name'] print('number: %d, name: %s'%(i, name.encode('utf8'))) if Conference.objects.filter(translations__name=name).exists(): continue data_begin = item['data_begin'] data_end= item['data_end'] place_id = item['places_id'] # convert to country and city country, city = get_places(place_id) #if isinstance(city, unicode): #print(city.encode('utf8')) # value = bad_cities.get(city) # if value: # bad_cities[city] = value + 1 # else: # bad_cities[city] = 1 # counter += 1 # continue if not country or not city: continue old_url = item['old_url'] periodic = item['period'] periodic = get_periodic(periodic) web_page = item['web_page'] currency = 'USD' expohit = item['expohit'] ufi = item['ufi'] if ufi: ufi = 1 else: ufi = 0 created = item['created'] modified = item['modified'] data = {'name_ru': name, 'main_title_ru': item['main_title'], 'description_ru': item['description'], 'main_themes_ru': item['products'], 'time_ru': '', 'discount_description_ru': '', 'title_ru': '', 'descriptions_ru': '', 'keywords_ru': ''} conference = Conference(data_begin=data_begin, data_end=data_end, city=city, country=country, web_page=web_page, old_url=old_url, periodic=periodic, currency=currency, expohit=expohit, created=created, modified=modified) try: fill_with_signal(Conference, conference, data) except: continue print('conference: %s'%str(conference)) #print(len(bad_cities.keys())) #print(len(result)) #print(Conference.objects.filter().count()) #print(Conference.objects.exclude(translations__name__in=names).count()) def get_periodic(value): PERIODIC = {u'Ежегодно': 1.0, u'Раз в 2 года': 0.5, u'2 раза в год': 2.0, u'4 раза в год': 4.0, u'3 раза в год': 3.0, u'Раз в 3 года': 0.33, u'Раз в 4 года': 0.25, u'5 раз в год': 5.0, u'Раз в два года': 0.5, u'Три раза в год': 3.0} return PERIODIC.get(value, 0) def get_logo(value): if not value: return None if value.startswith('..'): value = value.replace('..', 'media') elif value.startswith('/'): value = 'media'+value return value def get_places(place_id): db = MySQLdb.connect(host="localhost", user="expomap", passwd="7FbLtAGjse", db="old_db", charset='utf8', cursorclass=DictCursor) cursor = db.cursor() sql_city = """ SELECT title as city_name, url as city_old_url, inflect as city_inflect, parent_id as country_id FROM `products_places` WHERE `places_id` =%s """%place_id cursor.execute(sql_city) result = cursor.fetchone() city_name, city_old_url, city_inflect = result['city_name'], result['city_old_url'], result['city_inflect'] country_id = result['country_id'] if city_name: city_name = city_name.strip() if city_name==u'Гонконг': country = Country.objects.get(id=76) city = City.objects.get(url='hong-kong') return (country, city) sql_country = """ SELECT title as country_name, url as country_old_url, inflect as country_inflect FROM `products_places` WHERE `places_id` =%s """%country_id cursor.execute(sql_country) result = cursor.fetchone() country_name, country_old_url, country_inflect = result['country_name'], result['country_old_url'], result['country_inflect'] country_name = filter_country(country_name) try: country = Country.objects.get(translations__name=country_name) except Country.DoesNotExist: country = None except Country.MultipleObjectsReturned: country = Country.objects.filter(translations__name=country_name)[0] city_name = filter_city(city_name) try: city = City.objects.get(translations__name=city_name, country=country) except City.DoesNotExist: #city = city_name city = None #print(city_name.encode('utf8')) except City.MultipleObjectsReturned: city = City.objects.filter(translations__name=city_name, country=country)[0] #print('----------------') #print(city_name.encode('utf8')) #print('----------------') return (country, city) def filter_country(name): if name == u'ЮАР': name = u'Южно-Африканская Республика' elif name == u'Танзания': name = u'Объединенная Республика Танзания' elif name == u'Гавайи': name = u'США' elif name == u'Бирма': name = u'Мьянма' elif name == u'Босния и Герцоговина': name = u'Босния и Герцеговина' return name def filter_city(name): cities = {u'Дели': u'Нью-Дели', u'Дэли': u'Нью-Дели', u'Пуна': u'Пуне', u'Лонг-Бич': u'Лонг Бич', u'Калифорния': u'Лос-Анджелес', u'Санта-Клара': u'Санта Клара', u'Скотсдейл': u'Скоттсдейл', u'Пенанг': u'Пинанг', u'Лейк Буэна Виста': u'Лейк-Буэна-Виста', u'Лиллестрём': u'Лиллештром', u'Хертогенбош': u'Ден Бош', u'Марбелла': u'Марбелья', u'Лилль': u'Лилль', u'Ла Рош-сюр-Форон': u'Ла-Рош-сюр-Форон', u'Эль-Кувейт': u'Кувейт', u'Харрогит': u'Харрогейт', u'Иокогама': u'Йокогама', u'Палм Бич': u'Палм-Бич', u'Фрайбург': u'Фрейбург в Брейсгау', u'Колмар': u'Кольмар', u'Мускат': u'Маскат', u'Бад Зальцуфлен': u'Бад-Зальцуфлен'} new_name = cities.get(name) if new_name: return new_name else: return name """ # convert logo logo = item['logo'] logo = get_logo(logo) if logo and logo.startswith('media/images'): file = media + logo if os.path.isfile(file): f = open('mytest.pdf') else: logo = None """