source: WAeUP_SRP/base/skins/waeup_utilities/ti_441_resolve.py @ 2972

Last change on this file since 2972 was 2972, checked in by joachim, 17 years ago

enhencement to insert correct lgas into clearance_object fix for #441

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
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 2972 2008-01-03 10:58:29Z joachim $
11"""
12"""
13try:
14    from Products.zdb import set_trace
15except:
16    def set_trace():
17        pass
18
19mtool = context.portal_membership
20member = mtool.getAuthenticatedMember()
21if str(member) not in ('admin','joachim'):
22    return
23
24
25import logging
26import DateTime
27logger = logging.getLogger('Skins.ti_441_resolve')
28from Products.AdvancedQuery import Eq, Between, Le,In
29aq_portal = context.portal_catalog_real.evalAdvancedQuery
30aq_students = context.students_catalog.evalAdvancedQuery
31students_folder = context.portal_url.getPortalObject().campus.students
32
33request = context.REQUEST
34session = request.SESSION
35response = request.RESPONSE
36setheader = request.RESPONSE.setHeader
37def rwrite(s):
38    response.setHeader('Content-type','text/html; charset=ISO-8859-15')
39    response.write("%s<br>\n\r" % s)
40
41def 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('')
64query = Eq('lga','')
65student_records = aq_students(query)
66total = len(student_records)
67logger.info("found %d with empty clearance lga" % total)
68count = 0
69commit_after = 50
70logger.info('started to fix %d lga' % total)
71d,states,lgas = getLGAs()
72d = {}
73for student_record in student_records:
74    count += 1
75    if len(d) and not len(d)  % commit_after:
76        logger.info("found %d correct of  %d sofar" % (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
90logger.info("found %d correct lgas" % len(d))
91edited = 1
92for student_id,lga in d.items():
93    msg = "set clearance.lga to %s for %s" % (lga,student_record.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))
100logger.info('finished, %d checked, %d edited' % (count,
101                                               edited,))
102
Note: See TracBrowser for help on using the repository browser.