Add establishment employee
This commit is contained in:
parent
22982f3332
commit
6f4cd56fcd
56
apps/establishment/management/commands/add_empl_position.py
Normal file
56
apps/establishment/management/commands/add_empl_position.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from django.db import connections
|
||||
from establishment.management.commands.add_position import namedtuplefetchall
|
||||
from establishment.models import Establishment, Position, Employee, EstablishmentEmployee
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Add employee position from old db to new db'
|
||||
|
||||
def empl_position_sql(self):
|
||||
with connections['legacy'].cursor() as cursor:
|
||||
cursor.execute('''
|
||||
select t.id,
|
||||
t.profile_id,
|
||||
t.establishment_id,
|
||||
t.role,
|
||||
t.start_date,
|
||||
t.end_date
|
||||
from
|
||||
(
|
||||
select
|
||||
DISTINCT
|
||||
a.id,
|
||||
a.profile_id,
|
||||
a.establishment_id,
|
||||
a.role,
|
||||
a.start_date,
|
||||
a.end_date,
|
||||
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.empl_position_sql():
|
||||
pos = Position.objects.filter(name={"en-GB": e.role}).first()
|
||||
empl = Employee.objects.filter(old_id=e.profile_id).first()
|
||||
est = Establishment.objects.filter(old_id=e.establishment_id).first()
|
||||
if pos and empl and est:
|
||||
est_empl = EstablishmentEmployee(
|
||||
from_date=e.start_date, to_date=e.end_date,
|
||||
old_id=e.id
|
||||
)
|
||||
est_empl.establishment = est
|
||||
est_empl.employee = empl
|
||||
est_empl.position = pos
|
||||
objects.append(est_empl)
|
||||
|
||||
ee = EstablishmentEmployee.objects.bulk_create(objects)
|
||||
self.stdout.write(self.style.WARNING(f'Created establishment employee objects.'))
|
||||
|
|
@ -4,8 +4,8 @@ from establishment.management.commands.add_position import namedtuplefetchall
|
|||
from establishment.models import Employee
|
||||
from django.db.models import Q
|
||||
|
||||
class Command(BaseCommand):
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Add employee from old db to new db'
|
||||
|
||||
def employees_sql(self):
|
||||
|
|
|
|||
19
apps/establishment/migrations/0056_auto_20191105_1401.py
Normal file
19
apps/establishment/migrations/0056_auto_20191105_1401.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 2.2.4 on 2019-11-05 14:01
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('establishment', '0055_employee_old_id'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='establishmentemployee',
|
||||
name='from_date',
|
||||
field=models.DateTimeField(blank=True, default=django.utils.timezone.now, null=True, verbose_name='From date'),
|
||||
),
|
||||
]
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 2.2.4 on 2019-11-05 14:02
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('establishment', '0056_auto_20191105_1401'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='establishmentemployee',
|
||||
name='old_id',
|
||||
field=models.IntegerField(blank=True, null=True, verbose_name='Old id'),
|
||||
),
|
||||
]
|
||||
|
|
@ -557,11 +557,14 @@ class EstablishmentEmployee(BaseAttributes):
|
|||
verbose_name=_('Establishment'))
|
||||
employee = models.ForeignKey('establishment.Employee', on_delete=models.PROTECT,
|
||||
verbose_name=_('Employee'))
|
||||
from_date = models.DateTimeField(default=timezone.now, verbose_name=_('From date'))
|
||||
from_date = models.DateTimeField(default=timezone.now, verbose_name=_('From date'),
|
||||
null=True, blank=True)
|
||||
to_date = models.DateTimeField(blank=True, null=True, default=None,
|
||||
verbose_name=_('To date'))
|
||||
position = models.ForeignKey(Position, on_delete=models.PROTECT,
|
||||
verbose_name=_('Position'))
|
||||
# old_id = affiliations_id
|
||||
old_id = models.IntegerField(verbose_name=_('Old id'), null=True, blank=True)
|
||||
|
||||
objects = EstablishmentEmployeeQuerySet.as_manager()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user