1 | ## Script (Python) "ti_441_resolve_part1" |
---|
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: ti_441_resolve.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 | mtool = context.portal_membership |
---|
20 | member = mtool.getAuthenticatedMember() |
---|
21 | if str(member) not in ('admin','joachim'): |
---|
22 | return |
---|
23 | |
---|
24 | |
---|
25 | import logging |
---|
26 | import DateTime |
---|
27 | logger = logging.getLogger('Skins.ti_441_resolve_part1') |
---|
28 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
29 | aq_portal = context.portal_catalog_real.evalAdvancedQuery |
---|
30 | aq_students = context.students_catalog.evalAdvancedQuery |
---|
31 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
32 | |
---|
33 | request = context.REQUEST |
---|
34 | session = request.SESSION |
---|
35 | response = request.RESPONSE |
---|
36 | setheader = request.RESPONSE.setHeader |
---|
37 | def rwrite(s): |
---|
38 | response.setHeader('Content-type','text/html; charset=ISO-8859-15') |
---|
39 | response.write("%s<br>\n\r" % s) |
---|
40 | |
---|
41 | re_split = context.waeup_tool.re_split |
---|
42 | query = Eq('lga','') |
---|
43 | student_records = aq_students(query) |
---|
44 | total = len(student_records) |
---|
45 | logger.info("found %d records with empty clearance lga" % total) |
---|
46 | count = 0 |
---|
47 | commit_after = 100 |
---|
48 | logger.info('started to fix %d records' % total) |
---|
49 | mapping = context.waeup_tool.getStatesLgas() |
---|
50 | states = mapping['states'] |
---|
51 | lgas = mapping['lgas'] |
---|
52 | words_dict = mapping['word_dict'] |
---|
53 | d = {} |
---|
54 | for student_record in student_records: |
---|
55 | count += 1 |
---|
56 | jamb_state = getattr(getattr(students_folder,student_record.id),'application').getContent().jamb_state |
---|
57 | jamb_lga = getattr(getattr(students_folder,student_record.id),'application').getContent().jamb_lga |
---|
58 | words = ' '.join((jamb_state, jamb_lga)) |
---|
59 | # words = re_split('[^a-zA-Z0-9/]',words) |
---|
60 | # lga_words = [] |
---|
61 | # for word in words: |
---|
62 | # if word: |
---|
63 | # lga_words += word.lower(), |
---|
64 | # lga_words.sort() |
---|
65 | # state_lga = '' |
---|
66 | # while not state_lga: |
---|
67 | # for k,l in words_dict.items(): |
---|
68 | # if lga_words == l: |
---|
69 | # state_lga = k |
---|
70 | # break |
---|
71 | # break |
---|
72 | state_lga = context.waeup_tool.findLga(words,words_dict) |
---|
73 | if state_lga: |
---|
74 | msg = "found %s is %s for %s " % (state_lga,student_record.lga,student_record.id) |
---|
75 | #rwrite(msg) |
---|
76 | logger.info(msg) |
---|
77 | d[student_record.id] = state_lga |
---|
78 | if len(d) and not len(d) % commit_after: |
---|
79 | logger.info("found %d to correct state/lga combinations of %d so far" % (len(d),count)) |
---|
80 | else: |
---|
81 | msg = "no '%s' for %s" % (words,student_record.id) |
---|
82 | #logger.info(msg) |
---|
83 | #rwrite(msg) |
---|
84 | # if count > 150: |
---|
85 | # break |
---|
86 | logger.info("found %d correct lga values" % len(d)) |
---|
87 | edited = 1 |
---|
88 | for student_id,lga in d.items(): |
---|
89 | msg = "set clearance.lga to %s for %s" % (lga,student_id) |
---|
90 | getattr(getattr(students_folder,student_id),'clearance').getContent().edit(mapping={'lga':lga}) |
---|
91 | logger.info(msg) |
---|
92 | edited += 1 |
---|
93 | if edited and not edited % commit_after: |
---|
94 | context.waeup_tool.doCommit() |
---|
95 | logger.info("Committing %d, %d" % (commit_after,edited)) |
---|
96 | logger.info('finished, %d checked, %d edited' % (count, |
---|
97 | edited,)) |
---|
98 | |
---|