[IMP] Added script to fix encoding errors
[cds-indico.git] / bin / maintenance / fix_utf8_errors.py
blobe0047410d06181efe7130d01bf033a04ae3e3d9c
1 # -*- coding: utf-8 -*-
2 ##
3 ##
4 ## This file is part of CDS Indico.
5 ## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
6 ##
7 ## CDS 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 2 of the
10 ## License, or (at your option) any later version.
12 ## CDS 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 CDS Indico; if not, write to the Free Software Foundation, Inc.,
19 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 """
22 Fixes possible encoding errors caused by older DB data
23 """
25 from MaKaC.common import DBMgr
26 from MaKaC.conference import ConferenceHolder
27 from indico.util.console import conferenceHolderIterator
30 def fix_prop(c, name):
31 getter = getattr(c, 'get' + name)
32 try:
33 getter().decode('utf-8')
34 except UnicodeDecodeError:
35 print c.getId(), name
36 getattr(c, 'set' + name)(getter().decode('latin1').encode('utf-8'))
39 def fix_everything(dbi):
40 i = 0
41 for level, c in conferenceHolderIterator(ConferenceHolder(), deepness='event'):
42 fix_prop(c, 'Title')
43 fix_prop(c, 'Description')
45 for spk in c.getChairList():
46 fix_prop(spk, 'Affiliation')
47 fix_prop(spk, 'FamilyName')
48 fix_prop(spk, 'FirstName')
50 if i % 999 == 0:
51 dbi.commit()
52 i += 1
55 if __name__ == '__main__':
56 dbi = DBMgr.getInstance()
57 dbi.startRequest()
59 fix_everything(dbi)
61 dbi.endRequest()