'Returns a query to select the records in table owned by user
@param table: the table
@param user: the current auth.user (None for not authenticated)
@param use_realm: use realms
@param realm: limit owner access to these realms
@param no_realm: don\'t include these entities in role realms
@return: a web2py Query instance, or None if no query can be
constructed'

def owner_query(self, table, user, use_realm=True, realm=None, no_realm=None):

    OUSR = 'owned_by_user'
    OGRP = 'owned_by_group'
    OENT = 'realm_entity'
    if (realm is None):
        realm = []
    no_realm = (set() if (no_realm is None) else set(no_realm))
    query = None
    if (user is None):
        if hasattr(table, '_tablename'):
            tablename = original_tablename(table)
        else:
            tablename = table
        session = current.session
        if (('owned_records' in session) and (tablename in session.owned_records)):
            query = table._id.belongs(session.owned_records[tablename])
    else:
        use_realm = (use_realm and (OENT in table.fields) and self.entity_realm)
        if (OUSR in table.fields):
            user_id = user.id
            query = (table[OUSR] == user_id)
            if use_realm:
                if realm:
                    realm_query = self.realm_query(table, realm)
                    if realm_query:
                        query &= realm_query
                else:
                    query = None
        if (not self.strict_ownership):
            public = None
            if (OUSR in table.fields):
                public = (table[OUSR] == None)
            if (OGRP in table.fields):
                q = (table[OGRP] == None)
                if public:
                    public &= q
                else:
                    public = q
            if use_realm:
                q = (table[OENT] == None)
                if public:
                    public &= q
                else:
                    public = q
            if (public is not None):
                if (query is not None):
                    query |= public
                else:
                    query = public
        if (OGRP in table.fields):
            any_entity = set()
            g = None
            user_realms = user.realms
            for group_id in user_realms:
                role_realm = user_realms[group_id]
                if ((role_realm is None) or (not use_realm)):
                    any_entity.add(group_id)
                    continue
                role_realm = (set(role_realm) - no_realm)
                if role_realm:
                    q = ((table[OGRP] == group_id) & table[OENT].belongs(role_realm))
                    if (g is None):
                        g = q
                    else:
                        g |= q
            if any_entity:
                q = table[OGRP].belongs(any_entity)
                if (g is None):
                    g = q
                else:
                    g |= q
            if (g is not None):
                if (query is None):
                    query = g
                else:
                    query |= g
    return query
