-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added audit track expiry filter in notifications (#33381)
* feat: added audit track expiry filter in notifications
- Loading branch information
1 parent
4acd6c2
commit 90b0392
Showing
6 changed files
with
211 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
Notification filters | ||
""" | ||
import logging | ||
|
||
from django.utils import timezone | ||
|
||
from common.djangoapps.course_modes.models import CourseMode | ||
from common.djangoapps.student.models import CourseEnrollment | ||
from openedx.core.djangoapps.course_date_signals.utils import get_expected_duration | ||
from openedx.core.djangoapps.notifications.base_notification import COURSE_NOTIFICATION_TYPES | ||
from openedx.features.course_duration_limits.models import CourseDurationLimitConfig | ||
from xmodule.modulestore.django import modulestore | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class NotificationFilter: | ||
""" | ||
Filter notifications based on their type | ||
""" | ||
|
||
@staticmethod | ||
def filter_audit_expired(user_ids, course) -> list: | ||
""" | ||
Check if the user has access to the course | ||
""" | ||
verified_mode = CourseMode.verified_mode_for_course(course=course, include_expired=True) | ||
access_duration = get_expected_duration(course.id) | ||
course_time_limit = CourseDurationLimitConfig.current(course_key=course.id) | ||
if not verified_mode: | ||
logger.info( | ||
"Course %s does not have a verified mode, so no users will be filtered out", | ||
course.id, | ||
) | ||
return user_ids | ||
enrollments = CourseEnrollment.objects.filter( | ||
user_id__in=user_ids, | ||
course_id=course.id, | ||
mode=CourseMode.AUDIT, | ||
) | ||
if course_time_limit.enabled_for_course(course.id): | ||
enrollments = enrollments.filter(created__gte=course_time_limit.enabled_as_of) | ||
|
||
for enrollment in enrollments: | ||
content_availability_date = max(enrollment.created, course.start) | ||
expiration_date = content_availability_date + access_duration | ||
if expiration_date and timezone.now() > expiration_date: | ||
logger.info("User %s has expired audit access to course %s", enrollment.user_id, course.id) | ||
user_ids.remove(enrollment.user_id) | ||
return user_ids | ||
|
||
def apply_filters(self, user_ids, course_key, notification_type) -> list: | ||
""" | ||
Apply all the filters | ||
""" | ||
notification_config = COURSE_NOTIFICATION_TYPES.get(notification_type, {}) | ||
applicable_filters = notification_config.get('filters', []) | ||
course = modulestore().get_course(course_key) | ||
for filter_name in applicable_filters: | ||
logger.info( | ||
"Applying filter %s for notification type %s", | ||
filter_name, | ||
notification_type, | ||
) | ||
user_ids = getattr(self, filter_name)(user_ids, course) | ||
return user_ids |
This file contains 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
96 changes: 96 additions & 0 deletions
96
openedx/core/djangoapps/notifications/tests/test_filters.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
""" | ||
Test for the NotificationFilter class. | ||
""" | ||
from datetime import timedelta | ||
from unittest import mock | ||
|
||
import ddt | ||
from django.utils.timezone import now | ||
|
||
from common.djangoapps.course_modes.models import CourseMode | ||
from common.djangoapps.student.models import CourseEnrollment | ||
from common.djangoapps.student.tests.factories import UserFactory | ||
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview | ||
from openedx.core.djangoapps.notifications.filters import NotificationFilter | ||
from openedx.features.course_duration_limits.models import CourseDurationLimitConfig | ||
from openedx.features.course_experience.tests.views.helpers import add_course_mode | ||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase | ||
from xmodule.modulestore.tests.factories import CourseFactory | ||
|
||
|
||
@ddt.ddt | ||
class CourseExpirationTestCase(ModuleStoreTestCase): | ||
"""Tests to verify the get_user_course_expiration_date function is working correctly""" | ||
|
||
def setUp(self): | ||
super().setUp() # lint-amnesty, pylint: disable=super-with-arguments | ||
self.course = CourseFactory( | ||
start=now() - timedelta(weeks=10), | ||
) | ||
|
||
self.user = UserFactory() | ||
self.user_1 = UserFactory() | ||
|
||
# Make this a verified course, so we can test expiration date | ||
add_course_mode(self.course, mode_slug=CourseMode.AUDIT) | ||
add_course_mode(self.course) | ||
CourseEnrollment.enroll(self.user, self.course.id, CourseMode.AUDIT) | ||
expired_audit = CourseEnrollment.enroll(self.user, self.course.id, CourseMode.AUDIT) | ||
expired_audit.created = now() - timedelta(weeks=6) | ||
expired_audit.save() | ||
|
||
@mock.patch("openedx.core.djangoapps.course_date_signals.utils.get_course_run_details") | ||
def test_audit_expired_filter( | ||
self, | ||
mock_get_course_run_details, | ||
): | ||
""" | ||
Test if filter_audit_expired function is working correctly | ||
""" | ||
|
||
mock_get_course_run_details.return_value = {'weeks_to_complete': 4} | ||
result = NotificationFilter.filter_audit_expired( | ||
[self.user.id, self.user_1.id], | ||
self.course, | ||
) | ||
self.assertEqual([self.user_1.id], result) | ||
|
||
mock_get_course_run_details.return_value = {'weeks_to_complete': 7} | ||
result = NotificationFilter.filter_audit_expired( | ||
[self.user.id, self.user_1.id], | ||
self.course, | ||
) | ||
self.assertEqual([self.user.id, self.user_1.id], result) | ||
|
||
CourseDurationLimitConfig.objects.create( | ||
enabled=True, | ||
course=CourseOverview.get_from_id(self.course.id), | ||
enabled_as_of=now(), | ||
) | ||
# weeks_to_complete is set to 4 because we want to test if CourseDurationLimitConfig is working correctly. | ||
mock_get_course_run_details.return_value = {'weeks_to_complete': 4} | ||
result = NotificationFilter.filter_audit_expired( | ||
[self.user.id, self.user_1.id], | ||
self.course, | ||
) | ||
self.assertEqual([self.user.id, self.user_1.id], result) | ||
|
||
@mock.patch("openedx.core.djangoapps.course_date_signals.utils.get_course_run_details") | ||
@mock.patch("openedx.core.djangoapps.notifications.filters.NotificationFilter.filter_audit_expired") | ||
def test_apply_filter( | ||
self, | ||
mock_filter_audit_expired, | ||
mock_get_course_run_details, | ||
): | ||
""" | ||
Test if apply_filter function is working correctly | ||
""" | ||
mock_get_course_run_details.return_value = {'weeks_to_complete': 4} | ||
mock_filter_audit_expired.return_value = [self.user.id, self.user_1.id] | ||
result = NotificationFilter().apply_filters( | ||
[self.user.id, self.user_1.id], | ||
self.course.id, | ||
'new_comment_on_response' | ||
) | ||
self.assertEqual([self.user.id, self.user_1.id], result) | ||
mock_filter_audit_expired.assert_called_once() |
This file contains 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