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 | commit_after = 100 |
---|
72 | logger.info('started to fix %d records in students_catalog' % total) |
---|
73 | mapping = context.waeup_tool.getStatesLgas() |
---|
74 | states = mapping['states'] |
---|
75 | lgas = mapping['lgas'] |
---|
76 | words_dict = mapping['word_dict'] |
---|
77 | d = {} |
---|
78 | for student_record in student_records: |
---|
79 | # skip record without lga value |
---|
80 | count += 1 |
---|
81 | if not student_record.lga: |
---|
82 | continue |
---|
83 | # if student_record.lga.find(' ') < 0: |
---|
84 | # continue |
---|
85 | if student_record.lga.startswith('no state'): |
---|
86 | #logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id)) |
---|
87 | continue |
---|
88 | #rwrite("%s: %s" % (student_record.id,student_record.lga)) |
---|
89 | words = student_record.lga |
---|
90 | if len(words.split(' / ')) == 2: |
---|
91 | words = words.replace(' / ',' ') |
---|
92 | words = re_split('[^a-zA-Z0-9/]',words) |
---|
93 | lga_words = [] |
---|
94 | for word in words: |
---|
95 | if word: |
---|
96 | lga_words += word.lower(), |
---|
97 | lga_words.sort() |
---|
98 | # state = sl[0].lower().strip() |
---|
99 | # state = '_'.join(state.split()) |
---|
100 | # lga = sl[1].lower().strip() |
---|
101 | # if not lga or not state: |
---|
102 | # logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id)) |
---|
103 | # continue |
---|
104 | state_lga = '' |
---|
105 | while not state_lga: |
---|
106 | for k,l in words_dict.items(): |
---|
107 | if lga_words == l: |
---|
108 | state_lga = k |
---|
109 | break |
---|
110 | break |
---|
111 | if state_lga: |
---|
112 | if state_lga != student_record.lga: |
---|
113 | msg = "found %s is %s for %s " % (state_lga,student_record.lga,student_record.id) |
---|
114 | #rwrite(msg) |
---|
115 | d[student_record.id] = state_lga |
---|
116 | if len(d) and not len(d) % commit_after: |
---|
117 | logger.info("found %d to correct state/lga combinations of %d so far" % (len(d),count)) |
---|
118 | else: |
---|
119 | msg = "already corrected %s : %s for %s " % (state_lga,student_record.lga,student_record.id) |
---|
120 | else: |
---|
121 | msg = "no '%s' for %s" % (lga_words,student_record.id) |
---|
122 | # if count > 2000: |
---|
123 | # break |
---|
124 | to_edit = len(d) |
---|
125 | logger.info("found %d correct state/lga combinations of %d" % (to_edit,count)) |
---|
126 | |
---|
127 | edited = 1 |
---|
128 | for student_id,lga in d.items(): |
---|
129 | msg = "set clearance.lga to %s for %s" % (lga,student_id) |
---|
130 | getattr(getattr(students_folder,student_id),'clearance').getContent().edit(mapping={'lga':lga}) |
---|
131 | logger.info(msg) |
---|
132 | edited += 1 |
---|
133 | if edited and not edited % commit_after: |
---|
134 | context.waeup_tool.doCommit() |
---|
135 | logger.info("Committing %d, %d of %d" % (commit_after,edited,to_edit)) |
---|
136 | logger.info('finished, %d checked, %d edited' % (count, |
---|
137 | edited,)) |
---|
138 | |
---|