Ajout script migration
[cyril.git] / Scripts / SigmaCopyUsers / copy_users.py
1 #!/usr/bin/env python
2
3 import MySQLdb, re
4 from datetime import datetime
5 from conf import db_origin, db_destination, default_password
6
7
8 # Correspondance: code_bureau:id_groupe (responsable)
9 responsables = {1:21, 2:22, 4:23, 5:24, 6:25, 7:26,
10 8:27, 9:28, 10:29, 15:30}
11
12
13 if __name__ == "__main__":
14 origin = MySQLdb.connect(host=db_origin['host'],
15 user=db_origin['user'],
16 passwd=db_origin['pass'],
17 db=db_origin['db'])
18 destination = MySQLdb.connect(host=db_destination['host'],
19 user=db_destination['user'],
20 passwd=db_destination['pass'],
21 db=db_destination['db'])
22 o = origin.cursor ()
23 d = destination.cursor ()
24
25 # Cherche les anciens
26 o.execute (
27 """SELECT u.c_utilisateur, u.l_prenom_utilisateur, u.l_nom_utilisateur,
28 u.l_email, h.l_habilitation
29 FROM `RE_UTILISATEUR_SIGMA` u, `GM_HABILITATIONS` h
30 WHERE u.c_matricule=h.f_matricule
31 AND h.l_habilitation like '%AB[%'
32 AND h.df_activ is null
33 AND u.i_utilisateur_actif = 1
34 AND u.df_activation_utilisateur IS NULL
35 AND u.c_utilisateur!=''
36 ORDER BY u.c_utilisateur""")
37 rows = o.fetchall ()
38
39 print len (rows), "utilisateurs"
40 for row in rows:
41 (utilisateur, prenom, nom, email, habilitation) = row
42 d.execute (
43 """INSERT INTO `auth_user` (username, first_name, last_name, email,
44 password, is_staff, is_active, is_superuser,
45 last_login, date_joined)
46 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
47 (utilisateur, prenom, nom, email, default_password, 0, 1,
48 0, datetime.now(), datetime.now()))
49 d.execute ("""SELECT `id` FROM `auth_user` WHERE `username`=%s""", utilisateur)
50 user_id = d.fetchone()[0]
51
52 if re.match (r'AB\[\d+\]$', habilitation):
53 bureau = re.search (r'\[(\d+)\]', habilitation).group(1)
54 group = responsables[int(bureau)]
55 d.execute ("""INSERT INTO auth_user_groups (user_id, group_id) VALUES (%s, %s)""", (user_id, group))
56
57
58 o.close ()
59 d.close ()