## Script (Python) "cpsdocument_edit"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=REQUEST, cluster=None, cpsdocument_edit_and_view_button=None, action=None
##title=
# $Id: student_edit.py 607 2006-10-02 05:31:35Z henrik $
"""
Called when a document form is posted.

Validates data, then:

 - if there's no error, updates the object and redirects to it,

 - if there's an error, puts data in session and redirects to edit form.

A form uid is propagated during the redirect to uniquely identify the
form in the session.
"""
wftool = context.portal_workflow
from urllib import urlencode
from Products.CPSDocument.utils import getFormUidUrlArg

# 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.getStudentInfo()

student = info['student']
app = info['app']
app_doc = info['app_doc']
state = context.getStudentInfo()['review_state']

if context.portal_type == "Student":
    if app_doc.passport is None:
        is_valid, ds = app_doc.validate(request=REQUEST,
                                    proxy=app_doc,
                                    layout_id = "student_application_fe",
                                    layout_mode = 'edit',
                                    use_session=True)
        action = "/passport_entry_view"
        if is_valid and app_doc.passport is not None:
            psm = 'You successfully uploaded your passport image.'
            args = {'apply_button': 'Apply',}
        else:
            psm = "You didn't upload a passport image."
            args = {}
    elif 'apply_admission' not in REQUEST.form:
        is_valid, ds = app_doc.validate(request=REQUEST,
                                    proxy=app_doc,
                                    layout_id = "student_application_fe",
                                    layout_mode = 'edit',
                                    use_session=True)
        action = "/passport_entry_view"
        if is_valid:
            args = {}
            if 'apply_admission' not in REQUEST.form:
                args = {'apply_button': 'Apply',}
                psm = 'You successfully uploaded your passport image.'
            else:
                psm = 'You applied for admission.'
        else:
            args = getFormUidUrlArg(REQUEST)
            psm = 'psm_content_error'
    else:
        args = {}
        student.content_status_modify(workflow_action="apply_for_admission")
        wftool.doActionFor(app,'close',dest_container= app)
        psm = 'You applied for admission.'
        action = "/application_view"
else:
    is_valid, ds = app_doc.validate(request=REQUEST, proxy=context, cluster=cluster,
                                use_session=True)

    if action is None:
        ti = app.getTypeInfo()
        action = ti.queryMethodID('edit', 'cpsdocument_edit_form')
        action = '/' + action

    if is_valid:
        comments = REQUEST.get('comments')
        context.cpsdocument_notify_modification(comments=comments)
        if cpsdocument_edit_and_view_button is not None:
            action = ''
        psm = 'psm_content_changed'
        args = {}
    else:
        psm = 'psm_content_error'
        args = getFormUidUrlArg(REQUEST)

args['portal_status_message'] = psm
url = context.absolute_url() + action + '?' + urlencode(args)
REQUEST.RESPONSE.redirect(url)
