def check(self, message): course_ids = message.context.get('course_ids') if not course_ids: return PolicyResult(deny=frozenset()) course_keys = [CourseKey.from_string(course_id) for course_id in course_ids] if Optout.objects.filter(user__username=message.recipient.username, course_id__in=course_keys).count() == len(course_keys): return PolicyResult(deny={ChannelType.EMAIL}) return PolicyResult(deny=frozenset())
def check(self, message): course_id = message.context.get('course_id') if not course_id: return PolicyResult(deny=frozenset()) course_key = CourseKey.from_string(course_id) if Optout.objects.filter(user__username=message.recipient.username, course_id=course_key).exists(): return PolicyResult(deny={ChannelType.EMAIL}) return PolicyResult(deny=frozenset())
def test_policy_no_course_id(self): """ Make sure the policy denies ACE emails if there is no course id in the context. """ message = self.create_test_message() message.context = {} channel_mods = self.policy.check(message) self.assertEqual(channel_mods, PolicyResult(deny=set()))
def test_policy_optedout(self): """ Make sure the policy prevents ACE emails if the user is opted-out. """ self._set_email_optout(True) channel_mods = self.policy.check(self.create_test_message()) self.assertEqual(channel_mods, PolicyResult(deny={ChannelType.EMAIL}))
def test_policy_optedin(self): """ Make sure the policy allows ACE emails if the user is opted-in. """ channel_mods = self.policy.check(self.create_test_message()) self.assertEqual(channel_mods, PolicyResult(deny=set()))