'Create or update a record
@param record_id: the record ID
@param data: the data
@param alias: the component alias
@param format: the request format (for audit)
@param hierarchy: the data for the hierarchy link to create
@param link: resource.link for linktable components
@param undelete: reinstate a previously deleted record'

def _accept(self, record_id, data, alias=None, format=None, hierarchy=None, link=None, undelete=False):

    if (alias is not None):
        if ((not data) or ((not record_id) and all(((value is None) for value in data.values())))):
            return (None, Storage())
    elif (record_id and (not data)):
        return (record_id, Storage())
    s3db = current.s3db
    if (alias is None):
        component = self.resource
    else:
        component = self.resource.components[alias]
    table = component.table
    tablename = component.tablename
    if (component._alias != tablename):
        unaliased = s3db.table(component.tablename)
        for field in table:
            field_ = unaliased[field.name]
            field_.default = field.default
            field_.update = field.update
        table = unaliased
    get_config = s3db.get_config
    oldrecord = None
    if record_id:
        accept_id = record_id
        db = current.db
        onaccept = get_config(tablename, 'update_onaccept', get_config(tablename, 'onaccept', None))
        table_fields = table.fields
        query = (table._id == record_id)
        if onaccept:
            oldrecord = db(query).select(limitby=(0, 1)).first()
        elif ('deleted' in table_fields):
            oldrecord = db(query).select(table.deleted, limitby=(0, 1)).first()
        else:
            oldrecord = None
        if undelete:
            if ('deleted' in table_fields):
                data['deleted'] = False
            if (('created_by' in table_fields) and current.auth.user):
                data['created_by'] = current.auth.user.id
            if ('created_on' in table_fields):
                data['created_on'] = current.request.utcnow
        elif (oldrecord and ('deleted' in oldrecord) and oldrecord.deleted):
            return accept_id
        db((table._id == record_id)).update(**data)
    else:
        accept_id = table.insert(**data)
        if (not accept_id):
            raise RuntimeError('Could    not    create    record')
        onaccept = get_config(tablename, 'create_onaccept', get_config(tablename, 'onaccept', None))
    data[table._id.name] = accept_id
    (prefix, name) = tablename.split('_', 1)
    form_vars = Storage(data)
    form = Storage(vars=form_vars, record=oldrecord)
    if ((record_id is None) or undelete):
        current.audit('create', prefix, name, form=form, representation=format)
    else:
        current.audit('update', prefix, name, form=form, record=accept_id, representation=format)
    s3db.update_super(table, form_vars)
    if (link and (link.postprocess is None)):
        resource = link.resource
        master = link.master
        resource.update_link(master, form_vars)
    if accept_id:
        if ((record_id is None) or undelete):
            if hierarchy:
                from s3hierarchy import S3Hierarchy
                h = S3Hierarchy(tablename)
                if h.config:
                    h.postprocess_create_node(hierarchy, form_vars)
            auth = current.auth
            auth.s3_set_record_owner(table, accept_id)
            auth.s3_make_session_owner(table, accept_id)
        else:
            update_realm = get_config(table, 'update_realm')
            if update_realm:
                current.auth.set_realm_entity(table, form_vars, force_update=True)
        component.lastid = str(accept_id)
        s3_store_last_record_id(tablename, accept_id)
        try:
            callback(onaccept, form, tablename=tablename)
        except:
            error = ('onaccept    failed:    %s' % str(onaccept))
            current.log.error(error)
            raise
    if (alias is None):
        return (accept_id, form.vars)
    else:
        return accept_id
