From e0309cd832b8ea330975eaaf690716405458d30f Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 23 Oct 2020 16:44:30 +0200 Subject: [PATCH] cd05: add script to migrate roles from default ou to new ou --- cd05/migration-communes.py | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 cd05/migration-communes.py diff --git a/cd05/migration-communes.py b/cd05/migration-communes.py new file mode 100644 index 0000000..71f40e7 --- /dev/null +++ b/cd05/migration-communes.py @@ -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()