summaryrefslogtreecommitdiffstats
path: root/grandlyon/scripts/copy-roles.py
blob: 3f9861d350b1aef97a4ee989ff963293efedb974 (plain)
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
import os
import json
import sys

from django_rbac.utils import get_role_model, get_ou_model

Ou = get_ou_model()
Role = get_role_model()

mapping = {}

name_src = sys.argv[1]
name_dst = sys.argv[2]

ou_src = Ou.objects.get(name=name_src)
ou = Ou.objects.get(name=name_dst)

for role in Role.objects.filter(ou=ou_src):
    if not (role.name.startswith('Gestionnaire') or role.name.startswith('Administrateur')):
        continue
    if role.slug.startswith('_'):
        continue
    new_name = role.name.replace(name_src, name_dst).replace(name_src.lower(), name_dst.lower())
    new_role, created = Role.objects.get_or_create(ou=ou, name=new_name)
    mapping[role.uuid] = {name_dst: new_role.uuid}

json.dump(mapping, open('/tmp/roles.mapping.json', 'w'), indent=2)