From 5493835a9cb6bb3b75e7549381da16e16e28b4de Mon Sep 17 00:00:00 2001 From: Joseba S Date: Wed, 30 Nov 2022 13:47:17 +0100 Subject: [PATCH] Solved issues with failing tests --- oppia/models/cohorts.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/oppia/models/cohorts.py b/oppia/models/cohorts.py index 2710d17e6..8cf79c1c7 100644 --- a/oppia/models/cohorts.py +++ b/oppia/models/cohorts.py @@ -85,22 +85,24 @@ def update_participants_by_role(self, role): from profile.utils import get_customfields_filter role_criteria = CohortCritera.objects.filter(cohort=self, role=role) - - participants = User.objects + applicable_criteria = 0 + participants = User.objects.all() # as Django's filter() function is accumulative, we can concatenate them as an AND expression for criteria in role_criteria: customfield = CustomField.objects.filter(id=criteria.user_profile_field).first() if not customfield: continue value = criteria.user_profile_value - participants = participants.filter( - get_customfields_filter(value, customfield)) - - for participant in participants: - if not Participant.objects.filter(cohort=self, user=participant, role=role).exists(): - Participant.objects.create(cohort=self, user=participant, role=role) - - return len(participants) + participants = participants.filter(get_customfields_filter(value, customfield)) + applicable_criteria += 1 + + if applicable_criteria > 0: + for participant in participants: + if not Participant.objects.filter(cohort=self, user=participant, role=role).exists(): + Participant.objects.create(cohort=self, user=participant, role=role) + return len(participants) + else: + return 0 class CourseCohort(models.Model):