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 | """ |
---|
13 | try: |
---|
14 | from Products.zdb import set_trace |
---|
15 | except: |
---|
16 | def set_trace(): |
---|
17 | pass |
---|
18 | |
---|
19 | |
---|
20 | mtool = context.portal_membership |
---|
21 | member = mtool.getAuthenticatedMember() |
---|
22 | if str(member) not in ('admin','joachim'): |
---|
23 | return |
---|
24 | |
---|
25 | |
---|
26 | import logging |
---|
27 | import DateTime |
---|
28 | logger = logging.getLogger('Skins.ti_441_resolve_part2') |
---|
29 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
30 | aq_portal = context.portal_catalog_real.evalAdvancedQuery |
---|
31 | aq_students = context.students_catalog.evalAdvancedQuery |
---|
32 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
33 | |
---|
34 | request = context.REQUEST |
---|
35 | session = request.SESSION |
---|
36 | response = request.RESPONSE |
---|
37 | setheader = request.RESPONSE.setHeader |
---|
38 | def 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 |
---|
65 | re_split = context.waeup_tool.re_split |
---|
66 | #logger.info('') |
---|
67 | student_records = context.students_catalog() |
---|
68 | total = len(student_records) |
---|
69 | logger.info("found %d students" % total) |
---|
70 | count = 0 |
---|
71 | correctable = wrong = 1 |
---|
72 | commit_after = 100 |
---|
73 | logger.info('started to fix %d records in students_catalog' % total) |
---|
74 | mapping = context.waeup_tool.getStatesLgas() |
---|
75 | states = mapping['states'] |
---|
76 | lgas = mapping['lgas'] |
---|
77 | words_dict = mapping['word_dict'] |
---|
78 | d = {} |
---|
79 | not_found_lgas = [] |
---|
80 | not_found_count = {} |
---|
81 | rename_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 | |
---|
116 | for 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 |
---|
161 | not_found_lgas.sort() |
---|
162 | for lga in not_found_lgas: |
---|
163 | msg = "not found %s count: %d" % (lga,not_found_count[lga]) |
---|
164 | logger.info(msg) |
---|
165 | to_edit = len(d) |
---|
166 | logger.info("found %d correctable state/lga combinations, not correctable %d total %d" % (correctable, |
---|
167 | wrong, |
---|
168 | count)) |
---|
169 | edited = 1 |
---|
170 | for 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)) |
---|
178 | logger.info('finished, %d checked, %d edited' % (count, |
---|
179 | edited,)) |
---|
180 | |
---|