## Script (Python) "admitStudents"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# $Id: admitStudents.py 2727 2007-11-21 14:12:30Z joachim $
"""
"""
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

entry_session = '07'
pin_password = False

import logging
import DateTime
logger = logging.getLogger('Skins.admitStudents')
from Products.AdvancedQuery import Eq, Between, Le,In
aq_applicants = context.applicants_catalog.evalAdvancedQuery
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<br>\n\r" % s)

brains = aq_applicants(In('status',('admitted'),))
total = len(brains)
logger.info("found %d students" % (total))
count = 0
admitted = 0
cocount = 0
commit_after = 20
d = {'status': 'created'}
reg_nos = [brain.reg_no for brain in brains]
for reg_no in reg_nos:
    count += 1
    if admitted and not admitted % commit_after:
        context.waeup_tool.doCommit()
        logger.info("Committing %s transactions, total %s" % (commit_after,count))
        # cocount += 1
        # if cocount > 1:
        #     break
    brain = aq_applicants(Eq('reg_no',reg_no))[0]
    #logger.info("start creating  objects of student %s" % (brain.reg_no))
    sid = d['student_id'] = context.waeup_tool.admitOneStudent(brain,entry_session,pin_password)
    if sid is not None:
        d['reg_no'] = reg_no
        context.applicants_catalog.modifyRecord(**d)
        logger.info("created objects of student %s with id %s" % (reg_no,sid))
        admitted += 1
    else:
        logger.info("could not create objects of student %s with pin %s" % (reg_no,brain.pin))

msg = "finished admitting %d students" % (admitted)
logger.info(msg)
#rwrite(msg)
return
