def get_entities_by_kind(membership_cache=None, is_active=True):
<EOL>if membership_cache is None:<EOL><INDENT>membership_cache = EntityGroup.objects.get_membership_cache(is_active=is_active)<EOL><DEDENT>entities_by_kind = {}<EOL>kinds_with_all = set()<EOL>kinds_with_supers = set()<EOL>super_ids = set()<EOL>for group_id, memberships in membership_cache.items():<EOL><INDENT>for entity_id, entity_kind_id in memberships:<EOL><INDENT>if entity_kind_id:<EOL><INDENT>entities_by_kind.setdefault(entity_kind_id, {})<EOL>if entity_id:<EOL><INDENT>entities_by_kind[entity_kind_id][entity_id] = []<EOL>kinds_with_supers.add(entity_kind_id)<EOL>super_ids.add(entity_id)<EOL><DEDENT>else:<EOL><INDENT>entities_by_kind[entity_kind_id]['<STR_LIT:all>'] = []<EOL>kinds_with_all.add(entity_kind_id)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>all_entities_for_types = Entity.objects.filter(<EOL>entity_kind_id__in=kinds_with_all<EOL>).values_list('<STR_LIT:id>', '<STR_LIT>')<EOL>for id, entity_kind_id in all_entities_for_types:<EOL><INDENT>entities_by_kind[entity_kind_id]['<STR_LIT:all>'].append(id)<EOL><DEDENT>relationships = EntityRelationship.objects.filter(<EOL>super_entity_id__in=super_ids,<EOL>sub_entity__entity_kind_id__in=kinds_with_supers<EOL>).values_list(<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>'<EOL>)<EOL>for super_entity_id, sub_entity_id, sub_entity__entity_kind_id in relationships:<EOL><INDENT>entities_by_kind[sub_entity__entity_kind_id].setdefault(super_entity_id, [])<EOL>entities_by_kind[sub_entity__entity_kind_id][super_entity_id].append(sub_entity_id)<EOL><DEDENT>return entities_by_kind<EOL>
Builds a dict with keys of entity kinds if and values are another dict. Each of these dicts are keyed
off of a super entity id and optional have an 'all' key for any group that has a null super entity.
Example structure:
{
    entity_kind_id: {
        entity1_id: [1, 2, 3],
        entity2_id: [4, 5, 6],
        'all': [1, 2, 3, 4, 5, 6]
    }
}

:rtype: dict