You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

91 lines
2.7 KiB

# -*- 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
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='')
#expo = Exposition.objects.get(old_url='mir-detstva-i-shkoly-2015')
#handle_expo(expo, cursor)
for expo in expos:
handle_expo(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(expo, cursor):
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()