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

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

WAeUP LGA Widget uses new algorithm
ti_441_resolve_part1 + part2 also
part2 can now be used to rename lgas, a list of all not found lgas is
printed at the end.

File size: 5.0 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
19
20mtool = context.portal_membership
21member = mtool.getAuthenticatedMember()
22if str(member) not in ('admin','joachim'):
23    return
24
25
26import logging
27import DateTime
28logger = logging.getLogger('Skins.ti_441_resolve_part2')
29from Products.AdvancedQuery import Eq, Between, Le,In
30aq_portal = context.portal_catalog_real.evalAdvancedQuery
31aq_students = context.students_catalog.evalAdvancedQuery
32students_folder = context.portal_url.getPortalObject().campus.students
33
34request = context.REQUEST
35session = request.SESSION
36response = request.RESPONSE
37setheader = request.RESPONSE.setHeader
38def rwrite(s):
39    response.setHeader('Content-type','text/html; charset=ISO-8859-15')
40    response.write("%s<br>\n\r" % s)
41
42# def getLGAs():
43#     voc = getattr(context.portal_vocabularies,'local_gov_areas')
44#     states = []
45#     lgas  = []
46#     d = {}
47#     for k,v in voc.items():
48#         parts = v.split(' / ')
49#         if len(parts) == 1:
50#             state = parts[0].lower()
51#             lga = ""
52#         elif len(parts) == 2:
53#             state = parts[0].lower()
54#             lga = "_".join(parts[1].lower().split())
55#         else:
56#             continue
57#         if state not in states:
58#             states.append(state)
59#         if lga not in lgas:
60#             lgas.append(lga)
61#         d[k] = v
62#     return (d,states,lgas)
63
64#difference = context.waeup_tool.difference
65re_split = context.waeup_tool.re_split
66#logger.info('')
67student_records = context.students_catalog()
68total = len(student_records)
69logger.info("found %d students" % total)
70count = 0
71correctable = wrong = 1
72commit_after = 100
73logger.info('started to fix %d records in students_catalog' % total)
74mapping = context.waeup_tool.getStatesLgas()
75states = mapping['states']
76lgas = mapping['lgas']
77words_dict = mapping['word_dict']
78d = {}
79not_found_lgas = []
80not_found_count = {}
81rename_lgas = {}
82rename_lgas['edo_orhionmwon'] = 'edo_ohionmwon'
83for student_record in student_records:
84    # skip record without lga value
85    count += 1
86    if not student_record.lga:
87        continue
88    # if student_record.lga.find(' ') < 0:
89    #     continue
90    if student_record.lga.startswith('no state'):
91        #logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id))
92        continue
93    #rwrite("%s: %s" % (student_record.id,student_record.lga))
94    if student_record.lga in rename_lgas.keys():
95        correctable += 1
96        d[student_record.id] = rename_lgas[student_record.lga]
97        if len(d) and not len(d)  % commit_after:
98            logger.info("found %d to correct state/lga combinations of %d so far" % (len(d),count))
99        continue
100    words = student_record.lga
101    if len(words.split(' / ')) == 2:
102        words = words.replace(' / ',' ')
103    state_lga = context.waeup_tool.findLga(words,words_dict)
104    if state_lga:
105        if state_lga != student_record.lga:
106            correctable += 1
107            msg = "found %s is %s for %s " % (state_lga,student_record.lga,student_record.id)
108            #rwrite(msg)
109            d[student_record.id] = state_lga
110            if len(d) and not len(d)  % commit_after:
111                logger.info("found %d to correct state/lga combinations of %d so far" % (len(d),count))
112        else:
113            msg = "already corrected %s : %s for %s " % (state_lga,student_record.lga,student_record.id)
114    else:
115        wrong += 1
116        msg = "no '%s' for %s" % (words,student_record.id)
117        if len(words) > 2:
118            if words not in not_found_lgas:
119                not_found_lgas += words,
120                not_found_count[words] = 1
121            else:
122                nfc = not_found_count[words]
123                nfc += 1
124                not_found_count[words] = nfc
125        #rwrite(msg)
126    # if count > 2000:
127    #     break
128not_found_lgas.sort()
129for lga in not_found_lgas:
130    msg = "not found %s count: %d" % (lga,not_found_count[lga])
131    logger.info(msg)
132to_edit = len(d)
133logger.info("found %d correctable state/lga combinations, not correctable %d total %d" % (correctable,
134                                                                                          wrong,
135                                                                                          count))
136edited = 1
137for student_id,lga in d.items():
138    msg = "set clearance.lga to %s for %s" % (lga,student_id)
139    getattr(getattr(students_folder,student_id),'clearance').getContent().edit(mapping={'lga':lga})
140    logger.info(msg)
141    edited += 1
142    if edited and not edited  % commit_after:
143        context.waeup_tool.doCommit()
144        logger.info("Committing %d,  %d of %d" % (commit_after,edited,to_edit))
145logger.info('finished, %d checked, %d edited' % (count,
146                                               edited,))
147
Note: See TracBrowser for help on using the repository browser.