[2978] | 1 | ## Script (Python) "fix_lgas" |
---|
| 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 | """ |
---|
| 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.fix_lgas') |
---|
| 28 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
| 29 | aq_portal = context.portal_catalog_real.evalAdvancedQuery |
---|
| 30 | aq_students = context.students_catalog.evalAdvancedQuery |
---|
| 31 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
| 32 | |
---|
| 33 | request = context.REQUEST |
---|
| 34 | session = request.SESSION |
---|
| 35 | response = request.RESPONSE |
---|
| 36 | setheader = request.RESPONSE.setHeader |
---|
| 37 | def rwrite(s): |
---|
| 38 | response.setHeader('Content-type','text/html; charset=ISO-8859-15') |
---|
| 39 | response.write("%s<br>\n\r" % s) |
---|
| 40 | |
---|
| 41 | def 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('') |
---|
| 64 | student_records = context.students_catalog() |
---|
| 65 | total = len(student_records) |
---|
| 66 | logger.info("found %d students" % total) |
---|
| 67 | count = 0 |
---|
[2979] | 68 | commit_after = 100 |
---|
| 69 | logger.info('started to fix %d records in students_catalog' % total) |
---|
[2978] | 70 | d,states,lgas = getLGAs() |
---|
| 71 | d = {} |
---|
| 72 | for student_record in student_records: |
---|
| 73 | if not student_record.lga: |
---|
| 74 | continue |
---|
| 75 | if student_record.lga.find('_') > -1: |
---|
| 76 | continue |
---|
| 77 | if student_record.lga.startswith('no state'): |
---|
[2979] | 78 | logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id)) |
---|
[2978] | 79 | continue |
---|
| 80 | #rwrite("%s: %s" % (student_record.id,student_record.lga)) |
---|
| 81 | sl = student_record.lga.split('/') |
---|
| 82 | if len(sl) != 2: |
---|
[2979] | 83 | logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id)) |
---|
[2978] | 84 | #rwrite("wrong size %s: %s" % (student_record.id,sl)) |
---|
| 85 | continue |
---|
| 86 | state = sl[0].lower().strip() |
---|
| 87 | lga = sl[1].lower().strip() |
---|
| 88 | if not lga or not state: |
---|
[2979] | 89 | logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id)) |
---|
[2978] | 90 | continue |
---|
| 91 | count += 1 |
---|
| 92 | if len(d) and not len(d) % commit_after: |
---|
[2979] | 93 | logger.info("found %d correct of %d so far" % (len(d),count)) |
---|
[2978] | 94 | state_lga = state + '_' + lga |
---|
| 95 | if state in states: |
---|
| 96 | found = False |
---|
| 97 | while True: |
---|
| 98 | if lga in lgas: |
---|
| 99 | found = True |
---|
| 100 | break |
---|
| 101 | lga = lga.replace(' ','-') |
---|
| 102 | if lga in lgas: |
---|
| 103 | found = True |
---|
| 104 | break |
---|
| 105 | if found: |
---|
[2979] | 106 | #msg = "found %s for %s" % (state_lga,student_record.id) |
---|
[2978] | 107 | d[student_record.id] = state_lga |
---|
[2979] | 108 | #else: |
---|
| 109 | #msg = "no '%s' '%s' for %s" % (state,lga,student_record.id) |
---|
[2978] | 110 | #rwrite(msg) |
---|
[2979] | 111 | #else: |
---|
| 112 | #msg = "no state '%s' '%s' for %s" % (state,lga,student_record.id) |
---|
[2978] | 113 | #rwrite(msg) |
---|
| 114 | # if count > 150: |
---|
| 115 | # break |
---|
| 116 | to_edit = len(d) |
---|
| 117 | logger.info("found %d correct lgas of %d" % (to_edit,count)) |
---|
| 118 | #return |
---|
| 119 | |
---|
| 120 | edited = 1 |
---|
| 121 | for student_id,lga in d.items(): |
---|
| 122 | msg = "set clearance.lga to %s for %s" % (lga,student_id) |
---|
| 123 | getattr(getattr(students_folder,student_id),'clearance').getContent().edit(mapping={'lga':lga}) |
---|
| 124 | logger.info(msg) |
---|
| 125 | edited += 1 |
---|
| 126 | if edited and not edited % commit_after: |
---|
| 127 | context.waeup_tool.doCommit() |
---|
| 128 | logger.info("Committing %d, %d of %d" % (commit_after,edited,to_edit)) |
---|
| 129 | logger.info('finished, %d checked, %d edited' % (count, |
---|
| 130 | edited,)) |
---|
| 131 | |
---|