Commit 93616b08 by Adam Committed by GitHub

Merge pull request #15108 from edx/adam/fix-bulk-email-for-track

fix for issue where emails to students in one track are sent to users…
parents fd33b38a c478e796
......@@ -107,11 +107,12 @@ class Target(models.Model):
staff_qset = CourseStaffRole(course_id).users_with_role()
instructor_qset = CourseInstructorRole(course_id).users_with_role()
staff_instructor_qset = (staff_qset | instructor_qset)
enrollment_qset = User.objects.filter(
enrollment_query = models.Q(
is_active=True,
courseenrollment__course_id=course_id,
courseenrollment__is_active=True
)
enrollment_qset = User.objects.filter(enrollment_query)
if self.target_type == SEND_TO_MYSELF:
if user_id is None:
raise ValueError("Must define self user to send email to self.")
......@@ -120,13 +121,16 @@ class Target(models.Model):
elif self.target_type == SEND_TO_STAFF:
return use_read_replica_if_available(staff_instructor_qset)
elif self.target_type == SEND_TO_LEARNERS:
return use_read_replica_if_available(enrollment_qset.exclude(id__in=staff_instructor_qset))
return use_read_replica_if_available(
enrollment_qset.exclude(id__in=staff_instructor_qset)
)
elif self.target_type == SEND_TO_COHORT:
return self.cohorttarget.cohort.users.filter(id__in=enrollment_qset) # pylint: disable=no-member
elif self.target_type == SEND_TO_TRACK:
return use_read_replica_if_available(
enrollment_qset.filter(
courseenrollment__mode=self.coursemodetarget.track.mode_slug
User.objects.filter(
models.Q(courseenrollment__mode=self.coursemodetarget.track.mode_slug)
& enrollment_query
)
)
else:
......
......@@ -357,6 +357,43 @@ class TestEmailSendFromDashboardMockedHtmlToText(EmailSendFromDashboardTestCase)
[s.email for s in self.students]
)
def test_send_to_track_other_enrollments(self):
"""
Failing test for EDUCATOR-217: verifies that emails are only sent to
users in a specific track if they're in that track in the course the
email is being sent from.
"""
# Create a mode and designate an enrolled user to be placed in that mode
CourseMode.objects.create(mode_slug='test_mode', course_id=self.course.id)
test_mode_student = self.students[0]
update_enrollment(test_mode_student, unicode(self.course.id), 'test_mode')
# Take another user already enrolled in the course, then enroll them in
# another course but in that same test mode
test_mode_student_other_course = self.students[1]
other_course = CourseFactory.create()
CourseMode.objects.create(mode_slug='test_mode', course_id=other_course.id)
CourseEnrollmentFactory.create(
user=test_mode_student_other_course,
course_id=other_course.id
)
update_enrollment(test_mode_student_other_course, unicode(other_course.id), 'test_mode')
# Send the emails...
test_email = {
'action': 'Send email',
'send_to': '["track:test_mode"]',
'subject': 'test subject for test_mode track',
'message': 'test message for test_mode track',
}
response = self.client.post(self.send_mail_url, test_email)
self.assertEquals(json.loads(response.content), self.success_content)
# Only the the student in the test mode in the course the email was
# sent from should receive an email
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to[0], test_mode_student.email)
def test_send_to_all(self):
"""
Make sure email send to all goes there.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment