Count only active learners against the learners cap#714
Merged
Conversation
Organization.can_enroll_learner() previously counted every Learner row in the organization, regardless of enrollment status — a learner with no enrollment, or only an unverified/completed/deactivated one, still counted against MAX_LEARNERS_PER_ORGANIZATION. It now counts distinct learners with at least one ACTIVE enrollment. Updated all existing cap tests across the enrollment paths (platform, public, EnrollCommand) to give the "existing" learner an active enrollment, since a bare Learner no longer counts. Added tests for the two new behaviors: learners without an active enrollment don't count, and a learner active in multiple courses only counts once (.distinct()). Documented the active-only counting in docs/source/platform/learners.rst. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
`Organization.can_enroll_learner()` (`django_email_learning/models/organizations.py`) previously counted every `Learner` row belonging to the organization against `MAX_LEARNERS_PER_ORGANIZATION`, regardless of enrollment status. A learner with no enrollment at all, or only an unverified/completed/deactivated one, still counted toward the cap.
It now counts distinct learners with at least one `EnrollmentStatus.ACTIVE` enrollment:
```python
def can_enroll_learner(self) -> bool:
cap = self.get_learners_cap()
if not cap:
return True
active_learner_count = self.learner_set.filter(enrollments__status=EnrollmentStatus.ACTIVE).distinct().count()
return active_learner_count < cap