##parameters=REQUEST
# $Id: apply_admission.py 511 2006-09-13 06:47:55Z joachim $
"""
process the Application Form
return html renderer + psm
"""
import DateTime
current = DateTime.DateTime()
pr = context.portal_registration


type_name = 'StudentApplication'
ti = context.portal_types[type_name]
REQUEST.set('type_name',type_name)

validate = REQUEST.has_key("cpsdocument_create_button")

lt = context.portal_layouts
pr = context.portal_registration

res,psm,ds = lt.renderLayout(layout_id= 'student_application_fe',
                      schema_id= 'student_application',
                      context=context,
                      mapping=validate and REQUEST,
                      ob={},
                      layout_mode='create',
                      formaction = "apply_admission",
                      button = "Open JAMB Record",
                      )
if psm == 'invalid':
    return context.application_pin_form(rendered = res,
                                 psm = "Please correct your input.",
                                 #psm = "%s, %s" % (psm,ds),
                                 firstlayout = True,
                                 lastlayout = True,
                                 ds = ds,
                                 )
elif psm == '':
    return context.application_pin_form(rendered = res,
                                 psm = None,
                                 firstlayout = True,
                                 lastlayout = True,
                                 ds = ds,
                                 )
elif psm == 'valid':
    s_id = ds.get('s_id')
    pin = str(ds.get('app_ac_pin'))
    return context.REQUEST.RESPONSE.redirect("%s/logged_in?__ac_name=%s&__ac_password=%s&pin=%s"
          % (context.absolute_url(),s_id,pin.split('-')[2],pin))
    
##    jamb_id = ds.get('jamb_reg_no')
##    student = context.getContent().getStudentByRegNo(jamb_id)
##    if student is None:
##        psm = "JAMB record %s not accessible." % (jamb_id)
##        return context.application_pin_form(rendered = res,
##                                 psm = psm,
##                                 #psm = "%s, %s" % (psm,ds),
##                                 ds = ds,
##                                 )
##if context.portal_workflow.getInfoFor(student,'review_state',None) == "created":
##    #student.invokeFactory('StudentClearance','clearance')
##da = {}
##pin = str(ds.get('app_ac_pin'))
##da['app_ac_pin'] = pin
##da['app_ac_date'] = current
##apdoc.edit(mapping = da)


# now display the passport form with jamb-data readonly
return context.REQUEST.RESPONSE.redirect("%s/application_form" % (student.application.absolute_url()))
return student.application_form()


