Merge pull request #16048 from edx/sstudent/EDUCATOR-1288
Sstudent/educator 1288
This commit is contained in:
@@ -28,6 +28,7 @@ from .models import (
|
||||
CourseUserGroupPartitionGroup,
|
||||
UnregisteredLearnerCohortAssignments
|
||||
)
|
||||
from .signals.signals import COHORT_MEMBERSHIP_UPDATED
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -424,7 +425,9 @@ def remove_user_from_cohort(cohort, username_or_email):
|
||||
|
||||
try:
|
||||
membership = CohortMembership.objects.get(course_user_group=cohort, user=user)
|
||||
course_key = membership.course_id
|
||||
membership.delete()
|
||||
COHORT_MEMBERSHIP_UPDATED.send(sender=None, user=user, course_key=course_key)
|
||||
except CohortMembership.DoesNotExist:
|
||||
raise ValueError("User {} was not present in cohort {}".format(username_or_email, cohort))
|
||||
|
||||
@@ -454,7 +457,7 @@ def add_user_to_cohort(cohort, username_or_email):
|
||||
|
||||
membership = CohortMembership(course_user_group=cohort, user=user)
|
||||
membership.save() # This will handle both cases, creation and updating, of a CohortMembership for this user.
|
||||
|
||||
COHORT_MEMBERSHIP_UPDATED.send(sender=None, user=user, course_key=membership.course_id)
|
||||
tracker.emit(
|
||||
"edx.cohort.user_add_requested",
|
||||
{
|
||||
|
||||
6
openedx/core/djangoapps/course_groups/signals/signals.py
Normal file
6
openedx/core/djangoapps/course_groups/signals/signals.py
Normal file
@@ -0,0 +1,6 @@
|
||||
"""
|
||||
Cohorts related signals.
|
||||
"""
|
||||
from django.dispatch import Signal
|
||||
|
||||
COHORT_MEMBERSHIP_UPDATED = Signal(providing_args=['user', 'course_key'])
|
||||
@@ -591,7 +591,8 @@ class TestCohorts(ModuleStoreTestCase):
|
||||
)
|
||||
|
||||
@patch("openedx.core.djangoapps.course_groups.cohorts.tracker")
|
||||
def test_add_user_to_cohort(self, mock_tracker):
|
||||
@patch("openedx.core.djangoapps.course_groups.cohorts.COHORT_MEMBERSHIP_UPDATED")
|
||||
def test_add_user_to_cohort(self, mock_signal, mock_tracker):
|
||||
"""
|
||||
Make sure cohorts.add_user_to_cohort() properly adds a user to a cohort and
|
||||
handles errors.
|
||||
@@ -603,6 +604,10 @@ class TestCohorts(ModuleStoreTestCase):
|
||||
first_cohort = CohortFactory(course_id=course.id, name="FirstCohort")
|
||||
second_cohort = CohortFactory(course_id=course.id, name="SecondCohort")
|
||||
|
||||
def check_and_reset_signal():
|
||||
mock_signal.send.assert_called_with(sender=None, user=course_user, course_key=self.toy_course_key)
|
||||
mock_signal.reset_mock()
|
||||
|
||||
# Success cases
|
||||
# We shouldn't get back a previous cohort, since the user wasn't in one
|
||||
self.assertEqual(
|
||||
@@ -619,6 +624,8 @@ class TestCohorts(ModuleStoreTestCase):
|
||||
"previous_cohort_name": None,
|
||||
}
|
||||
)
|
||||
check_and_reset_signal()
|
||||
|
||||
# Should get (user, previous_cohort_name) when moved from one cohort to
|
||||
# another
|
||||
self.assertEqual(
|
||||
@@ -635,6 +642,8 @@ class TestCohorts(ModuleStoreTestCase):
|
||||
"previous_cohort_name": first_cohort.name,
|
||||
}
|
||||
)
|
||||
check_and_reset_signal()
|
||||
|
||||
# Should preregister email address for a cohort if an email address
|
||||
# not associated with a user is added
|
||||
(user, previous_cohort, prereg) = cohorts.add_user_to_cohort(first_cohort, "new_email@example.com")
|
||||
@@ -650,6 +659,7 @@ class TestCohorts(ModuleStoreTestCase):
|
||||
"cohort_name": first_cohort.name,
|
||||
}
|
||||
)
|
||||
|
||||
# Error cases
|
||||
# Should get ValueError if user already in cohort
|
||||
self.assertRaises(
|
||||
|
||||
@@ -158,7 +158,8 @@ class CreditApiTestBase(ModuleStoreTestCase):
|
||||
|
||||
def setUp(self, **kwargs):
|
||||
super(CreditApiTestBase, self).setUp()
|
||||
self.course_key = CourseKey.from_string("edX/DemoX/Demo_Course")
|
||||
self.course = CourseFactory.create(org="edx", course="DemoX", run="Demo_Course")
|
||||
self.course_key = self.course.id
|
||||
|
||||
def add_credit_course(self, course_key=None, enabled=True):
|
||||
"""Mark the course as a credit """
|
||||
@@ -631,8 +632,6 @@ class CreditRequirementApiTests(CreditApiTestBase):
|
||||
# Configure a course with two credit requirements
|
||||
self.add_credit_course()
|
||||
user = self.create_and_enroll_user(username=self.USER_INFO['username'], password=self.USER_INFO['password'])
|
||||
CourseFactory.create(org='edX', number='DemoX', display_name='Demo_Course')
|
||||
|
||||
requirements = [
|
||||
{
|
||||
"namespace": "grade",
|
||||
@@ -664,7 +663,7 @@ class CreditRequirementApiTests(CreditApiTestBase):
|
||||
self.assertFalse(api.is_user_eligible_for_credit(user.username, self.course_key))
|
||||
|
||||
# Satisfy the other requirement
|
||||
with self.assertNumQueries(24):
|
||||
with self.assertNumQueries(23):
|
||||
api.set_credit_requirement_status(
|
||||
user,
|
||||
self.course_key,
|
||||
@@ -822,7 +821,6 @@ class CreditRequirementApiTests(CreditApiTestBase):
|
||||
# Configure a course with two credit requirements
|
||||
self.add_credit_course()
|
||||
user = self.create_and_enroll_user(username=self.USER_INFO['username'], password=self.USER_INFO['password'])
|
||||
CourseFactory.create(org='edX', number='DemoX', display_name='Demo_Course')
|
||||
requirements = [
|
||||
{
|
||||
"namespace": "grade",
|
||||
|
||||
Reference in New Issue
Block a user