'Get the entity/group/user owning a record
@param table: the table
@param record: the record ID (or the Row, if already loaded)
@note: if passing a Row, it must contain all available ownership
fields (id, owned_by_user, owned_by_group, realm_entity),
otherwise the record will be re-loaded by this function.
@return: tuple of (realm_entity, owner_group, owner_user)'

def get_owners(self, table, record):

    realm_entity = None
    owner_group = None
    owner_user = None
    record_id = None
    DEFAULT = (None, None, None)
    if (table and (not hasattr(table, '_tablename'))):
        table = current.s3db.table(table)
    if (not table):
        return DEFAULT
    ownership_fields = ('realm_entity', 'owned_by_group', 'owned_by_user')
    fields = [f for f in ownership_fields if (f in table.fields)]
    if (not fields):
        return DEFAULT
    if isinstance(record, Row):
        missing = [f for f in fields if (f not in record)]
        if missing:
            if (table._id.name in record):
                record_id = record[table._id.name]
            record = None
    else:
        record_id = record
        record = None
    if ((not record) and record_id):
        fs = ([table[f] for f in fields] + [table.id])
        query = (table._id == record_id)
        record = current.db(query).select(limitby=(0, 1), *fs).first()
    if (not record):
        return DEFAULT
    if ('realm_entity' in record):
        realm_entity = record['realm_entity']
    if ('owned_by_group' in record):
        owner_group = record['owned_by_group']
    if ('owned_by_user' in record):
        owner_user = record['owned_by_user']
    return (realm_entity, owner_group, owner_user)
