source: WAeUP_SRP/base/skins/waeup_utilities/ti_441_resolve_part2.py @ 2983

Last change on this file since 2983 was 2982, checked in by Henrik Bettermann, 17 years ago

set key value correctly (the key value was often wrong!)

File size: 4.2 KB
Line 
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"""
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_part2')
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('')
64student_records = context.students_catalog()
65total = len(student_records)
66logger.info("found %d students" % total)
67count = 0
68commit_after = 100
69logger.info('started to fix %d records in students_catalog' % total)
70d,states,lgas = getLGAs()
71d = {}
72for student_record in student_records:
73    # skip record without lga value
74    if not student_record.lga:
75        continue
76    #skip records with correct lga key value
77    if student_record.lga.find('_') > -1:
78        continue
79    if student_record.lga.startswith('no state'):
80        logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id))
81        continue
82    #rwrite("%s: %s" % (student_record.id,student_record.lga))
83    sl = student_record.lga.split(' / ')
84    if len(sl) != 2:
85        sl = student_record.lga.split('/')
86        if len(sl) != 2:
87            logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id))
88            #rwrite("wrong size %s: %s" % (student_record.id,sl))
89            continue
90    state = sl[0].lower().strip()
91    lga = sl[1].lower().strip()
92    if not lga or not state:
93        logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id))
94        continue
95    count += 1
96    if len(d) and not len(d)  % commit_after:
97        logger.info("found %d correct state/lga combinations of %d so far" % (len(d),count))
98    if state in states:
99        found = False
100        while True:
101            if lga in lgas:
102                found = True
103                break
104            lga = lga.replace(' ','-')
105            if lga in lgas:
106                found = True
107            break
108        if found:
109            #msg = "found %s for %s" % (state_lga,student_record.id)
110            state_lga = state + '_' + lga
111            d[student_record.id] = state_lga
112        #else:
113            #msg = "no '%s' '%s' for %s" % (state,lga,student_record.id)
114            #rwrite(msg)
115    #else:
116        #msg = "no state '%s' '%s' for %s" % (state,lga,student_record.id)
117        #rwrite(msg)
118    # if count > 150:
119    #     break
120to_edit = len(d)
121logger.info("found %d correct state/lga combinations of %d" % (to_edit,count))
122
123edited = 1
124for student_id,lga in d.items():
125    msg = "set clearance.lga to %s for %s" % (lga,student_id)
126    getattr(getattr(students_folder,student_id),'clearance').getContent().edit(mapping={'lga':lga})
127    logger.info(msg)
128    edited += 1
129    if edited and not edited  % commit_after:
130        context.waeup_tool.doCommit()
131        logger.info("Committing %d,  %d of %d" % (commit_after,edited,to_edit))
132logger.info('finished, %d checked, %d edited' % (count,
133                                               edited,))
134
Note: See TracBrowser for help on using the repository browser.