1 | ## Script (Python) "ti_441_resolve" |
---|
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') |
---|
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 | def getLGAs(): |
---|
42 | voc = getattr(context.portal_vocabularies,'local_gov_areas') |
---|
43 | states = [] |
---|
44 | lgas = [] |
---|
45 | d = {} |
---|
46 | for k,v in voc.items(): |
---|
47 | parts = v.split(' / ') |
---|
48 | if len(parts) == 1: |
---|
49 | state = parts[0].lower() |
---|
50 | lga = "" |
---|
51 | elif len(parts) == 2: |
---|
52 | state = parts[0].lower() |
---|
53 | lga = "_".join(parts[1].lower().split()) |
---|
54 | else: |
---|
55 | continue |
---|
56 | if state not in states: |
---|
57 | states.append(state) |
---|
58 | if lga not in lgas: |
---|
59 | lgas.append(lga) |
---|
60 | d[k] = v |
---|
61 | return (d,states,lgas) |
---|
62 | |
---|
63 | #logger.info('') |
---|
64 | query = Eq('lga','') |
---|
65 | student_records = aq_students(query) |
---|
66 | total = len(student_records) |
---|
67 | logger.info("found %d records with empty clearance lga" % total) |
---|
68 | count = 0 |
---|
69 | commit_after = 100 |
---|
70 | logger.info('started to fix %d records' % total) |
---|
71 | d,states,lgas = getLGAs() |
---|
72 | d = {} |
---|
73 | for student_record in student_records: |
---|
74 | count += 1 |
---|
75 | if len(d) and not len(d) % commit_after: |
---|
76 | logger.info("found %d correct lga values of %d so far" % (len(d),count)) |
---|
77 | jamb_state = getattr(getattr(students_folder,student_record.id),'application').getContent().jamb_state |
---|
78 | jamb_lga = getattr(getattr(students_folder,student_record.id),'application').getContent().jamb_lga |
---|
79 | jamb_state = jamb_state.lower() |
---|
80 | jamb_lga = jamb_lga.lower() |
---|
81 | lga = jamb_state + '_' + jamb_lga |
---|
82 | if jamb_state and jamb_lga and jamb_state in states and jamb_lga in lgas: |
---|
83 | d[student_record.id] = lga |
---|
84 | #else: |
---|
85 | # msg = "no %s for %s" % (lga,student_record.id) |
---|
86 | # pass |
---|
87 | # rwrite(msg) |
---|
88 | # if count > 150: |
---|
89 | # break |
---|
90 | logger.info("found %d correct lga values" % len(d)) |
---|
91 | edited = 1 |
---|
92 | for student_id,lga in d.items(): |
---|
93 | msg = "set clearance.lga to %s for %s" % (lga,student.id) |
---|
94 | getattr(getattr(students_folder,student_id),'clearance').getContent().edit(mapping={'lga':lga}) |
---|
95 | logger.info(msg) |
---|
96 | edited += 1 |
---|
97 | if edited and not edited % commit_after: |
---|
98 | context.waeup_tool.doCommit() |
---|
99 | logger.info("Committing %d, %d" % (commit_after,edited)) |
---|
100 | logger.info('finished, %d checked, %d edited' % (count, |
---|
101 | edited,)) |
---|
102 | |
---|