[FIX] Compute starting year value
[cds-indico.git] / bin / legacy / fixWrongImportedAgendaUsers.py
blobb4f491cdf22964d879c42108a7c4dc125dbadc42
1 # -*- coding: utf-8 -*-
2 ##
3 ##
4 ## This file is part of Indico.
5 ## Copyright (C) 2002 - 2012 European Organization for Nuclear Research (CERN).
6 ##
7 ## Indico is free software; you can redistribute it and/or
8 ## modify it under the terms of the GNU General Public License as
9 ## published by the Free Software Foundation; either version 3 of the
10 ## License, or (at your option) any later version.
12 ## Indico is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ## General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with Indico;if not, see <http://www.gnu.org/licenses/>.
20 import sys
21 sys.path.append("c:/development/indico/code/code")
22 import ldap
24 from MaKaC.common import DBMgr
25 from MaKaC import user
29 """
30 Migration tool has imported users into Indico. Due to the format of agenda, the new users
31 in Indico don't have Surname nor affilation, and both fields are mandatory. This script is
32 aim to fix it, taking the info from NICE LDAP.
33 """
36 def getNiceUserByEmail(email):
38 ldapserver.protocol_version = ldap.VERSION3
39 baseDN = "ou=people, o=cern, c=ch"
40 searchScope = ldap.SCOPE_ONELEVEL
41 retrieveAttributes = None
42 searchFilter="mail=%s"%email
43 ldap_result_id = ldapserver.search(baseDN, searchScope, searchFilter, retrieveAttributes)
44 result_type, result_data = ldapserver.result(ldap_result_id, 0)
45 if (result_data == []):
46 return {}
47 else:
48 #proccess the entry
49 dict = {}
50 for k in result_data[0][1].keys():
51 dict[k] = result_data[0][1][k][0]
52 return dict
53 return {}
58 DBMgr.getInstance().startRequest()
59 ldapserver = ldap.open("ldap.cern.ch:389")
60 error = False
62 ah=user.AvatarHolder()
63 notmodified=[]
64 modified=[]
65 notconfirmedNice=[]
66 withoutEmail=[]
67 for av in ah.getList():
68 if av.getEmail().strip() == "":
69 withoutEmail.append(av.getId())
70 else:
71 niceUserData=getNiceUserByEmail(av.getEmail())
72 if av.getSurName().strip() == "" or \
73 av.getFirstName().strip() == "" or \
74 av.getOrganisation().strip() == "":
75 if niceUserData != {}:
76 try:
77 if niceUserData['givenname']:
78 pass
79 if niceUserData['sn']:
80 pass
81 if niceUserData['homeinstitute']:
82 pass
83 modified.append(av.getId())
84 av.setName(niceUserData['givenname'])
85 av.setSurName(niceUserData['sn'])
86 if niceUserData['homeinstitute'].lower() != "unknown" or av.getOrganisation().strip()=="":
87 av.setOrganisation(niceUserData['homeinstitute'])
88 except Exception, e:
89 print "error:%s,email:%s"%(e, av.getEmail())
90 else:
91 notmodified.append(av.getId())
92 elif av.isNotConfirmed() and niceUserData != {}:
93 if len(av.getIdentityList()) == 1 and av.getIdentityList()[0].getAuthenticatorTag() == "Nice":
94 av.activateAccount()
95 notconfirmedNice.append(av.getId())
96 try:
97 DBMgr.getInstance().commit()
98 DBMgr.getInstance().sync()
99 except Exception, e:
100 print "error:%s"%e
101 print ""
102 print ""
103 print "Without email (%s): %s"%(len(withoutEmail), withoutEmail)
104 print "NICE to confirm (%s): %s"%(len(notconfirmedNice), notconfirmedNice)
105 print "No data - Will be modified (%s): %s"%(len(modified), modified)
106 print "No data - Will NOT be modified (%s): %s"%(len(notmodified), notmodified)
107 print ""
108 #if not error:
109 # DBMgr.getInstance().endRequest()
110 # print "No error. The change are saved"
111 #else:
112 # print "There were errors. The changes was not saved"
113 try:
114 DBMgr.getInstance().endRequest()
115 except Exception, e:
116 print "error:%s"%e
117 print ""
118 print ""
119 print "Without email (%s): %s"%(len(withoutEmail), withoutEmail)
120 print "NICE to confirm (%s): %s"%(len(notconfirmedNice), notconfirmedNice)
121 print "No data - Will be modified (%s): %s"%(len(modified), modified)
122 print "No data - Will NOT be modified (%s): %s"%(len(notmodified), notmodified)
124 print ""
125 print ""
126 print "Without email (%s): %s"%(len(withoutEmail), withoutEmail)
127 print "NICE to confirm (%s): %s"%(len(notconfirmedNice), notconfirmedNice)
128 print "No data - Will be modified (%s): %s"%(len(modified), modified)
129 print "No data - Will NOT be modified (%s): %s"%(len(notmodified), notmodified)