Add employee

This commit is contained in:
Виктор Гладких 2019-11-05 16:29:27 +03:00
parent e7143f73c3
commit ea1ddfa320
4 changed files with 60 additions and 2 deletions

View File

@ -0,0 +1,39 @@
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
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, ' ',
p.email,'')
) 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 self.employees_sql():
count = Employee.objects.filter(old_id=e.profile_id).count()
count += Employee.objects.filter(name=e.name).count()
if count == 0:
objects.append(Employee(name=e.name, old_id=e.profile_id))
print(e.name)
empls = Employee.objects.bulk_create(objects)
self.stdout.write(self.style.WARNING(f'Created employee objects.'))

View File

@ -1,5 +1,5 @@
from django.core.management.base import BaseCommand
from django.db import connection, connections
from django.db import connections
from collections import namedtuple
from establishment.models import Position
@ -30,7 +30,6 @@ class Command(BaseCommand):
count = Position.objects.filter(name={"en-GB": p.position_name}).count()
if count == 0:
objects.append(Position(name={"en-GB": p.position_name}))
print(p.position_name)
p = Position.objects.bulk_create(objects)
self.stdout.write(self.style.WARNING(f'Created positions objects.'))

View File

@ -0,0 +1,18 @@
# Generated by Django 2.2.4 on 2019-11-05 13:15
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('establishment', '0054_auto_20191103_2117'),
]
operations = [
migrations.AddField(
model_name='employee',
name='old_id',
field=models.IntegerField(blank=True, null=True, verbose_name='Old id'),
),
]

View File

@ -578,6 +578,8 @@ class Employee(BaseAttributes):
awards = generic.GenericRelation(to='main.Award', related_query_name='employees')
tags = models.ManyToManyField('tag.Tag', related_name='employees',
verbose_name=_('Tags'))
# old_id = profile_id
old_id = models.IntegerField(verbose_name=_('Old id'), null=True, blank=True)
class Meta:
"""Meta class."""