@@ -321,8 +321,14 @@ def get_membership_cache(self, group_ids=None, is_active=True):
321
321
:rtype: dict
322
322
"""
323
323
membership_queryset = EntityGroupMembership .objects .filter (
324
- Q (entity__isnull = True ) | (Q (entity__isnull = False ) & Q (entity__is_active = is_active ))
324
+ # Select all memberships that are defined by a sub entity kind only
325
+ Q (entity__isnull = True ) |
326
+ # Select memberships that define a single entity (null kind) and respect active flag
327
+ (Q (entity__isnull = False ) & Q (sub_entity_kind__isnull = True ) & Q (entity__is_active = is_active )) |
328
+ # Select memberships that are all of a kind under an entity and only query active supers
329
+ (Q (entity__isnull = False ) & Q (sub_entity_kind__isnull = False ) & Q (entity__is_active = True ))
325
330
)
331
+
326
332
if is_active is None :
327
333
membership_queryset = EntityGroupMembership .objects .all ()
328
334
@@ -387,7 +393,10 @@ def get_all_entities(self, membership_cache=None, entities_by_kind=None, return_
387
393
membership_cache = EntityGroup .objects .get_membership_cache ([self .id ], is_active = is_active )
388
394
389
395
if entities_by_kind is None :
390
- entities_by_kind = entities_by_kind or get_entities_by_kind (membership_cache = membership_cache )
396
+ entities_by_kind = entities_by_kind or get_entities_by_kind (
397
+ membership_cache = membership_cache ,
398
+ is_active = is_active ,
399
+ )
391
400
392
401
# Build set of all entity ids for this group
393
402
entity_ids = set ()
@@ -584,19 +593,29 @@ def get_entities_by_kind(membership_cache=None, is_active=True):
584
593
kinds_with_all .add (entity_kind_id )
585
594
586
595
# Get entities for 'all'
587
- all_entities_for_types = Entity .objects .filter (
588
- entity_kind_id__in = kinds_with_all
589
- ).values_list ('id' , 'entity_kind_id' )
596
+ all_entities_for_types = Entity .all_objects .filter (
597
+ entity_kind_id__in = kinds_with_all ,
598
+ )
599
+ if is_active is not None :
600
+ all_entities_for_types = all_entities_for_types .filter (is_active = is_active )
601
+
602
+ all_entities_for_types = all_entities_for_types .values_list ('id' , 'entity_kind_id' )
590
603
591
604
# Add entity ids to entity kind's all list
592
605
for id , entity_kind_id in all_entities_for_types :
593
606
entities_by_kind [entity_kind_id ]['all' ].append (id )
594
607
595
- # Get relationships
608
+ # Get relationships for memberships defined by all of a kind under a super
596
609
relationships = EntityRelationship .objects .filter (
597
610
super_entity_id__in = super_ids ,
598
- sub_entity__entity_kind_id__in = kinds_with_supers
599
- ).values_list (
611
+ sub_entity__entity_kind_id__in = kinds_with_supers ,
612
+ )
613
+
614
+ # Make sure to respect the active flag for the sub entities under the supers
615
+ if is_active is not None :
616
+ relationships = relationships .filter (sub_entity__is_active = is_active )
617
+
618
+ relationships = relationships .values_list (
600
619
'super_entity_id' , 'sub_entity_id' , 'sub_entity__entity_kind_id'
601
620
)
602
621
0 commit comments