gault-millau/apps/main/management/commands/add_award.py
Виктор Гладких e04c621a3d Fix message
2019-11-06 11:50:42 +03:00

41 lines
1.7 KiB
Python

from django.core.management.base import BaseCommand
from django.db import connections
from establishment.management.commands.add_position import namedtuplefetchall
from main.models import Award, AwardType
from establishment.models import Employee
class Command(BaseCommand):
help = '''Add award from old db to new db.
Run after command add_award_type!'''
def award_sql(self):
with connections['legacy'].cursor() as cursor:
cursor.execute('''
select
DISTINCT
a.id, a.profile_id, a.title,
a.`year` as vintage_year, a.state,
t.id as award_type
from awards as a
join award_types t on t.id = a.award_type_id
join profiles p on p.id = a.profile_id
''')
return namedtuplefetchall(cursor)
def handle(self, *args, **kwargs):
objects =[]
for a in self.award_sql():
profile = Employee.objects.filter(old_id=a.profile_id).first()
type = AwardType.objects.filter(old_id=a.award_type).first()
state = Award.PUBLISHED if a.state == 'published' else Award.WAITING
if profile and type:
award = Award(award_type=type, vintage_year=a.vintage_year,
title={"en-GB": a.title}, state=state,
content_object=profile, old_id=a.id)
objects.append(award)
awards = Award.objects.bulk_create(objects)
self.stdout.write(self.style.WARNING(f'Created awards objects.'))