-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed_db_position.py
105 lines (83 loc) · 3.39 KB
/
seed_db_position.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import django
from django.db.models import Count
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE", "EmployeeManagementDjangoProject.settings"
)
django.setup()
from staff_management.models import Employee, Position
def assign_positions() -> None:
position_counts = {7: 2,
6: 10,
5: 50,
4: 250,
3: 1250,
2: 6250,
1: None}
employees_without_position = Employee.objects.filter(
position=None).order_by("id")
for employee in employees_without_position:
for position, count in position_counts.items():
if Employee.objects.filter(
position__hierarchy_level=position).count() == count:
print(f"Level {position}: employee amount capped")
break
if count is None or count > 0:
employee.position = Position.objects.get(pk=position)
employee.save()
if count is not None:
position_counts[position] -= 1
break
print("Positions assigned")
def fix_positions() -> None:
for employee in Employee.objects.all():
position = employee.position
supervisor = employee.supervisor
if position is None:
continue
if position.hierarchy_level == 7:
if supervisor is not None:
employee.supervisor = None
employee.save()
print(
f"Removed supervisor from employee {employee.full_name}.")
else:
if supervisor is not None and supervisor.position.hierarchy_level != position.hierarchy_level + 1:
new_supervisor = (
Employee.objects.filter(
position__hierarchy_level=position.hierarchy_level + 1)
.annotate(num_subordinates=Count("subordinates"))
.order_by("num_subordinates")
.first()
)
if new_supervisor:
employee.supervisor = new_supervisor
employee.save()
print(
f"Reassigned supervisor for "
f"employee {employee.full_name} "
f"to {new_supervisor.full_name}.")
else:
print(
f"No suitable new supervisor "
f"found for employee {employee.full_name}.")
elif supervisor is None:
new_supervisor = (
Employee.objects.filter(
position__hierarchy_level=position.hierarchy_level + 1)
.annotate(num_subordinates=Count("subordinates"))
.order_by("num_subordinates")
.first()
)
if new_supervisor:
employee.supervisor = new_supervisor
employee.save()
print(
f"Assigned supervisor for employee {employee.full_name} to {new_supervisor.full_name}.")
else:
print(
f"No suitable supervisor found for employee {employee.full_name}.")
print("Positions fixed")
if __name__ == "__main__":
assign_positions()
fix_positions()