@periodic_task(run_every=timedelta(hours=24))
def invalidate_group_membership():
 from mozillians.groups.models import Group, GroupMembership
 groups = Group.objects.filter(invalidation_days__isnull=False)
 for group in groups:
  curator_ids = group.curators.all().values_list('id', flat=True)
  memberships = group.groupmembership_set.filter(status=GroupMembership.MEMBER).exclude(userprofile__id__in=curator_ids)
  last_update = (datetime.now() - timedelta(days=group.invalidation_days))
  memberships = memberships.filter(updated_on__lte=last_update)
  for member in memberships:
   group.remove_member(member.userprofile)

@ periodic_task ( run_every = timedelta ( hours = 24 ) ) def invalidate_group_membership ( ) : from mozillians . groups . models import Group , GroupMembership groups = Group . objects . filter ( invalidation_days__isnull = False ) for group in groups : curator_ids = group . curators . all ( ) . values_list ( 'id' , flat = True ) memberships = group . groupmembership_set . filter ( status = GroupMembership . MEMBER ) . exclude ( userprofile__id__in = curator_ids ) last_update = ( datetime . now ( ) - timedelta ( days = group . invalidation_days ) ) memberships = memberships . filter ( updated_on__lte = last_update ) for member in memberships : group . remove_member ( member . userprofile )
for groups with defined invalidation_days we need to invalidate user membership after timedelta .