from django.core.management.base import BaseCommand, CommandError from company.models import Company from theme.models import Theme, Tag from accounts.models import User from functions.form_check import translit_with_separator import datetime import MySQLdb from MySQLdb.cursors import DictCursor def convert_to_int(st): if not st: return None deduct = ('-','(',')','.',' ') for elem in deduct: st = st.replace(elem, '') if st.isdigit(): return int(st) else: return None class Command(BaseCommand): def handle(self, *args, **options): db = MySQLdb.connect(host="localhost", user="root", passwd="qazedc", db="expomap_ru", charset='utf8', cursorclass=DictCursor) cursor = db.cursor() sql = "select * from customers_company WHere otrasly>0" cursor.execute(sql) res = cursor.fetchall() print(len(res)) for c in res: id = c['company_id'] company = Company.objects.safe_get(id=id) if not company: continue theme_id = c.get('otrasly') tags = c.get('tags') if theme_id: try: theme = Theme.objects.get(id=theme_id) except Theme.DoesNotExist: continue print(theme) print(company) company.theme.add(theme) print('add %s theme to %s company'%(str(theme), str(company))) print('123') if not theme: continue tags = c.get('tags') if tags: tags = tags.split(',') if tags: for tag_id in tags: try: tag = Tag.objects.get(id=tag_id) except Tag.DoesNotExist: continue if tag.theme == theme: company.tag.add(tag) print('add %s tag to %s company'%(str(tag), str(company))) else: continue