## Script (Python) "apply_admission_manage"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=REQUEST
##title=
##
# $Id: apply_admission_manage.py 2225 2007-09-12 21:04:51Z henrik $
"""
search for  Pume Applications
"""

try:
    from Products.zdb import set_trace
except:
    def set_trace():
        pass

import DateTime
import logging
logger = logging.getLogger('Skins.apply_admission_manage')
mtool = context.portal_membership
member = mtool.getAuthenticatedMember()

current = DateTime.DateTime()
pr = context.portal_registration
request = REQUEST


if not context.isSectionOfficer():
    return request.RESPONSE.redirect("%s/srp_anonymous_view" % context.absolute_url())

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

search = "search" in request.keys()
reset = "reset" in request.keys()
edit = "edit" in request.keys()
lt = context.portal_layouts

reg_no = request.get('reg_no','').upper()
new_reg_no = request.get('new_reg_no','').upper()

if reset or edit:
    reg_no = reg_no
elif new_reg_no:
    reg_no = new_reg_no

res = ''
psm = ''
object = {}
ds = []
found = False

validate = not search

if reg_no:
    brains = context.applicants_catalog(reg_no = reg_no)

    if len(brains) == 1:
        found = True
        for field in context.applicants_catalog.schema():
            object[field] = getattr(brains[0],field,None)
        res,psm,ds = lt.renderLayout(layout_id= 'application_manage',
                                     schema_id= 'import_application',
                                     layout_mode = "edit",
                                     context=context,
                                     mapping=validate and REQUEST,
                                     ob=object,
                                     commit = False,
                                    )


if psm == 'invalid':
    return context.apply_admission_manage_form(rendered = res,
                                   psm = "Please correct your input!",
                                   #psm = "%s, %s" % (psm,ds),
                                   ds = ds,
                                  )
elif psm == '':
    if search and not found:
        psm = 'Registration Number not found!'
    return context.apply_admission_manage_form(rendered = res,
                                   psm = psm,
                                   reg_no = reg_no,
                                   ds = ds,
                                  )
elif psm == 'valid':
    psm = ''
    pass

data = {}
dm = ds.getDataModel()
for field in context.applicants_catalog.schema():
    if dm.has_key("%s" % field):
        data[field] = dm.get(field)


if reset and reg_no:
    #data['reg_no'] = reg_no
    psm = "You successfully reset the record! The applicant will be able to edit and resubmit the data."
    data['status'] = "reset"
    context.applicants_catalog.modifyRecord(**data)
    logger.info('%s reset application record of %s' % (member,reg_no))

if edit and reg_no:
    #data['reg_no'] = reg_no
    psm = "Content changed!"
    context.applicants_catalog.modifyRecord(**data)
    logger.info('%s edited application record of %s' % (member,reg_no))


brains = context.applicants_catalog(reg_no = reg_no)
for field in context.applicants_catalog.schema():
    object[field] = getattr(brains[0],field,None)

res,psm_dummy,ds = lt.renderLayout(layout_id= 'application_manage',
                             schema_id= 'import_application',
                             layout_mode = "edit",
                             context=context,
                             ob=object,
                             commit = False,
                            )


return context.apply_admission_manage_form(rendered = res,
                                      psm = psm,
                                      reg_no = reg_no,
                                      ds = ds
                                     )


