gault-millau/apps/establishment/management/commands/update_employee.py
Виктор Гладких b590a762c1 Fix update
2019-11-15 10:48:04 +03:00

40 lines
1.5 KiB
Python

from django.core.management.base import BaseCommand
from django.db import connections
from establishment.management.commands.add_position import namedtuplefetchall
from establishment.models import Employee, EstablishmentEmployee
from django.utils import timezone
from django.db.models import Q
from tqdm import tqdm
class Command(BaseCommand):
help = 'Add employee from old db to new db.'
def employees_sql(self):
with connections['legacy'].cursor() as cursor:
cursor.execute('''
select t.profile_id, t.name
from
(
select
DISTINCT
a.profile_id,
trim(CONCAT(p.firstname, ' ', p.lastname)
) as name
from affiliations as a
join profiles p on p.id = a.profile_id
) t
where t.name is not null
''')
return namedtuplefetchall(cursor)
def handle(self, *args, **options):
objects = []
for e in tqdm(self.employees_sql()):
empl = Employee.objects.filter(old_id=e.profile_id).update(name=e.name)
EstablishmentEmployee.objects.filter(from_date__isnull=True, old_id__isnull=False)\
.update(from_date=timezone.now()-timezone.timedelta(days=1))
self.stdout.write(self.style.WARNING(f'Update employee name objects.'))