diff --git a/lms/djangoapps/django_comment_client/tests/test_utils.py b/lms/djangoapps/django_comment_client/tests/test_utils.py index 3a0b68b4fbba278c8e9d32a1fe906de2cb76e9aa..65a0b613be6003b84d3e7b6d66cc367272e0550b 100644 --- a/lms/djangoapps/django_comment_client/tests/test_utils.py +++ b/lms/djangoapps/django_comment_client/tests/test_utils.py @@ -38,6 +38,7 @@ from openedx.core.djangoapps.content.course_structures.models import CourseStruc from openedx.core.djangoapps.course_groups import cohorts from openedx.core.djangoapps.course_groups.cohorts import set_course_cohorted from openedx.core.djangoapps.course_groups.tests.helpers import CohortFactory, config_course_cohorts +from openedx.core.djangoapps.request_cache.middleware import RequestCache from openedx.core.djangoapps.util.testing import ContentGroupTestCase from student.roles import CourseStaffRole from student.tests.factories import AdminFactory, CourseEnrollmentFactory, UserFactory @@ -1785,6 +1786,7 @@ class GroupModeratorPermissionsTestCase(ModuleStoreTestCase): 'can_vote': True, 'can_report': True }) + RequestCache.clear_request_cache() set_discussion_division_settings(self.course.id, division_scheme=CourseDiscussionSettings.ENROLLMENT_TRACK) content = {'user_id': self.verified_user.id, 'type': 'thread', 'username': self.verified_user.username} diff --git a/lms/djangoapps/django_comment_client/utils.py b/lms/djangoapps/django_comment_client/utils.py index cd197ac23248764f2ed6d080f51670b2030889a4..edfba0b0d145422c2f16af4fe1bf7600d0a674e0 100644 --- a/lms/djangoapps/django_comment_client/utils.py +++ b/lms/djangoapps/django_comment_client/utils.py @@ -592,15 +592,14 @@ def get_user_group_ids(course_id, content, user=None): content_user_group_id = None user_group_id = None if course_id is not None: - course_discussion_settings = get_course_discussion_settings(course_id) if content.get('username'): try: content_user = get_user_by_username_or_email(content.get('username')) - content_user_group_id = get_group_id_for_user(content_user, course_discussion_settings) + content_user_group_id = get_group_id_for_user_from_cache(content_user, course_id) except User.DoesNotExist: content_user_group_id = None - user_group_id = get_group_id_for_user(user, course_discussion_settings) if user else None + user_group_id = get_group_id_for_user_from_cache(user, course_id) if user else None return user_group_id, content_user_group_id @@ -819,7 +818,7 @@ def get_group_id_for_comments_service(request, course_key, commentable_id=None): _verify_group_exists(group_id, course_discussion_settings) else: # regular users always query with their own id. - group_id = get_group_id_for_user(request.user, course_discussion_settings) + group_id = get_group_id_for_user_from_cache(request.user, course_key) return group_id else: # Never pass a group_id to the comments service for a non-divided @@ -827,6 +826,15 @@ def get_group_id_for_comments_service(request, course_key, commentable_id=None): return None +@request_cached +def get_group_id_for_user_from_cache(user, course_id): + """ + Caches the results of get_group_id_for_user, but serializes the course_id + instead of the course_discussions_settings object as cache keys. + """ + return get_group_id_for_user(user, get_course_discussion_settings(course_id)) + + def get_group_id_for_user(user, course_discussion_settings): """ Given a user, return the group_id for that user according to the course_discussion_settings.