## Script (Python) "ti_441_resolve" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters= ##title= ## # $Id: ti_441_resolve.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.ti_441_resolve') 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('') query = Eq('lga','') student_records = aq_students(query) total = len(student_records) logger.info("found %d records with empty clearance lga" % total) count = 0 commit_after = 100 logger.info('started to fix %d records' % total) d,states,lgas = getLGAs() d = {} for student_record in student_records: count += 1 if len(d) and not len(d) % commit_after: logger.info("found %d correct lga values of %d so far" % (len(d),count)) jamb_state = getattr(getattr(students_folder,student_record.id),'application').getContent().jamb_state jamb_lga = getattr(getattr(students_folder,student_record.id),'application').getContent().jamb_lga jamb_state = jamb_state.lower() jamb_lga = jamb_lga.lower() lga = jamb_state + '_' + jamb_lga if jamb_state and jamb_lga and jamb_state in states and jamb_lga in lgas: d[student_record.id] = lga #else: # msg = "no %s for %s" % (lga,student_record.id) # pass # rwrite(msg) # if count > 150: # break logger.info("found %d correct lga values" % len(d)) 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" % (commit_after,edited)) logger.info('finished, %d checked, %d edited' % (count, edited,))