## Script (Python) "add_submit"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
from Products.Silva import mangle

model = context.REQUEST.model
view = context
REQUEST = context.REQUEST

# if we cancelled, then go back to edit tab
if REQUEST.has_key('add_cancel'):
    return view.tab_edit()

# validate form
from Products.Formulator.Errors import ValidationError, FormValidationError
try:
    result = view.form.validate_all(REQUEST)
except FormValidationError, e:
    # in case of errors go back to add page and re-render form
    return view.add_form(message_type="error", message=view.render_form_errors(e))

# get id and title from form, convert title to unicode
id = result['object_id']
# remove them from result dictionary
del result['object_id']

# try to cope with absence of title in form (happens for ghost)
if result.has_key('object_title'):
    title = result['object_title']
    del result['object_title']
else:
    title = ""

# if we don't have the right id, reject adding
if not mangle.Id(context, id).isValid():
  return view.add_form(message_type="error", message="%s is not a valid id." % view.quotify(id))

# process data in result and add using validation result
object = context.add_submit_helper(model, id, title, result)

editable = object
if hasattr(object, 'get_editable'):
    editable = object.get_editable()

# add any extra data to the object
for key in result.keys():
    if hasattr(editable, 'set_%s' % key):
        getattr(editable, 'set_%s' % key)(result[key])

# update last author info in new object
object.sec_update_last_author_info()

# now go to tab_edit in case of add and edit, back to container if not.
if REQUEST.has_key('add_edit_submit'):
    REQUEST.RESPONSE.redirect(object.absolute_url() + '/edit/tab_edit')
else:
    return view.tab_edit(message_type="feedback", 
                         message="Added %s %s." % (object.meta_type, view.quotify(id)))
