##parameters=REQUEST,matric_no=None
# $Id: set_access_data.py 3367 2008-03-21 13:29:31Z henrik $
"""
process the Application Form
return html renderer + psm
"""
import DateTime,logging
current = DateTime.DateTime()
pr = context.portal_registration
logger = logging.getLogger('Skins.set_access_data')
validate = REQUEST.has_key("cpsdocument_edit_button")

if matric_no is None:
    logger.info('No matric_no in request')
    return REQUEST.RESPONSE.redirect("%s" % context.portal_url())
matric_no = matric_no.upper()
lt = context.portal_layouts
#pr = context.portal_registration
default = {'matric_no':matric_no}
st_res = context.students_catalog(matric_no = matric_no)
if st_res:
    sid = st_res[0].id
else:
    st_res = context.returning_import(matric_no = matric_no)
    if st_res:
        sid = st_res[0].id
        logger.info('Student object with matric_no %s does not exist in students_catalog, Id taken from returning_import' % matric_no)
    else:
        logger.info('Student object with matric_no %s neither exists in students_catalog nor in returning_import' % matric_no)
        return
res,psm,ds = lt.renderLayout(layout_id= 'student_firsttime_login',
                      schema_id= 'student_returning',
                      context=context,
                      mapping=validate and REQUEST,
                      ob=default,
                      layout_mode='edit',
                      formaction = "set_access_data",
                      button = "Search",
                      matric_no = matric_no,
                      commit = False,
                      )
if psm == '':
    return context.set_access_data_form(rendered = res,
                                 psm = None,
                                 firstlayout = True,
                                 lastlayout = True,
                                 ds = ds,
                                 name = "%(name)s" % REQUEST.form
                                 )
elif psm == 'invalid':
    psm = "Please correct your input!"
elif psm == 'valid' and not REQUEST.form.has_key('acknowledge'):
    psm = "You did not check the ackkowledge box!"
else:
    password = REQUEST.get('widget__password')
    try:
        context.waeup_tool.makeStudentMember(sid,password=password)
        psm = ''
    except KeyError:
        #psm = "You are already registered please log in with your UserId %s" % sid
        psm = ""
        logger.info('%s repeatedly tried to set password' % sid)
    except:
        psm = "The system encountered an error. Please try again."
        logger.info('%s, makeStudentMember error, password = %s' % (sid,password))
if psm != '':
    return context.set_access_data_form(rendered = res,
                                 #psm = "%s, %s" % (psm,ds),
                                 psm = psm,
                                 firstlayout = True,
                                 lastlayout = True,
                                 ds = ds,
                                 name = "%(name)s" % REQUEST.form
                                 )
email = ds.get('email')
args = {}
args['__ac_name'] = sid
args['__ac_password'] = password
args['returning'] = '1'
args['email'] = email
args['phone_nr'] = ds.get('phone_nr')
from urllib import urlencode
url = "%s/logged_in?%s" % (context.absolute_url(),urlencode(args))
return REQUEST.RESPONSE.redirect(url)
