## Script (Python) "create_content"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=REQUEST, RESPONSE
##title=Creates content as specified from folder_contents
##
type_name=REQUEST.get('portal_type', None)
if not type_name:
    #TODO: Verify that the user came from folder_contents, and handle other referers.
    RESPONSE.redirect("%s/%s?%s"%( context.absolute_url(), context.getTypeInfo().getActionById('folderContents'), "portal_status_message=Du+valgte+ikke+type."))
    return

contentid = REQUEST.get('new_content_id', '').strip()
if not contentid:
    contentid = context.portal_topicmanagement.generateRandomId()
    while not context.checkIdAvailable(contentid):
        contentid = context.portal_topicmanagement.generateRandomId()

REQUEST.set('type_name', type_name)
context.invokeFactory(type_name=type_name, id=contentid, RESPONSE=RESPONSE)
#TODO: Handle failures more gracefully
if not getattr(context, contentid, None):
    return 'Error'
else:
    return "You were not correctly redirected. Please report this error to the developers"
