1 | ## Script (Python) "copyLGA" |
---|
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 | logger = logging.getLogger('Skins.copyLGA') |
---|
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 | |
---|
39 | re_split = context.waeup_tool.re_split |
---|
40 | query = Eq('lga','') |
---|
41 | student_records = aq_students(query) |
---|
42 | total = len(student_records) |
---|
43 | logger.info("found %d records with empty clearance lga" % total) |
---|
44 | count = 0 |
---|
45 | commit_after = 100 |
---|
46 | logger.info('started to fix %d records' % total) |
---|
47 | mapping = context.waeup_tool.getStatesLgas() |
---|
48 | states = mapping['states'] |
---|
49 | lgas = mapping['lgas'] |
---|
50 | words_dict = mapping['word_dict'] |
---|
51 | d = {} |
---|
52 | for student_record in student_records: |
---|
53 | count += 1 |
---|
54 | jamb_state = getattr(getattr(students_folder,student_record.id),'application').getContent().jamb_state |
---|
55 | jamb_lga = getattr(getattr(students_folder,student_record.id),'application').getContent().jamb_lga |
---|
56 | words = ' '.join((jamb_state, jamb_lga)) |
---|
57 | state_lga = context.waeup_tool.findLga(words,words_dict) |
---|
58 | if state_lga: |
---|
59 | msg = "found %s for %s " % (state_lga,student_record.id) |
---|
60 | logger.info(msg) |
---|
61 | d[student_record.id] = state_lga |
---|
62 | if len(d) and not len(d) % commit_after: |
---|
63 | logger.info("found %d to correct state/lga combinations of %d so far" % (len(d),count)) |
---|
64 | else: |
---|
65 | msg = "no '%s' for %s" % (words,student_record.id) |
---|
66 | logger.info("found %d correct lga values" % len(d)) |
---|
67 | edited = 1 |
---|
68 | for student_id,lga in d.items(): |
---|
69 | msg = "set clearance.lga to %s for %s" % (lga,student_id) |
---|
70 | getattr(getattr(students_folder,student_id),'clearance').getContent().edit(mapping={'lga':lga}) |
---|
71 | logger.info(msg) |
---|
72 | edited += 1 |
---|
73 | if edited and not edited % commit_after: |
---|
74 | context.waeup_tool.doCommit() |
---|
75 | logger.info("Committing %d, %d" % (commit_after,edited)) |
---|
76 | logger.info('finished, %d checked, %d edited' % (count, |
---|
77 | edited,)) |
---|
78 | |
---|