source: WAeUP_SRP/trunk/skins/waeup_utilities/ti_441_resolve_part2.py @ 17305

Last change on this file since 17305 was 3021, checked in by joachim, 17 years ago

list of lgas to rename added, must be edited with correct names.

File size: 6.5 KB
Line 
1## Script (Python) "ti_441_resolve_part2"
2##bind container=container
3##bind context=context
4##bind namespace=
5##bind script=script
6##bind subpath=traverse_subpath
7##parameters=
8##title=
9##
10# $Id: fix_lgas.py 2979 2008-01-05 15:01:33Z henrik $
11"""
12"""
13try:
14    from Products.zdb import set_trace
15except:
16    def set_trace():
17        pass
18
19
20mtool = context.portal_membership
21member = mtool.getAuthenticatedMember()
22if str(member) not in ('admin','joachim'):
23    return
24
25
26import logging
27import DateTime
28logger = logging.getLogger('Skins.ti_441_resolve_part2')
29from Products.AdvancedQuery import Eq, Between, Le,In
30aq_portal = context.portal_catalog_real.evalAdvancedQuery
31aq_students = context.students_catalog.evalAdvancedQuery
32students_folder = context.portal_url.getPortalObject().campus.students
33
34request = context.REQUEST
35session = request.SESSION
36response = request.RESPONSE
37setheader = request.RESPONSE.setHeader
38def rwrite(s):
39    response.setHeader('Content-type','text/html; charset=ISO-8859-15')
40    response.write("%s<br>\n\r" % s)
41
42# def getLGAs():
43#     voc = getattr(context.portal_vocabularies,'local_gov_areas')
44#     states = []
45#     lgas  = []
46#     d = {}
47#     for k,v in voc.items():
48#         parts = v.split(' / ')
49#         if len(parts) == 1:
50#             state = parts[0].lower()
51#             lga = ""
52#         elif len(parts) == 2:
53#             state = parts[0].lower()
54#             lga = "_".join(parts[1].lower().split())
55#         else:
56#             continue
57#         if state not in states:
58#             states.append(state)
59#         if lga not in lgas:
60#             lgas.append(lga)
61#         d[k] = v
62#     return (d,states,lgas)
63
64#difference = context.waeup_tool.difference
65re_split = context.waeup_tool.re_split
66#logger.info('')
67student_records = context.students_catalog()
68total = len(student_records)
69logger.info("found %d students" % total)
70count = 0
71correctable = wrong = 1
72commit_after = 100
73logger.info('started to fix %d records in students_catalog' % total)
74mapping = context.waeup_tool.getStatesLgas()
75states = mapping['states']
76lgas = mapping['lgas']
77words_dict = mapping['word_dict']
78d = {}
79not_found_lgas = []
80not_found_count = {}
81rename_lgas = {}
82# rename_lgas['abia_owo'] = 'abia_owo'
83# rename_lgas['abia_ughelli-nort'] = 'abia_ughelli-nort'
84# rename_lgas['akwa ibom_urue-offong-oruk'] = 'akwa ibom_urue-offong-oruk'
85# rename_lgas['anambra_anambr'] = 'anambra_anambr'
86# rename_lgas['anambra_fufor'] = 'anambra_fufor'
87# rename_lgas['bayelsa_borip'] = 'bayelsa_borip'
88# rename_lgas['bayelsa_kolokuma-opkum'] = 'bayelsa_kolokuma-opkum'
89# rename_lgas['cross river_aboa/odua'] = 'cross river_aboa/odua'
90# rename_lgas['delta_ihial'] = 'delta_ihial'
91# rename_lgas['delta_ik'] = 'delta_ik'
92# rename_lgas['delta_ikpemil'] = 'delta_ikpemil'
93# rename_lgas['delta_ile-oluji-okeigb'] = 'delta_ile-oluji-okeigb'
94# rename_lgas['delta_nnewi-sout'] = 'delta_nnewi-sout'
95# rename_lgas['delta_ored'] = 'delta_ored'
96# rename_lgas['delta_osogb'] = 'delta_osogb'
97# rename_lgas['delta_owan-eas'] = 'delta_owan-eas'
98# rename_lgas['delta_uhunmwond'] = 'delta_uhunmwond'
99# rename_lgas['edo_abakalik'] = 'edo_abakalik'
100# rename_lgas['edo_ihial'] = 'edo_ihial'
101# rename_lgas['edo_isoko-sout'] = 'edo_isoko-sout'
102# rename_lgas['edo_nkanu-wes'] = 'edo_nkanu-wes'
103# rename_lgas['edo_oshimili-nort'] = 'edo_oshimili-nort'
104# rename_lgas['edo_uden'] = 'edo_uden'
105# rename_lgas['edo_ughelli-nort'] = 'edo_ughelli-nort'
106# rename_lgas['edo_ukwuan'] = 'edo_ukwuan'
107# rename_lgas['edo_uvwe'] = 'edo_uvwe'
108# rename_lgas['edo_uvwi'] = 'edo_uvwi'
109# rename_lgas['edo_uy'] = 'edo_uy'
110# rename_lgas['ekiti_ughelli-sout'] = 'ekiti_ughelli-sout'
111# rename_lgas['imo_ahoada-wes'] = 'imo_ahoada-wes'
112# rename_lgas['kogi_owan-wes'] = 'kogi_owan-wes'
113# rename_lgas['kwara_orhionmwo'] = 'kwara_orhionmwo'
114# rename_lgas['ogun_ijer'] = 'ogun_ijer'
115
116for student_record in student_records:
117    # skip record without lga value
118    count += 1
119    if not student_record.lga:
120        continue
121    # if student_record.lga.find(' ') < 0:
122    #     continue
123    if student_record.lga.startswith('no state'):
124        #logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id))
125        continue
126    #rwrite("%s: %s" % (student_record.id,student_record.lga))
127    if student_record.lga in rename_lgas.keys():
128        correctable += 1
129        d[student_record.id] = rename_lgas[student_record.lga]
130        if len(d) and not len(d)  % commit_after:
131            logger.info("found %d to correct state/lga combinations of %d so far" % (len(d),count))
132        continue
133    words = student_record.lga
134    if len(words.split(' / ')) == 2:
135        words = words.replace(' / ',' ')
136    state_lga = context.waeup_tool.findLga(words,words_dict)
137    if state_lga:
138        if state_lga != student_record.lga:
139            correctable += 1
140            msg = "found %s is %s for %s " % (state_lga,student_record.lga,student_record.id)
141            #rwrite(msg)
142            d[student_record.id] = state_lga
143            if len(d) and not len(d)  % commit_after:
144                logger.info("found %d to correct state/lga combinations of %d so far" % (len(d),count))
145        else:
146            msg = "already corrected %s : %s for %s " % (state_lga,student_record.lga,student_record.id)
147    else:
148        wrong += 1
149        msg = "no '%s' for %s" % (words,student_record.id)
150        if len(words) > 2:
151            if words not in not_found_lgas:
152                not_found_lgas += words,
153                not_found_count[words] = 1
154            else:
155                nfc = not_found_count[words]
156                nfc += 1
157                not_found_count[words] = nfc
158        #rwrite(msg)
159    # if count > 2000:
160    #     break
161not_found_lgas.sort()
162for lga in not_found_lgas:
163    msg = "not found %s count: %d" % (lga,not_found_count[lga])
164    logger.info(msg)
165to_edit = len(d)
166logger.info("found %d correctable state/lga combinations, not correctable %d total %d" % (correctable,
167                                                                                          wrong,
168                                                                                          count))
169edited = 1
170for student_id,lga in d.items():
171    msg = "set clearance.lga to %s for %s" % (lga,student_id)
172    getattr(getattr(students_folder,student_id),'clearance').getContent().edit(mapping={'lga':lga})
173    logger.info(msg)
174    edited += 1
175    if edited and not edited  % commit_after:
176        context.waeup_tool.doCommit()
177        logger.info("Committing %d,  %d of %d" % (commit_after,edited,to_edit))
178logger.info('finished, %d checked, %d edited' % (count,
179                                               edited,))
180
Note: See TracBrowser for help on using the repository browser.