# -*- coding: utf-8 -*- import MySQLdb import os.path from MySQLdb.cursors import DictCursor from django.utils.translation import activate from django.core.management.base import BaseCommand from django.conf import settings from exposition.models import Exposition from theme.models import Theme from theme.models import Theme, Tag class Command(BaseCommand): def handle(self, *args, **options): #expos = Exposition.objects.filter(theme__isnull=True, old_url__isnull=False) db = MySQLdb.connect(host="localhost", user="expomap", passwd="7FbLtAGjse", db="old_db", charset='utf8', cursorclass=DictCursor) cursor = db.cursor() activate('ru') #expos = Exposition.enable.upcoming().filter(logo='') expos = Exposition.objects.filter(tag__isnull=True).order_by('-data_end') #expo = Exposition.objects.get(old_url='salon-du-livre-2015') #handle_expo_tag(expo, cursor) for expo in expos: handle_expo_tag(expo, cursor) ''' find_old_id = """ SELECT products.products_id from products LEFT JOIN `products_description` ON products.products_id=products_description.products_id WHERE url='%s' """ find_themes = "SELECT categories_id FROM `products_to_categories` WHERE `products_id` =%d" ''' """ for expo in expos: cursor.execute(find_old_id%expo.old_url) old_ids = [item['products_id'] for item in cursor.fetchall()] print expo.old_url for id in old_ids: cursor.execute(find_themes%id) themes_ids = [item['categories_id'] for item in cursor.fetchall()] print themes_ids #if not themes_ids: # continue theme_qs = Theme.objects.filter(id__in=themes_ids) #expo.theme.add(*theme_qs) break print('----------------------') """ def handle_expo_tag(expo, cursor): old_url = expo.old_url if not old_url: return None print(old_url) find_old = """ SELECT products.products_id, url from products LEFT JOIN `products_description` ON products.products_id=products_description.products_id WHERE url='%s' """ cursor.execute(find_old%old_url) result = cursor.fetchone() expo_id = result.get('products_id') if not expo_id: return find_tag_id = """ SELECT tag_id FROM `products_tags` WHERE `product_id` =%d """ cursor.execute(find_tag_id%expo_id) tags_ids = [str(item['tag_id']) for item in cursor.fetchall()] if not tags_ids: return None find_tag = """ SELECT title FROM `tags` WHERE id in(%s) """ cursor.execute(find_tag%', '.join(tags_ids)) tag_names = [item['title'] for item in cursor.fetchall()] if not tag_names: return None themes = [item['id'] for item in expo.theme.all().values('id')] qs = Tag.objects.filter(translations__name__in=tag_names, theme__in=themes) expo.tag.add(*qs) def handle_expo(expo, cursor): """ fixing logos """ if expo.logo: return find_old = """ SELECT products.products_id, url, products_img1 as logo from products LEFT JOIN `products_description` ON products.products_id=products_description.products_id WHERE url='%s' """ cursor.execute(find_old%expo.old_url) result = cursor.fetchall() if not result: return logo = result[0]['logo'] if logo: logo = logo.replace('..', '') print(logo) print(os.path.isfile(settings.MEDIA_ROOT[:-1]+logo)) if (os.path.isfile(settings.MEDIA_ROOT[:-1]+logo)): expo.logo = logo expo.save()