chore: resolve conflicts
This commit is contained in:
@@ -62,7 +62,8 @@ def notification_preferences_viewed_event(request, course_id):
|
||||
)
|
||||
|
||||
|
||||
def notification_generated_event(user_ids, app_name, notification_type, course_key, content_url, content):
|
||||
def notification_generated_event(user_ids, app_name, notification_type, course_key,
|
||||
content_url, content, sender_id=None):
|
||||
"""
|
||||
Emit an event when a notification is generated.
|
||||
"""
|
||||
@@ -78,6 +79,7 @@ def notification_generated_event(user_ids, app_name, notification_type, course_k
|
||||
'notification_app': app_name,
|
||||
'content_url': content_url,
|
||||
'notification_content': content,
|
||||
'sender_id': sender_id,
|
||||
}
|
||||
with tracker.get_tracker().context(NOTIFICATION_GENERATED, context):
|
||||
tracker.emit(
|
||||
|
||||
@@ -30,7 +30,7 @@ class NotificationFilter:
|
||||
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",
|
||||
"NotificationFilter: Course %s does not have a verified mode, so no users will be filtered out",
|
||||
course.id,
|
||||
)
|
||||
return user_ids
|
||||
@@ -41,10 +41,13 @@ class NotificationFilter:
|
||||
)
|
||||
if course_time_limit.enabled_for_course(course.id):
|
||||
enrollments = enrollments.filter(created__gte=course_time_limit.enabled_as_of)
|
||||
|
||||
logger.info("NotificationFilter: Number of audit enrollments for course %s: %s", course.id, enrollments.count())
|
||||
for enrollment in enrollments:
|
||||
content_availability_date = max(enrollment.created, course.start)
|
||||
expiration_date = content_availability_date + access_duration
|
||||
logger.info("NotificationFilter: content_availability_date: %s and access_duration: %s",
|
||||
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)
|
||||
@@ -59,7 +62,7 @@ class NotificationFilter:
|
||||
course = modulestore().get_course(course_key)
|
||||
for filter_name in applicable_filters:
|
||||
logger.info(
|
||||
"Applying filter %s for notification type %s",
|
||||
"NotificationFilter: Applying filter %s for notification type %s",
|
||||
filter_name,
|
||||
notification_type,
|
||||
)
|
||||
|
||||
@@ -92,12 +92,15 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c
|
||||
course_key = CourseKey.from_string(course_key)
|
||||
if not ENABLE_NOTIFICATIONS.is_enabled(course_key):
|
||||
return
|
||||
|
||||
user_ids = list(set(user_ids))
|
||||
batch_size = settings.NOTIFICATION_CREATION_BATCH_SIZE
|
||||
|
||||
notifications_generated = False
|
||||
notification_content = ''
|
||||
default_web_config = get_default_values_of_preference(app_name, notification_type).get('web', True)
|
||||
sender_id = context.pop('sender_id', None)
|
||||
default_web_config = get_default_values_of_preference(app_name, notification_type).get('web', False)
|
||||
generated_notification_audience = []
|
||||
for batch_user_ids in get_list_in_batches(user_ids, batch_size):
|
||||
# check if what is preferences of user and make decision to send notification or not
|
||||
preferences = CourseNotificationPreference.objects.filter(
|
||||
@@ -110,7 +113,7 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c
|
||||
preferences = create_notification_pref_if_not_exists(batch_user_ids, preferences, course_key)
|
||||
|
||||
if not preferences:
|
||||
return
|
||||
continue
|
||||
|
||||
for preference in preferences:
|
||||
preference = update_user_preference(preference, preference.user_id, course_key)
|
||||
@@ -137,16 +140,19 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c
|
||||
course_id=course_key,
|
||||
)
|
||||
)
|
||||
generated_notification_audience.append(user_id)
|
||||
|
||||
# send notification to users but use bulk_create
|
||||
notification_objects = Notification.objects.bulk_create(notifications)
|
||||
if notification_objects and not notifications_generated:
|
||||
notifications_generated = True
|
||||
notification_content = notification_objects[0].content
|
||||
|
||||
if notifications_generated:
|
||||
notification_generated_event(
|
||||
batch_user_ids, app_name, notification_type, course_key, content_url, notification_content,
|
||||
)
|
||||
if notifications_generated:
|
||||
notification_generated_event(
|
||||
generated_notification_audience, app_name, notification_type, course_key, content_url,
|
||||
notification_content, sender_id=sender_id
|
||||
)
|
||||
|
||||
|
||||
def update_user_preference(preference: CourseNotificationPreference, user_id, course_id):
|
||||
|
||||
@@ -124,7 +124,12 @@ class SendNotificationsTest(ModuleStoreTestCase):
|
||||
content_url = 'https://example.com/'
|
||||
|
||||
# Call the `send_notifications` function.
|
||||
send_notifications([self.user.id], str(self.course_1.id), app_name, notification_type, context, content_url)
|
||||
with patch('openedx.core.djangoapps.notifications.tasks.notification_generated_event') as event_mock:
|
||||
send_notifications([self.user.id], str(self.course_1.id), app_name, notification_type, context, content_url)
|
||||
assert event_mock.called
|
||||
assert event_mock.call_args[0][0] == [self.user.id]
|
||||
assert event_mock.call_args[0][1] == app_name
|
||||
assert event_mock.call_args[0][2] == notification_type
|
||||
|
||||
# Assert that `Notification` objects have been created for the users.
|
||||
notification = Notification.objects.filter(user_id=self.user.id).first()
|
||||
|
||||
Reference in New Issue
Block a user