Merge branch 'feature/affiliations' into 'develop'
Feature/affiliations See merge request gm/gm-backend!102
This commit is contained in:
commit
8fca3916f9
57
apps/establishment/management/commands/add_empl_position.py
Normal file
57
apps/establishment/management/commands/add_empl_position.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
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.
|
||||
Run after add_position and add_employee!'''
|
||||
|
||||
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.'))
|
||||
38
apps/establishment/management/commands/add_employee.py
Normal file
38
apps/establishment/management/commands/add_employee.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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
|
||||
from django.db.models import Q
|
||||
|
||||
|
||||
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(Q(old_id=e.profile_id) | Q(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.'))
|
||||
35
apps/establishment/management/commands/add_position.py
Normal file
35
apps/establishment/management/commands/add_position.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from django.db import connections
|
||||
from collections import namedtuple
|
||||
from establishment.models import Position
|
||||
|
||||
|
||||
def namedtuplefetchall(cursor):
|
||||
"Return all rows from a cursor as a namedtuple"
|
||||
desc = cursor.description
|
||||
nt_result = namedtuple('Result', [col[0] for col in desc])
|
||||
return [nt_result(*row) for row in cursor.fetchall()]
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Add position from old db to new db'
|
||||
|
||||
def position_sql(self):
|
||||
with connections['legacy'].cursor() as cursor:
|
||||
cursor.execute('''
|
||||
select
|
||||
DISTINCT a.`role` as position_name
|
||||
from affiliations as a
|
||||
''')
|
||||
return namedtuplefetchall(cursor)
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
|
||||
objects = []
|
||||
for p in self.position_sql():
|
||||
count = Position.objects.filter(name={"en-GB": p.position_name}).count()
|
||||
if count == 0:
|
||||
objects.append(Position(name={"en-GB": p.position_name}))
|
||||
|
||||
p = Position.objects.bulk_create(objects)
|
||||
self.stdout.write(self.style.WARNING(f'Created positions objects.'))
|
||||
18
apps/establishment/migrations/0055_employee_old_id.py
Normal file
18
apps/establishment/migrations/0055_employee_old_id.py
Normal 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'),
|
||||
),
|
||||
]
|
||||
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'),
|
||||
),
|
||||
]
|
||||
|
|
@ -558,11 +558,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()
|
||||
|
||||
|
|
@ -579,6 +582,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."""
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user