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

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

should fix all fixable, some lgas have been added some edited to/in
local_gov_areas.xml

File size: 4.4 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
64difference = 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
71commit_after = 100
72logger.info('started to fix %d records in students_catalog' % total)
73mapping = context.waeup_tool.getStatesLgas()
74states = mapping['states']
75lgas = mapping['lgas']
76words_dict = mapping['word_dict']
77d = {}
78for student_record in student_records:
79    # skip record without lga value
80    count += 1
81    if not student_record.lga:
82        continue
83    # if student_record.lga.find(' ') < 0:
84    #     continue
85    if student_record.lga.startswith('no state'):
86        #logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id))
87        continue
88    #rwrite("%s: %s" % (student_record.id,student_record.lga))
89    words = student_record.lga
90    if len(words.split(' / ')) == 2:
91        words = words.replace(' / ',' ')
92    words = re_split('[^a-zA-Z0-9/]',words)
93    lga_words = []
94    for word in words:
95        if word:
96            lga_words += word.lower(),
97    lga_words.sort()
98    # state = sl[0].lower().strip()
99    # state = '_'.join(state.split())
100    # lga = sl[1].lower().strip()
101    # if not lga or not state:
102    #     logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id))
103    #     continue
104    state_lga = ''
105    while not state_lga:
106        for k,l in words_dict.items():
107            if lga_words == l:
108                state_lga = k
109                break
110        break
111    if state_lga:
112        if state_lga != student_record.lga:
113            msg = "found %s is %s for %s " % (state_lga,student_record.lga,student_record.id)
114            #rwrite(msg)
115            d[student_record.id] = state_lga
116            if len(d) and not len(d)  % commit_after:
117                logger.info("found %d to correct state/lga combinations of %d so far" % (len(d),count))
118        else:
119            msg = "already corrected %s : %s for %s " % (state_lga,student_record.lga,student_record.id)
120    else:
121        msg = "no '%s' for %s" % (lga_words,student_record.id)
122    # if count > 2000:
123    #     break
124to_edit = len(d)
125logger.info("found %d correct state/lga combinations of %d" % (to_edit,count))
126
127edited = 1
128for student_id,lga in d.items():
129    msg = "set clearance.lga to %s for %s" % (lga,student_id)
130    getattr(getattr(students_folder,student_id),'clearance').getContent().edit(mapping={'lga':lga})
131    logger.info(msg)
132    edited += 1
133    if edited and not edited  % commit_after:
134        context.waeup_tool.doCommit()
135        logger.info("Committing %d,  %d of %d" % (commit_after,edited,to_edit))
136logger.info('finished, %d checked, %d edited' % (count,
137                                               edited,))
138
Note: See TracBrowser for help on using the repository browser.