## Script (Python) "fix_lgas" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters= ##title= ## # $Id: fix_lgas.py 2979 2008-01-05 15:01:33Z henrik $ """ """ try: from Products.zdb import set_trace except: def set_trace(): pass mtool = context.portal_membership member = mtool.getAuthenticatedMember() if str(member) not in ('admin','joachim'): return import logging import DateTime logger = logging.getLogger('Skins.fix_lgas') from Products.AdvancedQuery import Eq, Between, Le,In aq_portal = context.portal_catalog_real.evalAdvancedQuery aq_students = context.students_catalog.evalAdvancedQuery students_folder = context.portal_url.getPortalObject().campus.students request = context.REQUEST session = request.SESSION response = request.RESPONSE setheader = request.RESPONSE.setHeader def rwrite(s): response.setHeader('Content-type','text/html; charset=ISO-8859-15') response.write("%s
\n\r" % s) def getLGAs(): voc = getattr(context.portal_vocabularies,'local_gov_areas') states = [] lgas = [] d = {} for k,v in voc.items(): parts = v.split(' / ') if len(parts) == 1: state = parts[0].lower() lga = "" elif len(parts) == 2: state = parts[0].lower() lga = "_".join(parts[1].lower().split()) else: continue if state not in states: states.append(state) if lga not in lgas: lgas.append(lga) d[k] = v return (d,states,lgas) #logger.info('') student_records = context.students_catalog() total = len(student_records) logger.info("found %d students" % total) count = 0 commit_after = 100 logger.info('started to fix %d records in students_catalog' % total) d,states,lgas = getLGAs() d = {} for student_record in student_records: if not student_record.lga: continue if student_record.lga.find('_') > -1: continue if student_record.lga.startswith('no state'): logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id)) continue #rwrite("%s: %s" % (student_record.id,student_record.lga)) sl = student_record.lga.split('/') if len(sl) != 2: logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id)) #rwrite("wrong size %s: %s" % (student_record.id,sl)) continue state = sl[0].lower().strip() lga = sl[1].lower().strip() if not lga or not state: logger.info("found invalid lga %s of %s" % (student_record.lga,student_record.id)) continue count += 1 if len(d) and not len(d) % commit_after: logger.info("found %d correct of %d so far" % (len(d),count)) state_lga = state + '_' + lga if state in states: found = False while True: if lga in lgas: found = True break lga = lga.replace(' ','-') if lga in lgas: found = True break if found: #msg = "found %s for %s" % (state_lga,student_record.id) d[student_record.id] = state_lga #else: #msg = "no '%s' '%s' for %s" % (state,lga,student_record.id) #rwrite(msg) #else: #msg = "no state '%s' '%s' for %s" % (state,lga,student_record.id) #rwrite(msg) # if count > 150: # break to_edit = len(d) logger.info("found %d correct lgas of %d" % (to_edit,count)) #return edited = 1 for student_id,lga in d.items(): msg = "set clearance.lga to %s for %s" % (lga,student_id) getattr(getattr(students_folder,student_id),'clearance').getContent().edit(mapping={'lga':lga}) logger.info(msg) edited += 1 if edited and not edited % commit_after: context.waeup_tool.doCommit() logger.info("Committing %d, %d of %d" % (commit_after,edited,to_edit)) logger.info('finished, %d checked, %d edited' % (count, edited,))