##parameters=REQUEST
# $Id: add_student.py 2538 2007-11-05 16:19:18Z joachim $
"""
process the the accommodation reservation
"""
import DateTime
current = DateTime.DateTime()
pr = context.portal_registration
wftool = context.portal_workflow
lt = context.portal_layouts
mtool = context.portal_membership
member = mtool.getAuthenticatedMember()
acco_cat = context.portal_accommodation
import logging
logger = logging.getLogger('Skins.add_student')

if mtool.isAnonymousUser() or not context.isSectionOfficer():
    return REQUEST.RESPONSE.redirect("%s/srp_anonymous_view" % context.portal_url())

validate = REQUEST.has_key("cpsdocument_edit_button") and\
           REQUEST.get("cpsdocument_edit_button").startswith('Add')
d = {}
mode = 'edit'
rendered,psm,ds = lt.renderLayout(layout_id= 'add_student',
                      schema_id= 'add_student',
                      context=context,
                      mapping=validate and REQUEST,
                      ob=d,
                      layout_mode=mode,
                      commit = False
                      )
if psm == 'invalid':
    psm = "Please correct your input."
    return context.add_student_form(rendered = rendered,
                                 psm = psm,
                                 #psm = "%s, %s" % (psm,ds),
                                 mode = mode,
                                 formaction = "add_student",
                                 button = "Add",
                                 ds = ds,
                                 )
elif psm == '':
    return context.add_student_form(rendered = rendered,
                                 psm = psm,
                                 mode = mode,
                                 formaction = "add_student",
                                 button = "Add",
                                 ds = ds,
                                 )
elif psm == 'valid':
    pass
matric_no = ds.get('matric_no')
if matric_no:
    res = context.students_catalog(matric_no = matric_no)
    if res:
        psm = "Student with matric_no %s exists with Id: %s" % (matric_no,res[0].id)
        return context.add_student_form(rendered = rendered,
                                 psm = psm,
                                 #psm = "%s, %s" % (psm,ds),
                                 mode = mode,
                                 formaction = "add_student",
                                 button = "Add",
                                 ds = ds,
                                 )
dict = {}
dict.update(ds)
sid,password = context.waeup_tool.addStudent(dict)
dict['sid'] = sid
dict['password'] = password
student_rec = context.getFormattedStudentEntry(context.students_catalog(id = sid)[0])
psm = "Student record with Id %s and Password %s has been created." % (sid,password)
logger.info('%s created student %s' % (member,sid))
return context.add_student_form(rendered = rendered,
                                 mode = 'view',
                                 psm = psm,
                                 ds = dict,
                                 student = student_rec,
                                 )


