## Script (Python) "application_edit"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=REQUEST, cpsdocument_edit_button=None, cpsdocument_edit_and_view_button=None, action=None
##title=
# $Id: application_edit.py 2390 2007-10-19 09:20:48Z joachim $
"""
"""
wftool = context.portal_workflow
from urllib import urlencode
from Products.CPSDocument.utils import getFormUidUrlArg
import DateTime
current = DateTime.DateTime()
import logging
logger = logging.getLogger('Skins.application_edit')

# Until ajax posts directly to its own script...
##if 'ajax_edit' in REQUEST.form:
##    return context.cpsdocument_edit_ajax(REQUEST, cluster=cluster)

# Check flexible controls
#context.editLayouts(REQUEST=REQUEST)

# Validate the document and write it if it's valid
# (We don't call getEditableContent here, validate does it when needed.)
info = context.getApplicationInfo()

if info is None:
    logger.info('Anonymous user tried to access %s' % REQUEST.get('URL0'))
    return REQUEST.RESPONSE.redirect("%s/srp_anonymous_view" % context.portal_url())

student = info['student']
app = info['app']
app_doc = info['app_doc']
state = info['review_state']
is_valid, ds = app_doc.validate(request=REQUEST,
                                schema_id = 'student_application',
                                layout_id = 'student_application_fe',
                                proxy=app,
                                use_session=False)

psm = ""
args = {}
action = "/application_edit_form"
base_url = app.absolute_url()

if is_valid:
    if cpsdocument_edit_button:
        if app_doc.passport is not None:
            args['portal_status_message'] = 'You successfully uploaded your passport picture!'
        else:
            args['portal_status_message'] = "You didn't upload a passport picture!"
    elif cpsdocument_edit_and_view_button:
        if 1: ## app_doc.passport is not None:
            logger.info('%s proceeded to start clearance' % (info['id']))
            action = "/start_clearance"
            base_url = student.absolute_url()
            url = base_url + action
            return REQUEST.RESPONSE.redirect(url)
        else:
            args['portal_status_message'] = "You didn't upload a passport picture!"
if args:
  url = base_url + action + '?' + urlencode(args)
else:
  url = base_url + action
REQUEST.RESPONSE.redirect(url)
