'Post-processes a form with an S3EmbeddedComponentWidget and
created/updates the component record.
@param form: the form
@param component: the component tablename
@param key: the field name of the foreign key for the component
in the link table'

def _postprocess_embedded(self, form, component=None, key=None):

    s3db = current.s3db
    request = current.request
    get_config = (lambda key, tablename=component: s3db.get_config(tablename, key, None))
    try:
        selected = form.vars[key]
    except:
        selected = None
    if (request.env.request_method == 'POST'):
        db = current.db
        table = db[component]
        post_vars = request.post_vars
        form_vars = Storage(table._filter_fields(post_vars))
        for k in form_vars:
            (value, error) = s3_validate(table, k, form_vars[k])
            if (not error):
                form_vars[k] = value
        _form = Storage(vars=form_vars, errors=Storage())
        if _form.vars:
            if selected:
                form_vars[table._id.name] = selected
                onvalidation = (get_config('update_onvalidation') or get_config('onvalidation'))
                callback(onvalidation, _form, tablename=component)
                if (not _form.errors):
                    db((table._id == selected)).update(**_form.vars)
                else:
                    form.errors.update(_form.errors)
                    return
                s3db.update_super(table, dict(id=selected))
                update_realm = s3db.get_config(table, 'update_realm')
                if update_realm:
                    current.auth.set_realm_entity(table, selected, force_update=True)
                onaccept = (get_config('update_onaccept') or get_config('onaccept'))
                callback(onaccept, _form, tablename=component)
            else:
                form_vars.pop(table._id.name, None)
                onvalidation = (get_config('create_onvalidation') or get_config('onvalidation'))
                callback(onvalidation, _form, tablename=component)
                if (not _form.errors):
                    selected = table.insert(**_form.vars)
                else:
                    form.errors.update(_form.errors)
                    return
                if selected:
                    post_vars[key] = str(selected)
                    form.request_vars[key] = str(selected)
                    form.vars[key] = selected
                    s3db.update_super(table, dict(id=selected))
                    auth = current.auth
                    auth.s3_set_record_owner(table, selected)
                    auth.s3_make_session_owner(table, selected)
                    onaccept = (get_config('create_onaccept') or get_config('onaccept'))
                    callback(onaccept, _form, tablename=component)
                else:
                    form.errors[key] = current.T('Could    not    create    record.')
    return
