cd05: add script to migrate roles from default ou to new ou

This commit is contained in:
Benjamin Dauvergne 2020-10-23 16:44:30 +02:00
parent 5d93fe6716
commit e0309cd832
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
# Commune de Démonstration
# Commune de Démonstration - AEC
# Commune de Démonstration - DPO
# Commune de Démonstration - SVE
# Commune de Démonstration - Urbanisme
from authentic2.a2_rbac.models import OrganizationalUnit as OU, Role
print('start')
new_ou = OU.objects.get(slug='hobo-collectivites')
old_agent = Role.objects.get(ou__slug='default', name='Agent')
new_agent = Role.objects.get(ou__slug='hobo-collectivites', name='Agent')
base_roles = (
Role.objects.filter(ou__slug='default', name__startswith='Com')
.exclude(name__contains=' - ')
.order_by('name')
)
extensions = [' - SVE', ' - AEC', ' - DPO', ' - Urbanisme']
for role in base_roles:
new_role, created = Role.objects.update_or_create(name=role.name, ou=new_ou, defaults={'slug': role.slug})
print(new_role, created)
for ext in extensions:
new_subrole, created = Role.objects.update_or_create(name=role.name + ext, ou=new_ou)
new_role.add_parent(new_subrole)
print('adding agent to old roles')
all_old_roles = Role.objects.filter(ou__slug='default', name__startswith='Com').order_by('name')
for role in all_old_roles:
role.add_parent(old_agent)
print(role)
all_new_roles = Role.objects.filter(ou__slug='hobo-collectivites', name__startswith='Com').order_by('name')
for role in all_new_roles:
role.add_parent(new_agent)
try:
old_role = Role.objects.get(ou__slug='default', name=role.name)
except Role.DoesNotExist:
pass
else:
print('move members of', old_role)
role.members = old_role.members.all()